Monthly Archives: October 2021

19. Lambda

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. Syntax lambda arguments : expression The expression is executed and the result is returned: Example Add 10 to argument a, and return the result:x = lambda a : a + 10print(x(5)) Output:15 Lambda functions can take any number of… Read More »

18. Functions

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. Creating a Function In Python a function is defined using the def keyword: Example def my_function():  print(“Hello from a function”) Calling a Function To call a function,… Read More »

17. For Loops

Python For Loops A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set… Read More »

16. While Loops

Python Loops Python has two primitive loop commands: while loops for loops The while Loop With the while loop we can execute a set of statements as long as a condition is true. Example Print i as long as i is less than 6:i = 1while i < 6:  print(i)  i += 1 Output:12345 Note: remember to increment i, or else the loop will continue forever.… Read More »

15. If … Else

Python Conditions and If statements Python supports the usual logical conditions from mathematics: Equals: a == b Not Equals: a != b Less than: a < b Less than or equal to: a <= b Greater than: a > b Greater than or equal to: a >= b These conditions can be used in several ways, most commonly in “if statements”… Read More »

14.8 Dictionary Methods

Dictionary Methods Example: Create a dictionary with 3 keys, all with the value 0:x = (‘key1’, ‘key2’, ‘key3’)y = 0thisdict = dict.fromkeys(x, y)print(thisdict) Output:[‘key1’: 0, ‘key2’: 0, ‘key3’: 0] Python has a set of built-in methods that you can use on dictionaries. Method Syntax Description clear() dictionary.clear() Removes all the elements from the dictionary copy() dictionary.copy() Returns a… Read More »

14.7 Nested Dictionaries

Nested Dictionaries A dictionary can contain dictionaries, this is called nested dictionaries. Example Create a dictionary that contain three dictionaries:myfamily = {  “child1” : {    “name” : “Emil”,    “year” : 2004  },  “child2” : {    “name” : “Tobias”,    “year” : 2007  },  “child3” : {    “name” : “Linus”,    “year” : 2011  }} Output:{‘child1’: {‘name’: ‘Emil’, ‘year’: 2004}, ‘child2’: {‘name’: ‘Tobias’, ‘year’: 2007}, ‘child3’: {‘name’: ‘Linus’, ‘year’: 2011}} Or, if you want to add three dictionaries into a new dictionary:… Read More »

14.6 Copy Dictionaries

Copy a Dictionary You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2. There are ways to make a copy, one way is to use the built-in Dictionary method copy(). Example Make a copy of a dictionary with the copy() method:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}mydict = thisdict.copy()print(mydict) Output:{‘brand’: ‘Ford’,… Read More »

14.5 Loop Dictionaries

Loop Through a Dictionary You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well. Example Print all key names in the dictionary, one by one:for x in thisdict:  print(x) Output:brandmodelyear Example Print all values in the dictionary, one by one:for x in thisdict:  print(thisdict[x]) Output:FordMustang1964 Example… Read More »

14.4 Remove Dictionary Items

Removing Items There are several methods to remove items from a dictionary: Example The pop() method removes the item with the specified key name:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict.pop(“model”)print(thisdict) Output:{‘brand’: ‘Ford’, ‘year’: 1964} Example The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict.popitem()print(thisdict) Output:{‘brand’: ‘Ford’, ‘model’: ‘Mustang’} Example The del keyword removes the item with… Read More »