Python Variables and Collections Declaration and Operations: 1 Lab Overview
Python Variables and Collections Declaration and Operations: 1 Lab Overview
Operations
Department of Computer Science
University of Zakho
Third Year Students
Compiler Design (Practical Part)
October 2, 2020
1 Lab Overview
In this lab, students will learn how to declare variables and collections in
python. Then different operations on variables and collections will be given.
Please follow the instructions given below.
print is used to display result in python. Run the code and see result.
Variables do not need to be declared with any particular type and can even
change type after they have been set. Example: change above code to the
following:
1 x = 6
2 x = " Hello "
3 print ( x )
1
2.1 Variable Names
A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total volume). Rules for Python variables, are the following:
• A variable name can only contain alpha numeric characters and un-
derscores (A − z, 0 − 9, and underscores )
• Variable names are case-sensitive (age, Age and AGE are three differ-
ent variables)
2
2.4 Expressions Using Different Variables
Variables are used to create expressions using different operations. Change
given code to include the following:
1 x = 5
2 y = 4
3 z = 2
4 x = x + y *(3+ z )
5 print ( x )
6 x = y = ((3+ x ) +( z -2) *5)
7
8 print ( x )
9 print ( y )
10 m = x // 5 # // is div in python
11 print ( m )
12 n = x % 5 # % is mod
13 print ( n )
14 k = x ** 2
15 print ( k )
3
3 Collections in Python
When choosing a collection type, it is useful to understand the properties
of that type. Choosing the right type for a particular data set could mean
retention of meaning, and, it could mean an increase in efficiency or security.
In this lab, it will be shown how Lists, tuple and sets can be implemented
in Python, based on different operations.
Start this by writing a new python file with name Lab2_collections.py.
Add code given in each section into this file and see corresponding results.
You can change the code as you wish and ask lab assistants, if you get any
problems.
In programming, data type is an important concept. Variables can store
data of different types, and different types can do different things. Python
has the following data types built-in by default:
• Float: is number.number
1 x = 6.8
2 print ( x )
• List: is a list of objects. These object can be from the same or different
types.
1 x = [ 4 ,5 , " 6 " ,[4 ,6]]
2 print ( x )
3
4
• Range(n): is 0, 1, 2, ..., n
1 x = range (5)
2 print ( x )
5
1 s = " Hello5161 "
2 l = [1 ,2 ,3 ,4 ,5]
3 t = ( " Ali " ," Ahmed " , " Rame " )
4 set = {2.3 ,4.4 ,5.6}
5
6 # Printing first items ([0]) ( str is used to convert integers
to string )
7 print ( " First item in s is : " + s [0])
8 print ( " First item in l is : " + str ( l [0]) )
9 print ( " First item in T is : " + t [0])
10
11 # Printing last items [ -1]
12 print ( " Last item in s is : " + s [ -1])
13 print ( " Last item in l is : " + str ( l [ -1]) )
14 print ( " Last item in t is : " + t [ -1])
15
16 # Printing item indexed at i
17 i = 4
18 print ( str ( i ) + " item is : " + s [ i ])
6
3.5 Printing a range of items in Collections (Lists and Tu-
ples)
[first:last] is used to print a range of items starting from the index first (first
is included) to the index last (index last not included). Positives indexes
starting from the first item in the tuple. Negative indexes meaning starting
from the end.
Add this code and see results. Then, you can use different indexes and
see corresponding results. Ask if you need help.
1 # Printing a range of items in Lists and tuples
2 t = (1 ,2 ,3 ,4 ,5 ,6)
3 l = [5 ,7 ,8 ,8 ,9]
4 print ( t [2:4])
5 print ( t [ -4: -2])
You will see an error. Look at the error and see what it is. Then delete
above code and add the following:
1 # Changing tuples using lists
2 t = (1 ,2 ,3 ,4 ,5 ,6)
3 l = list ( t )
4 l [2] = 9
5 t = tuple ( l )
6 print ( t )
7
3.7 Sets Collections
Add the following code into Lab2_collections.py to see how data can
be added into sets and how sets are merged.
1 # sets and their methods
2 s1 = {1 ,2 ,4 ,5 ,6}
3 s2 ={ " a " ," b " ," c " }
4 print ( s1 )
5 s3 = s1 . union ( s2 )
6 s4 = {4 ,5 ,6 ,7 ,8}
7 s5 = s1 . union ( s4 ) # all items in both sets with no repeating (
duplicating )
8 s6 = s1 . intersection ( s4 ) # Items sharing both sets
9 print ( s3 )
10 print ( s5 )
11 print ( s6 )
You can see that when merging two sets having similar items, only one
of such items are in the result. See result of print(s5).
2. Write a code to print True if a > b and a > c , otherwise print False.
Use logical operators.
8
3. If you get time, apply methods given in the table 3 on sets, by changing
given code.