this file is derived from exercise 0
#1. Variables and data types
1.1 operator
Some 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 |
1.2 basic functions
x = "Hello"
y = "World"
print (x + y)
- type(variable)
- str(variable)
- len(variable)
2. Data Structures
2.1 List
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()
2.2 Tuple
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)
- 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.4 Dictionary
Python’s built-in mapping type. They map keys, which can be any immutable type, to values, which can be any type.
presidents_inauguration = {}
presidents_inauguration ['Trump'] = 2017
presidents_inauguration['Obama'] = 2009
presidents_inauguration['Bush'] = 2001
print(presidents_inauguration)
d = {'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
print (presidents_inauguration.keys())
print (presidents_inauguration.values())
2.5 Control Statements
control flow in Python noticeable does not use ANY (,),[,],{,},… Instead indendation determines what belongs to block of commands
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!')
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)
for index, name in enumerate (first_names):
print("Name "+ str(index) + ": " + name)
x = 1
while x < 100:
x = x * 2
print(x)
2.6 Functions
def print_all_names(names):
for x in names:
print (x)
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])
from math import log
log (2.71 * 2.72)
2.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")
2.9 File Handling
with open("filename", "r") as f:
for line in f:
print (line.strip())
my_list = ["a", "bc", "de"]
with open("fienae.txt", "wb") as f:
for element in my_list:
f.write (element + "\n")
|