this file is derived from exercise 0
#1. Variables and data types
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 |
x = "Hello"
y = "World"
print (x + y)
# "HelloWorld"
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]]
Tuples are just the same as lists, but are immutable.
t = (1,2,3)
t1 = ((1,2),(1,3),3)
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}
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]
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)
#{'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
d = {
'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
print (presidents_inauguration.keys())
#dict_keys(['Trump', 'Obama', 'Bush'])
print (presidents_inauguration.values())
#dict_values([2017, 2009, 2001])
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 + "!")
for x in range(10,25,5):
print (x)
#10
#15
#20
# 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 loops functions very similar to many popular languages:
x = 1
while x < 100:
x = x * 2
print(x)
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)
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.9975805151995156
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
with open("filename", "r") as f: #r ==> open for reading
for line in f:
print (line.strip())
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")