this file is derived from exercise 0
#1. Variables and data types
1.1 operatorSome relational operators:
| Symbol | Task Performed |
|---|---|
| == | True, if both sides are equal |
| != | True, if both sides are not equal |
| < | True, if the left side is smaller |
| > | True, if the left side is greater |
| <= | True, if the left side is smaller or equal to the right side |
| >= | True, if the left side is greater or equal to the right side |
- print()
x = "Hello" y = "World" print (x + y) # "HelloWorld"
- type(variable)
- str(variable)
- len(variable)
List can contain datas with same or different types, List is mutable.
lol = [[1,2,3], ["a","b","c"], [1.2,2.3,4.5,6.7,8.9]]
- lol.append(value)
- x = lol.pop(), pop out the last element of lol
- lol.length()
Tuples are just the same as lists, but are immutable.
t = (1,2,3) t1 = ((1,2),(1,3),3)2.3 Set
Sets are similar collections, but have no order and can contain each element only once.
s = set()
s.add(50)
s.add(20)
s.add(10)
s.add(20)
#s = {10, 20, 50}
- traverse all elements of a list and extract them to a set
l = [2,3,4,5,6,2,3,4,5,2]
s = set()
for x in l:
s.add(x)
list (set(l))
#[2, 3, 4, 5, 6]
2.4 Dictionary
Python’s built-in mapping type. They map keys, which can be any immutable type, to values, which can be any type.
- initialization 1st
presidents_inauguration = {}
presidents_inauguration ['Trump'] = 2017
presidents_inauguration['Obama'] = 2009
presidents_inauguration['Bush'] = 2001
print(presidents_inauguration)
#{'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
- initialization 2nd
d = {'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
- keys, values
print (presidents_inauguration.keys()) #dict_keys(['Trump', 'Obama', 'Bush']) print (presidents_inauguration.values()) #dict_values([2017, 2009, 2001])
- len(dic)
control flow in Python noticeable does not use ANY (,),[,],{,},…
Instead indendation determines what belongs to block of commands
- if-elif-else
x = 15
if x > 20:
print ('This is a very big number!')
elif x > 10:
print ('This is a big number!')
else:
print ('this is a small number!')
- loops
first_names = ['John', 'Paul', 'George', 'Ringo']
for name in first_names:
print("Hello " + name + "!")
- range function
Contrary to many other programming languages there is no built-in for… counting loop.
However, you can use the range function:
for x in range(10,25,5):
print (x)
#10
#15
#20
- enumerate
# enumerate is a useful convenience functions:
for index, name in enumerate (first_names):
print("Name "+ str(index) + ": " + name)
# Name 0: John
# Name 1: Paul
# Name 2: George
# Name 3: Ringo
- while loop
# while loops functions very similar to many popular languages:
x = 1
while x < 100:
x = x * 2
print(x)
2.6 Functions
- definition
def print_all_names(names):
for x in names:
print (x)
- lambda function
Lambda functions are a concise way of defining functions, e.g.:
f = lambda x: x*5 + 1 f(10)2.7 import
Python has a lot of built-in packages you can use, or you can download and install more packages from the internet.
Using such packages is easy:
import antigravity import statistics statistics.mean([3,5,7,9]) # 6 # you can also import just single functions from a package from math import log log (2.71 * 2.72) # 1.99758051519951562.8 Exceptions
def to_int_safe (x):
try:
z = int (x)
except:
print ("We cannot convert everything like this!")
z = -1
return z
to_int_safe("5.5")
# We cannot convert everything like this!
# -1
2.9 File Handling
- open for reading
with open("filename", "r") as f: #r ==> open for reading
for line in f:
print (line.strip())
- open for writing
my_list = ["a", "bc", "de"]
with open("fienae.txt", "wb") as f: #r ==> open for writing
for element in my_list:
f.write (element + "n")



