01-Python1
01-Python1
"""
➔#This
Comments for a paragraph useThis
is a comment """ is a comment
#written in written in
#more than just one line more than just one line
print("Hello, World!") """
print("Hello, World!")
Variables
➔ Many values can be assigned to multiple variables
x, y, z = "Orange", "Banana", "Cherry"
➔ Convert from one type to another with the int(), float(), and
complex() methods
String
➔ are surrounded by either single quotation marks, or double
quotation marks.
Ex: 'hello' is the same as "hello"
➔ Convert from one type to another with the int(),
float(), and complex() methods.
➔ Assign a multiline string to a variable by using three
quotes.
longer = " " " This string has
multiple lines " " "
+
String
Concatenation operator >>> str1+ str2
➔ String operators >>> “Hello world”
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another,
specified set
intersection_update Removes the items in this set that are not present in other, specified
() set(s)
Sets Methods
Method Description
isdisjoint() Returns whether two sets have a intersection or not
symmetric_difference_update inserts the symmetric differences from this set and another
()
union() Return a set containing the union of sets
update() Update the set with the union of this set and others
➢ Lists
Data Structures
➢ Tuples
➢ Sets
➢ Dictionary
○ Dictionary Initialization
○ Operations on Dictionary
○ Dictionary methods
Dictionaries
➔ Are used to store data values in key : value pairs.
➔ A dictionary is a collection which is ordered*, changeable
and do not allow duplicates.
➔ written with curly brackets, and have keys and values:
Dictionaries
Initialization
➔ Ex1: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Dictionaries
Ex2:
Initialization
# Duplicate values will overwrite existing
values:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
Operations on
➔ Access Items
Dictionaries
➔ Change Items
➔ Add Items
➔ Remove Items
➔ Loop Items
➔ Copy Dictionaries
Operations on
Dictionaries
➔ Access Items: access the items of a dictionary by
referring to its key name, inside square brackets:
➔ Ex: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
Operations on
Dictionaries
➔ Loop dictionaries: the return value are the keys of the
dictionary, but there are methods to return the values as
well.
o Ex1: Print all key names in the dictionary, one by one:
for x in thisdict:
print(x)
for x in thisdict:
print(thisdict[x])
Operations on
Dictionaries
➔ Loop dictionaries: the return value are the keys of the
dictionary, but there are methods to return the values as
well.
o Ex3: values() method to return values of a dictionary:
for x in thisdict.values():
print(x)
o Ex4: use the keys() method to return the keys of a
dictionary:
for x in thisdict.keys():
print(x)
o Ex5: items() method to through both keys and values
for x, y in thisdict.items():
print(x, y)