True and Falseprint("Test: 0x%08x %d %s" % (27, 12, "test"))print("Test: 0x{hex:08x} {dec:5d} {name:12s}".format(hex=27, dec=12, name="test"))str(5), int("12") and float("13.4")if "a" in "Hallo": - Performs the block when the letter "a" exists in the word "Hallo"if cnt < 5: - Performs the block when the variable cnt is less than 5while "Caro" in teachers: - Performs the block as long as the string "Caro" exists in the variable studentsfor cnt in range(0,10): - Performs the block with the values 0 to 9time.sleep(7.2) will sleep for 7.2 secondsimport datetime
# Will print "Current time is 2020-03-01 19:46:00"
print("Current time is %s" % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
def functionname(para1, para2, para3="hallo"):, where para3 is an optional parameter with the default value "Hello"params = [ 27, -12, 'huhu'] functionname(*params)This code fills the content of the list
params into the parameters of the function functionnameparams = { para1: 27, para3: 'huhu', para2: -12 }
functionname(**params)
This code fills the content of the dictionary params into the parameters of the function functionnamedef functionname(*para): writes all parameters in a tuple named paradef functionname(**para): writes all parameters in a dictionary named parawith open("myfile.txt", "r") as myfile:for line in myfile: you can read a file line by line. A line break must be removed with print(line.strip("\n"))myfile.write("mytext\n"). But now we must open the file with open("myfile.txt", "w") or open("myfile.txt", "a")[1, 2, 3, 4].pop() - Removes and returns the last element[1, 2, 3, 4].pop(0) - Removes and returns the first element[1, 2, 3, 4].append(6) - Appends the element to the end of the listlist = [1, 2, 3, 4]; del list[2] - Deletes the third element from the list5 in [1, 2, 3, 4] - The operator in returns True when the given value exists in the listlen([1, 2, 3, 4]) - Returns the length of the listcontact = ("Mustermann", "Max", 123456)name, surname, phone = contact - convert tuple to variables in one line{"Musterman":222332, "Mueller":45542554}for key in mydict: - receive key in for-loopfor name, phone in mydict.items(): - receive key and value in for-loopimport queue q = queue.PriorityQueue() q.put( (7, 'hallo') )
q.get() - returns and removes the key-value tuple with the lowest 'priority'for prio, value in q.queue: - iterates over the priority queue (the smallest value first)list[START:END] - Returns a list which starts at START and ends before END:mylist = ["a", "b", "c", "d", "e", "f", "g"] print(mylist) # prints ["a", "b", "c", "d", "e", "f", "g"] print(mylist[:]) # prints ["a", "b", "c", "d", "e", "f", "g"] print(mylist[2:]) # prints ["c", "d", "e", "f", "g"] print(mylist[2:4]) # prints ["c", "d", "e"] print(mylist[1:-1]) # prints ["b", "c", "d", "e", "f"]
list = [CODE for VAR in LIST] - Performs the code CODE with variable VAR for each entry in list LIST:# list of indexes xs = [x / 10 for x in range(-100,101)] # list of values ys = [x*x for x in xs] # index where the value is two are_two = [index for index, value in enumerate(ys) if value == 2]
list = [CODE1 : CODE2 for VAR in LIST] - Performs the code CODE1 and CODE2 with variable VAR for each entry in list LIST:# creates {0: 10, 1: 11, 2: 12, 3: 13}
mydict = {i : i+10 for i in range(4)}
def __init__(self, para): declares the constructordef __str__(self): returns what shall be printed when the object is converted into string. Best pattern is to return return "ClassName(surname=" + self.surname + ")"def __repr__(): similar as __str__ but returns a dictionary with the valuesdef __len__(): returns the number when the object is passed to the len() methodtype() returns the classname of the objectisinstance(object, class) returns True when the object is of type class or derived from itself.__surnameyield, returns the result and supsends. When the next result is read, the method resumes. When no futher result is accessed, also no further result is generated.
def FunctionWithYield(): sum = 0 for counter in range(7,99): sum = sum + counter yield sum # will print "7, 15, 24..." but not over 100 for result in FunctionWithYield(): if result > 100: break print(result)