08. Variables & Data Types
Variables & Data Types
Variables are responsible for data storage during program execution. All programs required data and the variables that store this data. It looks that variables provide a linkage between input devices to CPU and CPU to output devices.
- Variables are the most important thing and all programming belongs to these.
- Not a single program can be complete without variables.
- Variables are the memory locations where data stores before and after the processing.
- Memory locations are the circuits that store data in the form of electric charge.
- These circuits are manufactured in memory when memory chips fabricate in the factory.
- Every memory location has a unique number that is called memory address.
- Memory addresses are very long because there may be billions of memory addresses in RAM.
- It is difficult to remember the memory address for a developer so we assign a name to the memory location.
- This is similar to a person having an ID card number which may be very long but we use names to remember a person.
- In Python we create variables by assigning values to them
num = 10
Code1: Creating a Variable and Assign the Value
- The Code 1 shows how we create and assign a value to a variable in Python
- We created a variable named num and then assigned a value 10 to it.
- When Code 1 is executed, it will ask the operating system to reserve a memory location in RAM and name it num and store the value 10 inside it.
Figure 1: Memory Map and Variable
- A variable can store only one value at a time. If we assign another value in it then the old value will be removed.
- In a program, developers have to use many variables, even thousands.
- There are multiple types of data that are required to store in variables like a number 10 or some letters “A” or even many letters “Hello”.
- The kind of data is also called data type.
- Every language has a set of its own data types.
- Following data types are supported in Python.
Name What it will store Example
Integer Negative and positive numbers without decimal point a = 10
Float Negative and positive numbers with decimal point b = 10.5
String Characters including symbols, numbers and others a = “Hello”
Boolean Only store True or False a = True
Table 1: Popular data types available in Python
- Data can convert data type by using explicit methods but with some restrictions.
num = 10print (num)num = float(num)print (num)
Code 2: Converting and Printing an Integer to Float
- In code we convert a variable name num.
- We assign it a value 10 and print it.
- On the next line convert the data type and assign it to again num.
- And print it again and the following output in terminal.
Figure 2: Output of Code 2
- We can see that the first output is simple 10 because we print an integer.
- While the second output is 10.0 because we print a float number.
num = 10.8print (num)num = int(num)print (num)
Code 3: Converting Float to Integer
- This time we convert a float number into an integer.
Figure 3: Output of Code 3
- First, we print the float which printed as 10.8 but on the next line we convert it to integer and print again.
- The integer value drops the decimal part and only prints 10. Remember that this conversion will not perform round-off by default.
- Similarly, we can convert any integer, float, bool and string into each other but if we convert any string into number or bool, we should be careful.
num = 10print(num)s = str(num)print (s)
Code 4: Converting Integer to String
- This is a simple conversion from integer to string.
- In output we found no difference because this conversion will not change the value of data.
- But conversion from string to integer is not easy and causes the program to crash.
s = "123"print(s)num = int(s)print (num)
Code 5: String to Integer Conversion without Issue
- This will convert successfully without any error or crash but following code will not work.
- Following will not work because string is not a proper number
s = "Hello"print(s)num = int(s)print (num)
Code 6: Error causing due to type casting
Figure 4: Error Message for Code 6








