python basics
python basics
Ex code. SYNTAX:
firstName = “Elmarr” #set variable value print ( list [startIndex : endIndex] )
print ( list [ : endIndex] )
print (firstname) #print the value print ( list [ startIndex : ] )
Ex code.
Assigning List ITEMS
names = [“elmarr”, “reymond”, dela_cruz”] You can assign a list item by using an INDEX and an
Assignment Operator “=”.
print ( names )
SYNTAX:
Reading Lists ITEMS list [index] = value
You can read a list by printing one of the items
Ex code.
inside it by using an INDEX. names = [“elmarr”, “reymond”, “arroyo”, “dela_cruz”, “momon”]
Ex code.
SYNTAX: SYNTAX:
list.count(value) list.pop()
Ex code. list.pop(index)
names = [“elmarr”, “reymond”, “arroyo”, “dela_cruz”, “elmarr”] Ex code.
names = [“elmarr”, “reymond”, “arroyo”, “dela_cruz”]
print ( names.count(“elmarr”) )
names.pop ()
List ADD ITEMS by APPEND() print ( names)
append() adds an item at the END OF THE LIST.
Ex code.
SYNTAX: names = [“elmarr”, “reymond”, “arroyo”, “dela_cruz”]
list.append(value)
names.pop (3)
Ex code. print ( names )
names = [“elmarr”, “reymond”, “arroyo”]
names.clear()
print ( names )
Name: Elmar Reymond A. Delac Cruz Page # 4
Contact No.: 0966-560-8740 Created by: Elmar
print ( names )
Ex code.
names = [“elmarr”, “reymond”, “dela_cruz”]
places = [“Makati”, “Marikina”, “Manila”]
names.append(places)
print ( names )
Name: Elmar Reymond A. Delac Cruz Page # 5
Contact No.: 0966-560-8740 Created by: Elmar
9. Sets SYNTAX:
A Collection of variables that is Partially Writable set.update (list)
and it’s unordered and unindexed. Ex code. Ex code.
evenNumbers = {2, 4, 6, 8, 10} evenNumbers = {2, 4, 6, 8, 10}
SYNTAX: evenNumbers.update ([12, 14, 16]) extend = [12, 14, 16]
evenNumbers.update (extend)
Identifier = {value0, value1, value2}
print ( evenNumbers )
print ( evenNumbers )
Reading WHOLE Sets
You can read a set by printing the whole set.
Set DELETING ITEMS by REMOVE()
SYNTAX: remove() deletes an item based on their value.
print (set)
SYNTAX:
Ex code. set.remove(value)
evenNumbers = {2, 4, 6, 8, 10}
PS: If the value doesn’t exist in the set it will be
print ( evenNumbers )
counted as an error.
Reading Set ITEMS Ex code.
It is not possible to read certain item in a set unless evenNumbers = {2, 4, 6, 8, 10}
you cast into a List or Tuple. Hence Sets are evenNumbers.remove (6)
unindexed and unordered. print ( evenNumbers )
SYNTAX:
set.clear() INTERSECTION Set
Ex code. intersection() returns a set containing the values
evenNumbers = {2, 4, 6, 8, 10} that exists both on the two sets.
evenNumbers.clear()
SYNTAX:
print ( evenNumbers )
setOne = {value0, value1, value2}
setTwo = {value1, value2}
Copying a Set setThree = setOne.intersection(setTwo)
copy() copies the whole set which can be assigned to
Ex code.
a new set. Num1 = {1, 2, 3}
Num2 = {2, 3}
SYNTAX: numbers = Num2.intersection(Num1)
setOne = {value0, value1, value2}
print (numbers)
setTwo = setOne.copy()
Ex code.
setOne = {2, 4, 6, 8, 10}
SYMETRIC DIFFRENCE Set
setTwo = setOne.copy() symmetric_difference() returns a set containing all
values that exists EXCLUSIVELY on each set.
print (setOne)
SYNTAX:
UNION Set setOne = {1, 2, 3, 4, 5}
union() returns a set containing all the values of the setTwo = {3, 4, 5, 6, 7}
two sets. setThree = setOne.intersection(setTwo)
Ex code.
SYNTAX: Num1 = {1, 2, 3, 4}
setOne = {value0, value1, value2} Num2 = {2, 3, 4, 5}
numbers = Num2.symmetric_difference (Num1)
setTwo = {value3, value4, value5}
setThree = setOne.union(setTwo) print (numbers)
Ex code.
evenNum = {2, 4, 6, 8, 10}
oddNum = {1, 3, 5, 7, 9} DISJOINT Set
numbers = evenNum.union(oddNum) isdisjoint() returns a Boolean whether two sets have
an intersection or not.
print (setOne)
Ex code. SYNTAX:
Num1 = {1, 2, 3} setOne.isdisjoint(setTwo)
Num2 = {1, 4, 5}
Ex code. Ex code.
numbers = Num1.union(Num2)
evenNum = {2, 4, 6, 8, 10} evenNum = {2, 4, 6, 8, 10}
oddNum = {1, 3, 5, 7, 9} oddNum = {1, 3, 5, 7, 9, 10}
print (numbers)
print (evenNum.isdisjoint(oddNum)) print (evenNum.isdisjoint(oddNum))
Name: Elmar Reymond A. Delac Cruz Page # 7
Contact No.: 0966-560-8740 Created by: Elmar
SYNTAX: SYNTAX:
setOne.issubset(setTwo) print(dictionary[key])
Ex code. Ex code. Ex code.
numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} studentOne = {
evenNum = {2, 4, 6, 8, 10} evenNum = {2, 4, 6, 8, 10, 11} “name” : “elmarr”,
“course” : “BSIT”,
print (evenNum.issubset(numbers)) print (evenNum.issubset(numbers)) “age” : 15
}
studentTwo = {
SUPERSET “name” : “reymondr”,
issuperset() returns a boolean whether the right set “course” : “BSCs”,
“age” : 20
is contained in the left set.
}
SYNTAX:
CASTING SETS
You can cast Sets to Tuples or List and VICEVERSA in print(dictionary.get(key))
the same way you cast other variables. Ex code.
studentOne = {
Ex code. Ex code.
numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] “name” : “elmarr”,
numbers = list(numbers) numbers = set(numbers) “course” : “BSIT”,
“age” : 15
print (numbers) print (numbers) }
studentTwo = {
Ex code. “name” : “reymondr”,
numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} “course” : “BSCs”,
numbers = tuple(numbers) “age” : 20
}
print (numbers)
print (studenOne)
print (studentTwo)
Name: Elmar Reymond A. Delac Cruz Page # 8
Contact No.: 0966-560-8740 Created by: Elmar
SYNTAX: Syntax:
len(dictionary) dictionary.popitem()
Ex code. Ex code.
studentOne = { studentOne = {
“name” : “elmarr”, “name” : “elmarr”,
“course” : “BSIT”, “course” : “BSIT”,
“age” : 15 “age” : 15
} }
print (studenOne )
Dictionary ADD ITEMS
You can add items on a dictionary just by assigning a
non-existent key value in the dictionary. Clearing a Dictionary
clear() deletes all the items in a Dictionary.
SYNTAX:
dictionary[key] = value SYNTAX:
Ex code. dictionary.clear()
studentOne = {
“name” : “elmarr”, Ex code.
“course” : “BSIT”, studentOne = {
“name” : “elmarr”,
“age” : 15
} “course” : “BSIT”,
“age” : 15
}
studentOne[“gender”] = “male”
StudentOne.clear()
print (studenOne )
print (studenOne )
studentTwo.copy()
print (studentTwo )
Name: Elmar Reymond A. Delac Cruz Page # 9
Contact No.: 0966-560-8740 Created by: Elmar
print (studentOne.values() )
print ( studentInfo) print ( studentInfo.get(“sport”))
List of Dctionaries
You can put a Dictionary inside a List, you can also Ex code.
sports = {
use indexing combined with get().
“sport1” : “Taekwondo”
“sport2” : “badminton”
SYNTAX: “sport3” : “E-sport”
dictionary1 = { }
key1 : value1, studentInfo = {
key2 : value2, “name” : “elmarr”,
key3 : value3 “course” : “BSIT”,
} “age” : 15,
dictionary2 = { “sports” : sports
key4 : value4, }
key5 : value5,
key6 : value6 print (
} studentInfo.get(“sport”).get(“sport1”) )
Identifier = [ dictionary1, dictionary2]
Ex code. Ex code.
studentOne = { studentOne = {
“name” : “elmarr”, “name” : “elmarr”,
“course” : “BSIT”, “course” : “BSIT”,
“age” : 15 “age” : 15
} }
studentTwo = { studentTwo = {
“name” : “reymond”, “name” : “reymond”,
“course” : “BSCs”, “course” : “BSCs”,
“age” : 20 “age” : 20
} }
Ex. Code:
age = int( input (“Enter your age: “))
if age >= 18:
print(“You’re can vote”)
else:
print (“You’re too young”)
Name: Elmar Reymond A. Delac Cruz Page # 11
Contact No.: 0966-560-8740 Created by: Elmar
Ex. Code:
#finite loop
age = 12
Ex. Code:
#this is infinite loop
While True:
print (“Hello World!)
break #try to remove the break keyword
Name: Elmar Reymond A. Delac Cruz Page # 13
Contact No.: 0966-560-8740 Created by: Elmar
Ex. Code:
fruits = [“banana”, “apple”, “orange”] Using RANGE in NESTED FOR Loop
for x in fruits: Ex. Code: Ex. Code:
print (x) vertical asterisk horizontal asterisk
break
for e in range(5): for e in range(5):
for y in range(5): for y in range(5):
CONDITIONS in FOR Loop print(“*”) print(“*”,end=””)
You can use any Conditional Statement inside a FOR
Loop.
Ex. Code: Ex. Code:
5 row & column asterisk
fruits = [“banana”, “apple”, “orange”, “avocado”, “ grapes”]
for x in fruits:
for e in range(5):
print (x)
for y in range(5):
if x == “avocado”:
print(“*”,end=””)
break
print()
Ex. Code:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Reading Multi – Dimensional Collections
for num in numberss: Using Nested For Loop
if num % 2 == 0:
Ex. Code: Ex. Code:
print (“Even numbe: ” + str(num))
petNames = [ petNames = [
[“Dog”,”Sam”], [“Dog”,”Sam”],
[“Dog”,”Staby”], [“Dog”,”Staby”],
[“Dog”, “Princess”], [“Dog”, “Princess”],
[“Cat”,”Cloie”], [“Cat”,”Cloie”],
[“Cat”, “SnowBell”] [“Cat”, “SnowBell”]
] ]
for listPet in petName s; for listPet in petName s;
for x in listPet; for x in listPet;
print(i) print(i)
print()