Variables / Type conversion
M3-R5.1 · Chapter 3: Introduction to Python · 6 min read
Variable
- Variables in Python are like containers that store data.
- You can think of them as labels( name that represents some value) for data you want to use in your program.
- Variables in a computer program are not quite like mathematical variables. They are placeholders for locations in memory.
- In Python, variables are used to store data that can be accessed and manipulated later in your program.
How to Declare Variables?
In Languages like C, C++ or Java the Declaration of Variable is in such way like int a = 6 ; Means here we are declaring a variable ‘a’ with value ‘6’ of type ‘int’ .
But In Python, variables are created by assigning values to a name like
>>> a = 6
this is called an assignment. Means we assign the value 6 to the variable a
In Python we are not required to declare variable of type explicitly .Based on your provided values it will be Considered automatically by the Python Virtual Machine.
Rules for naming Variable in python
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable cannot contain punctuation marks.
- Variable name should be meaningful and short, and it cannot contain spaces.
- A python keyword cannot be used as a variable name.
- A variable name can only contain alpha-numeric characters and underscores
- Variable names are case-sensitive (jai, Jai and JAI are three different variables)
Example:
>>> name = "Jack" # String variable
>>> age = 25 # Integer variable
>>> height = 5.7 # Float variable
>>> is_student = True # Boolean variable
NOTE: Starting the name with a single or double underscore has a special meaning in python.
Assigning Values: You can assign values to variables using the = operator:
Example:
x = 10
y = x + 5
Multiple Assignments: You can assign values to multiple variables in one line:
Example: a,b,c = 1,2,"jai“
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "jai" is assigned to the variable c.
Or Assign the same value to multiple variables:
Example: x = y = z = 100
Here, an integer object is created with the value 100, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables.
Dynamic Typing: Python allows variables to change type during the program:
x = 10 # x is an integer
x = "Hello" # Now, x is a string
Deleting Variables: You can delete a variable using the del statement:
x = 10
del x
NOTE: Every assignment statement has two parts: a Left hand side (L-Value) and a right hand side (R Value).
NOTE: In an assignment statement, the variable that is receiving the value must appear on the left side of the = operator
Output Variables: The Python print statement is often used to output variables. Variables do not need to be declared with any particular type and can even change type after they have been set.
Example:
x = 5 # x is of type int
x = "prakash " # x is now of type str
print(x)
Output: prakash
To combine both text and a variable, Python uses the “+” character:
Example :
x = "awesome"
print("Python is " + x)
Output : Python is awesome
You can also use the + character to add a variable to another variable:
Example :
x = "Python is "
y = "awesome"
z = x + y
print(z) Output: Python is awesome
Note: In Python variable do not need declaration to reserve memory space. The variable declaring
or variable initialization happens automatically when we assign a value to a variable.
Type Conversion (Typecasting)
The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.
Sometimes we need to convert data type. Suppose. If we want to add two numbers in which one value of a variable is an integer and the second is a string. Then we need to use typecasting to convert string data type into integer before we perform addition. Casting in python is done using constructor functions
|
For integer use int() |
x=int(1) , y=int(2.8) # y will be 2 , z=int(”3”) # z will be 3 |
|
For float use float() |
x=float(1) , y= float (2.8) # y will be 2.8, z= float(”3” ) # z will be 3 |
|
For string use str() |
x=str(“s1”) , y=str(2) # y will be ‘2’ , z=str(3.0) # z will be ‘3.0’ |
NOTE: Converting one data type in to another data type is called typecasting.