Combining data and processes to produce desired output.
All programs produce data as output!
When you learn a programming language, you learn how the language handles data, and how the language manipulates data (processes), to produce the desired output (data).
In variables
# you can assign/replace
x = 5
# you can print (output)
print(x)
5
# you can use in operations
x + 2
7
vb
Dim x as Integer
x = "hello" ' will give an error
x = "5" ' will convert "5" into 5
x = 6 ' correct assignment
x = 5
y = "7"
print(x+y) # Error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-34-3c4368ee9884> in <module>() 1 x = 5 2 y = "7" ----> 3 print(x+y) # Error TypeError: unsupported operand type(s) for +: 'int' and 'str'
print(x+int(y)) # works, known as type casting
# discover types using type()
type(x)
type(y)
# Works with values and empty values also
type("5") # try type([])
x = None # known as Null, nil, nothing in other languages
x = 10 # integer values (no decimal points)
x = 11.6 # numeric values with decimal points (known as double in VB)
x = 11 + 1j # complex numbers
x = "hello" # identified with double quotes"
print(x[2])
x = [1,2,3,"x",1.1,1,2,3,4]
print(x[1]) # what will this show?
x[1] = 10
print(x)
x = (1,2,3,"x",1.1,1,2,3,4)
print(x[1]) # what will this show?
x[1] = 10 # what will happen here?
print(x)
x = (1,2,3,"x",1.1,1,2,3,4)
# <- What to type here?
x = {1,2,3,"x",1.1,1,2,3,4}
x # In jupyter notebook you do not need to type print to see contents of a variable
Can you spot the difference between a set and a tuple? (there are at least 2)
x = {1,2,3,"x",1.1,1,2,3,4}
# <- type your answer here
Discuss with your instructor your solutions and whether sets are useful.
# Here is an empty dictionary
x = {}
# how to create an empty set then?
# <- answer here
# Here is a non-empty dictionary
x = {
1: "value 1 int",
"1": "value 1 str",
"21112341234": ["student", "data", "here", "Can you retrieve me?"],
}
# try to fetch an item from the dictionary
# try to fetch the last item in the student data list ("Can you retrieve me?")
Everything else you write in a program is to tell the computer how to manipulate data. These are reffered to as processes, functions, operations, methods ...etc. The processes can be categorized into:
These are all the symboles used manipulate and mix data and variables. Main operator types are:
* **
/ // % == =5 + 6 * 2
(5 + 6) * 2
Are a way to execute instructions, only if a certain condition is met.
Consists of:
x = 5
if x > 1: # this is the condition
print("condition 1 is true") # this is the code block
print("This is part of the code block")
if x < 5:
print("condition 2 is true")
condition 1 is true This is part of the code block
x = 5 # change these values to see what happens
y = 10
if x > 2:
if y > 5:
print("y is greater than 5")
else:
print("y is not greater than 5")
print("x is greater than 2")
else:
print("x is NOT greater than 2")
y is greater than 5 x is greater than 2
Two types only:
my_list = [1,3,4,5]
for x in my_list:
print(x)
1 3 4 5
# can you explain what this program does?
my_list = [1,3,4,5]
for x in my_list:
if x%2 == 0:
print(x)
# suggest a modification and do it
4
my_dict = {"123":"Mohammad's record", "222":"Ali's record", "423":"Sara's record"}
for x in my_dict: # not good practice,
print(x) # what will this print?
123 222 423
# better way of doing it
my_dict = {"123":"Mohammad's record", "222":"Ali's record", "423":"Sara's record"}
for x in my_dict.keys(): # clearly you want to iterate the keys
print(x)
123 222 423
for x in my_dict.values(): # clearly you want to iterate the values
print(x)
Mohammad's record Ali's record Sara's record
for x in my_dict.items(): # clearly you want to iterate pairs
print(x)
('123', "Mohammad's record") ('222', "Ali's record") ('423', "Sara's record")
# you can unpack pairs
for k,v in my_dict.items(): # clearly you want to iterate pairs
print("key is {} and value is {}".format(k,v))
key is 123 and value is Mohammad's record key is 222 and value is Ali's record key is 423 and value is Sara's record
x = [5,4,2,1,-1,10,11]
# index first element
x[0]
5
# index last element
x[-1]
# What about indexing item before last?
# index the 3rd element
# get a slice starting from first element to the 3rd (inclusive)
x[0:3]
[5, 4, 2]
# get slice from last element to the 2nd (inclusive)
x[2:-1]
[2, 1, -1, 10]
# get slice from 3rd element to the end of the list
x[3:]
[1, -1, 10, 11]
# get slice from 2nd to last element, to first
x[:-2]
[5, 4, 2, 1, -1]
# how to get a copy of a list using slicing?
# can you think why slicing copies are important?
# replace x with the following string: "hello world"
# and perform the previous command to see what happens.
There are neumerous features to go over in our short review, we will learn as needed. Please refer to the following resources for more information:
If you want to create a list or a dictionary, by looping over the elements of another list or dictionary, then you use list/dictionary comprehension.
For examples, you have a list of numbers, and you want to create a new list containing only the even numbers.
nums = [5,4,2,1,-1,10,11]
# to create new list of even numbers only
even_nums = [x for x in nums if x % 2 == 0]
even_nums
[4, 2, 10]
# you can even perform some operations on the even numbers before storing them
# for example, you want to convert them into strings
str_even_nums = [str(x) for x in nums if x % 2 == 0]
str_even_nums
# you can perform expressions or run functions other than str
['4', '2', '10']