Variables And Datatypes

Requirements:
1. Linux Distribution
2. python3
3. A Shell Interface

My Setup:
1. Debian GNU/Linux
2. python3
3. BASH

Despite the fact that python is a dynamically typed, variables still have a type to them. Variables are locations in memory which you may store values to be used within your program. The fundamental data types consist of :

  1. None – object that specifies a lack of value . The only value this can be is None
  2. bool – a data type which can only hold the values “True” or “False”. Evaluate to 1 and 0 respectively.
  3. int – a data type which represents whole numbers.
  4. float – a data type which represents floating-point numbers
  5. complex – a data type which represents complex numbers
  6. str – a data type which represents strings of characters. These can not be changed, and any operations on it will return a new string.
  7. list – a data type which represents an indexed list of objects. Operations on these will modify the current variable.
  8. tuple – a data type which represents an indexed list of objects. Operations on these will not modify the current variable, but return a new one.
  9. dict – a data type that represents a hash table for storing unordered key-value pairs. Operations on this will modify the current variable.
  10. set – a data type that represents an unordered list of unique objects. Operations on these will modify the current variable.
  11. file – a data type that represents a file object.

There are many data types within the modules that python has to offer, and you can create your own as well. In the previous chapter, I created an example of declaring a variable by writing a variable name and assigning it a value.

myNoneObj = None
myInt = 43
myStr = "This is A String"
myFloat = 3.1415
myComplex = 1 + 2j

print(type(myNoneObj)) # should result in NoneType
print(type(myInt))     # should result in int
print(type(myStr))     # should result in str
print(type(myFloat))   # should result in float
print(type(myComplex)) # should result in complex
clim@debian:~/Desktop/tests/pytests/typeTest$ python3 typetest.py
<class 'NoneType'>
<class 'int'>
<class 'str'>
<class 'float'>
<class 'complex'>
clim@debian:~/Desktop/tests/pytests/typeTest$

When declaring variables, there are some rules that we should keep in mind.

  1. The variable name cannot start with a number. It can only start with a character or an underscore.
  2. Variables in python are case sensitive.
  3. They can only contain alpha-numeric characters and underscores.
  4. No special characters are allowed.

Leave a Reply