Uncategorized

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.

  1. Variables are the most important thing and all programming belongs to these.
  2. Not a single program can be complete without variables.
  3. Variables are the memory locations where data stores before and after the processing.
  4. Memory locations are the circuits that store data in the form of electric charge.
  5. These circuits are manufactured in memory when memory chips fabricate in the factory.
  6. Every memory location has a unique number that is called memory address.
  7. Memory addresses are very long because there may be billions of memory addresses in RAM.
  8. It is difficult to remember the memory address for a developer so we assign a name to the memory location.
  9. This is similar to a person having an ID card number which may be very long but we use names to remember a person.
  10. In Python we create variables by assigning values to them
num = 10

Code1: Creating a Variable and Assign the Value

  1. The Code 1 shows how we create and assign a value to a variable in Python
  2. We created a variable named num and then assigned a value 10 to it.
  3. 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

  1. A variable can store only one value at a time. If we assign another value in it then the old value will be removed.
  2. In a program, developers have to use many variables, even thousands.
  3. 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”.
  4. The kind of data is also called data type.
  5. Every language has a set of its own data types.
  6. 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

  1. 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

  1. In code we convert a variable name num.
  2. We assign it a value 10 and print it.
  3. On the next line convert the data type and assign it to again num.
  4. And print it again and the following output in terminal.

Figure 2: Output of Code 2

  1. We can see that the first output is simple 10 because we print an integer.
  2. 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

  1. This time we convert a float number into an integer.

Figure 3: Output of Code 3

  1. First, we print the float which printed as 10.8 but on the next line we convert it to integer and print again.
  2. The integer value drops the decimal part and only prints 10. Remember that this conversion will not perform round-off by default.
  3. 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

  1. This is a simple conversion from integer to string.
  2. In output we found no difference because this conversion will not change the value of data.
  3. 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

  1. This will convert successfully without any error or crash but following code will not work.
  2. 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

Leave a Reply

Your email address will not be published. Required fields are marked *