Hello, My aspiring programmers. Welcome to another class of Python Programming. I am your host and teacher, Dolores Haze. Till now, we have covered what is a programming language and a little basic. In the last class, we have seen how to set up a programming environment and we have also written our first program. If you haven’t seen those two, please watch them out, You can check them out here: Episode #1 & Episode#2. Now go on, I am waiting. If you had, Let’s continue with today’s class.
There are lots of terms in Python Programming like Keywords, Identifiers, Constants, Variables, etc.
A lot right? Well, Don’t worry! We will look at them, one by one. So Let’s start.
What are Keywords?
Keywords are the reserve words that throw or have some special meaning.
Well, but what does that mean? Keywords are words that you can’t use anywhere without any restriction, it does have a meaning and you have to deal with them in a context. It’s like an iPhone or Mac which has some proprietary software or applications only for Mac users, you can’t use them in Windows pc.
Some of the keywords are int, char, float, def, etc.
What are Variables?
Variables are the most important elements in a program that allocates space in memory with a specific address. It is a symbolic name given to that memory location. The variable is an element whose value can be changed during runtime of a program.
Woah! Quite a definition. Now here comes the analogy: variables are a container you have to store your data in the memory. Like when we want to store some biscuits or beans or pulses, what we do then, we store them boxes or containers, the same way if we have to store some numeric or text values, we have variables for that.
For Example: int a = 10
Here we have store the data which is 10 to variable “a” of int data-type. We will come to data types later. But for now, a is our container which holds a value 10, which will be stored in some random location in the memory. We can also have a specific memory address where data has been stored but it is a detailed topic and will come later.
Now you know, what is variable is and how to use one, but now you will ask if there are any rules for naming variables. Yes, there are some rules for naming variables:
- A variable name should not be a keyword. As told you earlier, keywords have special meaning, which can’t be used as a variable.
- The variable name should be identical. Yes, you can use a,b….z but in some specific programs its better to use some valid names like area, salary, etc.
- A variable name can be started with an underscore. Ex – _myfile
- Variable name should always start with an alphabet. Ex – class1
Those were some rules for naming variables, next we will see, how to initialization of variables.
Initialization of Variables
There are two types of variable initialization:
- Static Initialization
- Dynamic Initialization
Static Initialization: When the variable is initialized with a fixed value at the time of its declaration, it is referred to as static initialization. Ex – int a = 10.
Dynamic Initialization: The process of initializing a variable at the time of its declaration at runtime is known as Dynamic Initialization of a variable.
sal = int(input(“Enter salary: “))
bonus = sal + 1000
Here, we don’t give a value before, instead, we ask for the input for salary from the user and then calculate the bonus. We will later see, what the statements from input means.
What are Data types?
Data types specifies which type of value is to be stored in a variable for doing any specific operation.
As we have seen earlier, in Ex: int a = 10. Here, int is an integer, it describes the type of data in variable a. Similarly, there are two other major data types in python which are: “char” for character and “float” for decimal values. Nothing fancy in it, just write down the data type, the data you are dealing with.
What is Typecasting?
Typecasting is converting one data type to another. Like we have seen before while inputting the statement we write: sal = int(input(“Enter salary: “))
Usually, we don’t write like this, we ask for an input in python like sal = input() or sal = input(“Enter salary: “). So why use int then? Well, that’s because the default data type of input parameter is string or text. So, it expects a string or text input but here we dealing with numbers. So, we change it to int so that we can have a deal with it. You can typecast other datatypes also like float and char.
Woah! A lot today huh? Well, It is not over yet, how can we end the class without writing a program.
So let’s make a program where we have to find out the sum of two numbers. Open up your VS Code IDE and start programming.
Here, we have two variables a and b, we have Typecasted the input to int and the result which is stored in variable sum is then printed.
That’s the beauty of Python. In python, we don’t even have to initialize the variable data type which is int or float we can do it but it is not compulsory like in other languages like C or C++.
One bonus tip is that you can install Python extension in VS code which will help you to output your programs easily and quickly without any trouble on the terminal screen.
Well, That’s all for today, today we have seen what keywords are, variables, data types, typecasting. This is Dolores Haze, signing off. I will meet you in the next class till then “Happy Coding”.