Python E1 Notes
Python E1 Notes
a = "A"
b = "B"
c = "C"
d = " "
lst = [a, b, c, d]
lst.reverse()
del list_1[0]
del list_2[0]
del list_1[0]
del list_2
print(list_3)
Check
['B', 'C']
del list_1[0]
del list_2[:]
print(list_3)
Check
[]
6. nums=[1,2,3]
print(nums[-1:-2]) -----------------------> []
7. def hi():
print("hi")
hi(5)
Check
An exception will be thrown (the TypeError exception to be more precise) - the hi()
function doesn't take any arguments
8. KEYWORD PASSING
introduction(surname="Skywalker", first_name="Luke")
print(list_3)-----------------> ['C']
8.
9. print(None + 2)
10.
def strange_function(n):
if(n % 2 == 0):
return True
print(strange_function(1)) ---------------------------------------------> None
# Example 1
def wishes():
print("My Wishes")
return "Happy Birthday"
# Example 2
def wishes():
print("My Wishes")
return "Happy Birthday"
print(wishes())
# outputs: My Wishes
# Happy Birthday
12.
def message(number):
print("Enter a number:", number)
number = 1234
message(1)
print(number)
OUTPUT
Enter a number: 1
1234
value = None
if value is None:
print("Sorry, you don't carry any value")
14.
The data type we want to tell you about now is a tuple. A tuple is an immutable
sequence type
If you want to create a one-element tuple, you have to take into consideration the
fact that, due to syntax reasons (a tuple has to be distinguishable from an
ordinary, single value), you must end the value with a comma:
one_element_tuple_1 = (1, )
16.
The similarities may be misleading - don't try to modify a tuple's contents! It's
not a list!
All of these instructions (except the topmost one) will cause a runtime error:
my_tuple.append(10000)
del my_tuple[0]
my_tuple[1] = -10
17. the + operator can join tuples together (we've shown you this already)
the * operator can multiply tuples, just like lists;
EXAMPLE
my_tuple = (1, 10, 100)
OUTPUT
t1=(1, 10, 100, 1000, 10000)
t2=(1, 10, 100, 1, 10, 100, 1, 10, 100)
18 .
19.
dict.items() ----> gives the elements of the dict that is both key value pair
EXAMPLE
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
del dictionary['dog']
pol_eng_dictionary = {
"zamek": "castle",
"woda": "water",
"gleba": "soil"
}
copy_dictionary = pol_eng_dictionary.copy()
22. You can use the del keyword to remove a specific item, or delete a dictionary.
To remove all the dictionary's items, you need to use the clear() method:
EXAMPLE
pol_eng_dictionary = {
"zamek": "castle",
"woda": "water",
"gleba": "soil"
}
print(len(pol_eng_dictionary)) # outputs: 3
del pol_eng_dictionary["zamek"] # remove an item
print(len(pol_eng_dictionary)) # outputs: 2
23. Write a program that will "glue" the two dictionaries (d1 and d2) together and
create a new one (d3).
print(d3)
Check
Sample solution:
d1 = {'Adam Smith': 'A', 'Judy Paxton': 'B+'}
d2 = {'Mary Louis': 'A', 'Patrick White': 'C'}
d3 = {}
print(d3)
print(copy_my_dictionary)
Check
The program will print {'A': 1, 'B': 2} to the screen.
25.
Write a program that will convert the colors tuple to a dictionary.
colors = (("green", "#008000"), ("blue", "#0000FF"))
print(colors_dictionary)
Check
Sample solution:
colors = (("green", "#008000"), ("blue", "#0000FF"))
colors_dictionary = dict(colors)
print(colors_dictionary)