Python Variables

Python Objects

Python objects are like containers that hold data. In Python, every form of data is stored in objects. Normally, you don’t have to worry about how these objects work but for now, just understand that data is stored internally as objects.

What is a variable?

You can think of a variable as a name that you associate with some data. This data can be of any type including string, number, or list of items.

You can define and assign variable by using the “=” sign:

my_var = "Hello World"

The variable name can be anything but with some restrictions.

They can contain only lower case letters (a-z), uppercase letters (A-Z), digits (0-9), and underscore(_). They must begin with a letter or an underscore not a digit. There are a bunch of reserved words in Python, which you can’t use as variable names.

You can assign variable while doing some operation to the data on the right side. Examples:

>>> first_name = "Rayhan"
>>> last_name = "Kamal"
>>> name = first_name + " " + last_name
>>> print(first_name)
Rayhan
>>> length = 10
>>> width = 5
>>> area = length * width
>>> print(area)
50

Understanding types

In Python, every data has some sort of Type. Common types are string, integer, boolean, list and dictionary. Type is set for a variable based on the type of data that is being assiged to the variable. For example, if you do my_var= "I love python", then my_var becomes of type of string.

Instead, if you do my_var = 100, the type of my_var becomes integer. There is an easy way to see the type of a variable, which is using the type() method.

>>> my_var = "I Love Python"
>>> type(my_var)
<class 'str'>
>>> my_var = 100
>>> type(my_var)
<class 'int'>

Mutability (Ability to change)

There are two categories of types – mutable and immutable. If a type is mutable, the value can be changed. If a type is immutable, value cannot be changed.

Types such as boolean, integer, string and float are immutable. List and dictionary are mutable.

Once you assign a value to an immutable object, you can always read from it but if you try to change the value, Python will give an error message.

Data types in action

Python determines data types automatically depending on how you assign value to it. If you assign a number to variable the type becomes “int”. What if you add floating point to this variable?

>>> a = 10
>>> type(a)
<class 'int'>
>>> b = 10 + 2.0

In the above example, the type of “a” is originally int. When you a floating-point number (2.0) to “a”, a new object will be created and the resulting type will be a float.

>>> type(b)
<class 'float'>

There are some restrictions on what types can be added together. For example, you cannot add integer to string.

>>> a = "I was born in the year "
>>> b = 2011
>>> a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int

Finding all keywords in Python

>>> help("keywords")

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

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

Practice Questions

  1. Find the largest number that you can represent using the “int” datatype? Explain why there is a limit on the largest possible number.
  2. Assign 2024 to a variable and print the variable such that it appears as “The next Olympics is in the year 2024”.
  3. Assign a value of 100 to variable and add 20.5 to it. What is the type of the resulting value?
  4. Assign a boolean value (True or False) to a variable “my_var”. What happens if you add “10” to my_var? Why?
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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