This section provides a brief overview of:
In this section I am going to focus on integer, floating points, Boolean data types and associated arithmetic operations. 5 is an example of an integer data type whereas 5.0, 6.2 are examples of floating point data types. All standard arithmetic operators are defined in Python.
Note that python also has a string data type which is not covered in this tutorial.
3 + 2
3 - 1
3 / 2
3 // 2 # Integer division
3 ** 2 # 3 to the power of 2
3 % 2 # This is the remainder operator
Python treats every line as a statement to be executed except the line beginning with a #. # indicates the line contains comments to the right of # sign which are not to be executed.
Python has the following comparison operators on all relevant data types:
Boolean is another data type which can take two values – true or false.
3 == 2
3 != 2
(5 > 3) and (2 < 1)
(5 > 3) or (2 < 1)
In Python, there is no need to declare variables before assigning them.
x = 3 # Assigns 3 to an integer variable x
print(x)
y = 5.2 # Assigns 5.2 to a floating point variable y
print("y = ", y)
x + y
float(x) # converts to floating point
int(y) # converts to integer
type(x) #prints the variable type
You can use lists to store a sequence of values. Note than in Python list indexes start at 0 (similar to C or C++). Different ways of creating lists are shown below
a = [] # creates an empty list
a = [8, 9, 10] # creates a list of three elements
a[1] # Prints the second element
len(a) #prints the length of the list
a.append(20) #Adds 20 to the end of the list
a
a.pop() # Deletes the last element
a
a.reverse() # reverses the list
a
a.sort() #sorts the list
a
b = [11, 12, 13, 14]
a = a + b #lists can be concatenated
a
c = a[2:5] #creates a sublist from third to fifth element
c
c.remove(11) # removes 11
c
20 in b # Returns a boolean value for the check if element 20 is in list b
11 in b
list(range(start, stop, stepsize)) can be used to create a list comprising of a sequence of numbers. Note that the number corresponsing to stop is not a part of the list.
list(range(10,20,2))
Lists can be two dimensional.
twodlist = [[1, 2, 3],[11, 12, 13],[21, 22, 23]]
twodlist[1]
twodlist[1][1]
Along similar lines we can create, three, or four dimensional lists and so on. You can create lists of non-integer objects also.
cities= ["Nashville", "Austin", "Pittsburgh"]
cities
Lists can contain objects of different type.
d = [5, 6, "seven", 8, 9]
d
del d[2] #removes the third element
d
Tuples are like lists but with two main differences:
Tuples are faster than lists. However, the speed gains might not be significant for our applications. Therefore, stick to lists whenever possible.
e = (3, 8, 11, 7) # creates a tuple
e
e[2] # we use square brackets to access elements of tuples
Dictionaries can be used to create maps or unordered collection of objects in Python. In dictionaries, keys are associated with values. The keys are separated from their values using colon. Each key-value item is separated using comma.
pop = {'Nashville': 200, 'Austin': 500, 'Pittsburgh': 300}
pop.keys()
pop.values()
The keys and values can be converted to lists using list() function.
list(pop.keys())
pop['Nashville']
'Austin' in pop
If loops can be used to execute codes based on conditions.
Note that indentations are important for defining code blocks. Normal indentation is one tab or four whitespaces. The code will not compile properly and give errors if it is not indented properly.
x = 10
print(x)
if x > 5:
print("x is larger than 5")
elif x < 5:
print("x is smaller than 5")
else:
print("x is equal to 5")
While loops execute a section of code until a condition is met. The following code prints 1 through 10.
y=0
while y <= x:
print(y)
y=y+1
For loops can be used to iterate over elements in lists, tuples, or dictionaries.
f = [1, 5, 9, 10]
for i in f:
print(i*2)
for i in range(20, 30):
if (i % 2 == 0):
print(i)
In dictionaries, the default is to iterate through the keys.
for i in pop:
print(i)
for i in pop.values():
print(i)
for i,j in pop.items():
print(i,j)
For loops and conditional expressions can be used to create lists conveniently.s
[i for i in range(3)]
[i*2 for i in [10, 20, 25]]
[[i,j] for i in [1, 2, 3] for j in [10, 11, 12]]