3  Variables and Data Types

3.1 Variable

A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.

Words Explanation
Variable Container to store a value
Keywords (can’t be used a variable name) Reserved Words in Python
Identifiers Class/Function/Variable Name

3.1.1 Keywords


Here is a list of the Python keywords.  Enter any keyword to get more help.

False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not                 

3.2 Variable Types in Python

Every value in Python has a datatype. Different data types in Python are Integers, Floating point numbers, Strings, Boolean, List, Tuple, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.

  • A variable name can contain alphabets, digits and underscores

  • A variable name can start with alphabets or an underscore

  • Variable name is case sensitive

  • A variable name can’t start with a number/digit

  • Spaces are not allowed in variable name

3.2.1 How to Declare and Use a Variable

Let see an example. We will define variable in Python and declare it as “a” and print it.

10
5 6
30 30 30

3.2.2 How to know the Type of a Variable

Python is completely object oriented, and not “statically typed”. You do not need to declare variables before using them, or declare their type, i.e. python automatically identify the type. Every variable in Python is an object.

Here are few basic types of variables. type() function is used to find out the type of a variable.

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>

3.2.3 Re-declare a Variable

You can re-declare Python variables even after you have declared once.

State Bank of Pakistan
TypeError: can only concatenate str (not "int") to str

3.2.4 Type Casting

Type Casting is a process in which we convert a literal of one type to another. built-in functions int(), float() and str() shall be used for typecasting.

  • int() can take a float or string literal as argument and returns a value of class 'int' type.
  • float() can take an int or string literal as argument and returns a value of class 'float' type.
  • str() can take a float or int literal as argument and returns a value of class 'str' type.

3.2.5 Type Casting int to float and string

<class 'int'>
<class 'float'>
<class 'str'>

3.2.6 Type Casting float to int and string

<class 'float'>
<class 'int'>
<class 'str'>

3.3 Exercise

  1. Guess the type of 6/2, 6//2 and check the results.

  2. What is the value of x after the following is run:

    x=4

    x=x/2

  3. Create a variable named carname and assign the value toyota to it.

  4. Assign: 3 to variable glass_of_water and printout the message “I drank 3 glasses of water today” by using variable name.

  5. Let’s try to see what happens after assigning a new value to our variable. Note that program gets executed line by line.

    glass_of_water=3

    glass_of_water=glass_of_water + 1

    print()

  6. Check the type of the variable define above.

  7. Type Cast the above variable first into float and then to string.

  8. Define a variable a =3 and A=‘NIBAF’. Will ‘A’ overwrite ‘a’?

  9. Check the type of a variable assigned using input() function.

  10. Calculate square of the number entered by the user.

  11. Write a program to accept two numbers from the user and calculate multiplication.

  12. Display string “My”, ‘Name’, “Is”, “Python” as “My**Name**Is**Python” Hint: Default separator in print function is space i.e. ” “.

  13. Write a program to take three names as input from a user in the single input() function call.

  14. Define a complex variable. 😎