IS231: Web Technology Python: By: Neamat El Tazi
IS231: Web Technology Python: By: Neamat El Tazi
Python
- W3Schools - Python
List items are indexed, the first item has index [0], the
second item has index [1] etc.
Ordered
When we say that lists are ordered, it means that
the items have a defined order, and that order will
not change.
If you add new items to a list, the new items will
be placed at the end of the list.
If you do not specify the index, the pop() method removes the last
item
List Functions
The del keyword can also delete the list completely.
thislist = ["apple", "banana", "cherry"]
del thislist
thislist =
["apple", "banana", "cherry", "orange", "kiwi",
"mango"]
print(newlist)
23 Web Technology Neamat El Tazi
List Comprehension Syntax
newlist =
[expression for item in iterable if conditio
n == True]
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
thistuple =
tuple(("apple", "banana", "cherry")) # note
the double round-brackets
print(thistuple)
Negative Indexing:
Negative indexing means start from the end.
The number of variables must match the number of values in the tuple, if not, you must
use an asterix to collect the remaining values as a list.
Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of
where it was found
thisset =
{"apple", "banana", "cherry", "apple"}
print(thisset)
37 Web Technology Neamat El Tazi
The set() Constructor
It is also possible to use the set() constructor
to make a set.
thisset =
set(("apple", "banana", "cherry")) # note
the double round-brackets
print(thisset)
for x in thisset:
print(x)
print("banana" in thisset)
thisset.add("orange")
print(thisset)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(len(thisdict))
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
x = thisdict.get("model")
x = thisdict.keys()
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) #before the change
car["color"] = "white"
print(x) #after the change
53 Web Technology Neamat El Tazi
Get Values
The values() method will return a list of all the values in
the dictionary.
x = thisdict.values()
x = thisdict.items()
#will return:
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year',
2020)])
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
Update Dictionary
The update() method will update the dictionary with the items from the
given argument.
The argument must be a dictionary, or an iterable object with
key:value pairs
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"color": "red"})
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
a = 33
b = 200
if b > a:
print("b is greater than a")
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
a = 33
b = 200
if b > a:
pass
def my_function():
print("Hello from a function")
my_function("Emil", "Refsnes")
If you try to call the function with 1 or 3 arguments, you will get an error
def my_function(*kids):
print("The youngest child is " + kids[2])
def my_function(**kid):
print("His last name is " + kid["lname"])
Syntax
lambda arguments : expression