Declaring Variables and Inputs in Python - Ultimate Python Course Part - 3

Declaring Variables & Input Methods in Python




  • Rule for giving name to variable

In Python, there are some rules to give the name to the identifiers. 

  1. Name must not be a keyword word and should not contain space in between. For example: price, marks, etc.
  2. Must be made up of only letters, numbers and underscore (_). Letters can be in Uppercase and Lowercase. For example: max_marks, player_speed, etc.
  3. Variable name cannot start with a number but can contain number in between or end. For example: exam2021, etc.
Some valid identifiers are :- File, _python123, Hello_world, etc.
All above examples follow the rules mentioned and thus these are valid identifiers.

Some invalid identifiers are :- 23price, input, if, cat.animal, etc.
In above examples:
  1. 23price start with number and thus is not valid.
  2. input and if are keywords and thus we can't use it as identifier.
  3. cat.animal contains (.) which is not allowed. You can only use numbers, letters and underscore.

  • Assigning value to variables

Python is very easy language and if you know English you can easily write and understand the python syntax. In this part firstly we will be assigning values to the variables. So, let's learn to assign basic data like text, integer, float (decimal values) to the variables.

  • Text Assignment

For assigning a text to a variable let "a" you just have to write a = and then text in inverted quotes. For example:-  a = "Python Course". The text written in inverted quotes are taken as text/ string in python.

a = "Python Course"
print(a)

If you will write print(a), and then run the code, you will find output as "Python Course". 

  • Numerical Assignment

For integer assignment in variable let "num", the syntax is just num = and then the number you want to assign. For example here I am declaring a variable named num whose value is assigned as 34. If you print this variable, you will find 34, printed in the terminal.

num = 34
print(num)

Similarly, you can assign the float value (decimal). For example:- here I am declaring a variable "b" whose value is assigned as 24.56. Now, if you will print b then you will get the value of b printed in the terminal.

b = 24.56
print(b)

In this way, you can declare and assign value to the variable in python.

Now, let's learn how to take input from user.

  • Taking Input from User

In python syntax for taking input from the user in terminal is very simple. It is explained by a example as follows:-

r = input("Enter radius of Circle")

In above example, "r" in the name of variable in which the value taken by the user will get store.
"input" is the keyword used for taking input in python. After writing "input" you have to give parenthesis and then inside these you can write a string, which will be shown in the terminal and will help the user to understand what to input. There would be no error, if you do not write anything inside the parenthesis.

So, for basically you can understand syntax as :-

VariableName = input("Message")

Now, what will be the data type of Varaible and how can we change it??

By default the input in python is in string format/ text format. But for arithmetic calculations you have to input in integer or float format. 

So, for taking input in integer type, the syntax is like this

r = int(input("Enter radius of Circle"))

You have to first write "int"  then the parenthesis and after that whole input thing inside the that parenthesis. Note that if you open the parenthesis and does not close it, then it will show you a syntax error.

Now, if you print the data type of the variable "r" using the following command below, you will see "<class 'int'>" in the terminal. This means that the data type of "r" is integer.

print(type(r))

Similarly, you can take input in float type also. For taking input in float (decimal) value syntax is same as that of int just you have to replace "int" with "float".

r = float(input("Enter radius of Circle"))

Now, if you print the data type of the variable "r" using the following command shown above, you will see "<class 'float'>" in the terminal. This means that the data type of "r" is float.

Here, we finish the part of taking input from the user in all three basic data types i.e. string, integer and float.


If you have not watched the Part- 3 of the course on my channel then visit to my channel from the link in footer and watch it for the video explanation of the Part - 3 of the Ultimate Python Course.

Hope this part would be clear to you and you would be liking the content. If any query you can ask in the comments below.

Enjoy the Coding!!

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post