3.2 Variables and Data Types



In this session, we looked at how Python allows us to create variables, what naming rules we must follow, and the different data types in Python.

 Python Variables   Naming Conventions and Styles

As we learned Python Variables should not start with digits, contain any special characters or be one of the keywords.

Here’s a list of reserved words, or what we call as keywords in Python 3.6. These have special meanings and purposes and we can’t use them for anything other than those purposes.

 

These keywords may differ across Python versions. Some may be added, while others may be removed. By typing the following at the prompt, you can always obtain a list of keywords in your current version.

>>> import keyword

>>> print(keyword.kwlist)

 If you add values to these keywords or any variable that does not meet the rules we outlined, your script will include syntax errors.

 To increase readability, split the names with underscores as needed, and use Camel Case or Mixed Case.

 Eg:     first_name     FirstName    firstName      

 

 Data Types

 Python variables hold values of different types. Following are a few of the built-in data types of Python

  1.  Numeric Types (int, float, complex)
  2. Text Type (str)
  3. Sequence Types (list, tuple, range)
  4. Mapping Type (dict)

The variable's type is determined by the value you give to it. Remember that, unlike many other programming languages, Python changes the datatype of a variable if its value is changed. The type() method may be used to quickly determine the type of various variables.

Eg:

  • var = 100 # This creates an integer variable with the assigned value
  • var = "Hello" #Type is changed to string in here

You may use the constructor functions to explicitly assign a data type to a variable. You can also use them to convert or cast the data type of one variable to another.

 

  • int() creates an integer from an integer literal, a float (after eliminating all decimals), or a string (if the string represents a whole number)
  • float() - Creates a float number from an integer, float, or string (if the string represents a float or an integer)
  • str() - Creates a string from a variety of data types, such as strings, integers, and float literals. 

Eg:

      a.)

      weight = 55.5

      print(int(weight)) 

 

Here the weight which is assigned a float value will be turned to an integer. The output will be just 55.

 

      b.)

       weight = "55"

       print(int (weight) +5)

In here the weight is converted to int and will display 60 as the output.


Mutable and Immutable Objects

 You learned that in Python, everything is an Object, and each variable stores an object instance. When an item is created, it is issued a unique object id. However, depending on the mutability of the items, this unique identity can alter. Immutable objects cannot be altered once they are created, but Mutable objects may.

 


Mutable objects are those of built-in types such as list, set, and dict.

 Immutable built-in types include int, float, bool, str, and tuple.

 When changing the size or content of an object later in the program, it is best to utilize changeable objects.

 However, immutable objects are not always immutable. We learnt that a tuple in python is immutable. But the tuple consists of a sequence of names with unchangeable bindings to objects.

 


As an example consider a tuple consisting of a string and a list.

 

t = ([10, 12, 15], 'John')

 

Strings are immutable, so we can’t change their value. But the contents of the list can be changed. Here the tuple itself isn’t mutable but contains items that are mutable.