Phyton Basics
Start
Create by King Jasser Andaya
Lesson Two
Lesson Three
Lesson One
Lesson Four
Lesson One: Basic Syntax
Python Syntax
Variables
Data Types
Boolean
Numbers
Python Basics
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
- web development (server-side),
- software development,
- mathematics,
- system scripting.
Previous
Next
What can Python Do?
- Python can be used on a server to create web applications.
- Python can be used alongside software to create workflows.
- Python can connect to database systems. It can also read and modify files.
- Python can be used to handle big data and perform complex mathematics.
- Python can be used for rapid prototyping, or for production-ready software development.
Previous
Next
Fill in the Blanks
Why Python?
- Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
- Python has a simple syntax similar to the English language.
- Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
- Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
- Python can be treated in a procedural way, an object-oriented way or a functional way.
Previous
Next
Python Question
Previous
Next
Python Syntax
print("Hello, World!")Try it >>> Note: Python has the most simple and almost error free syntax
Variable
Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
x = 5
y = "John"
Variables do not need to be declared with any particular type, and can even change type after they have been set. x = 4 # x is of type integer x = "Sally" # x is now of type string print(x)
Previous
Next
Variable Names
- A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
- Rules for Python variables:
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
- A variable name cannot be any of the Python keywords.
Previous
Next
Previous
Python Question
Previous
Next
Variable
x = "Python " y = "is "
z = "awesome"
print(x, y, z)Try this >>>Note: Variable can store anything that you can use later on your Program
Previous
Next
Variable
x = 214594 y = 137960 z = 596070870 print(x + y + z)Try this >>>Note: Variables can be used to remember different data
Data Types
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
Previous
Next
Previous
Python Question
Previous
Next
Data Types
x = "12"y = 24 print( x + y ) Try this >>>Can you add the 2 Variables?Note: Always remember, you cannot interact different Data Type
Booleans
In programming you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two answers, True or False.
When you compare two values, the expression is evaluated and Python returns the Boolean answer:print(10 > 9)
print(10 == 9)
print(10 < 9)
Previous
Next
Previous
Python Question
Previous
Next
Booleans
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a") Try this >>>
Numbers
There are three numeric types in Python:
- Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
- Float, or "floating point number" is a number, positive or negative, containing one or more decimals.Float can also be scientific numbers with an "e" to indicate the power of 10.
- Complex numbers are written with a "j" as the imaginary part
Previous
Next
Previous
Python Question
Previous
Next
Numbers
x = 1 # int
y = 2.8 # float
z = 1j # complex #convert from int to float:
a = float(x) #convert from float to int:
b = int(y) #convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c) Try this >>>Note: Red Colored Texts are Comments, No need to include it
Previous
Code This
Project Time: Age Checker
1. Create a Variable with any Name and Integer Value 2. Write this Code after: if age >= 18:
print("You are an adult.")
else:
print("You are a minor.") 3. Run your Code 4. Explain your Code
Submit
Previous
Project Time: Age Checker
Finish your Project and you will be given access by your Teacher.
Enter the password
Strings
Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". You can display a string literal with the print() function. Python has a set of built-in methods that you can use on strings. The upper() method returns the string in upper case.The lower() method returns the string in lower case.The strip() method removes any whitespace from the beginning or the end.The replace() method replaces a string with another string.The split() method splits the string into substrings if it finds instances of the separator.
Previous
Next
Previous
Next
Strings
a = " Hello, World! "print(a.upper()) print(a.lower())print(a.strip())print(a.replace("H", "J"))print(a.split(",")) Try this >>>
Previous
Python Question
Slicing Strings
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string. Note: The first character has index 0.By leaving out the start index, the range will start at the first character. By leaving out the end index, the range will go to the end:
Previous
Next
Previous
Next
Slicing Strings
b = "Hello, World!"
print(b[2:5])#Get characters from position 2 to 5 print(b[:5])#Get the characters from the start to position 5print(b[2:])#Get the characters from position 2, and all the way to the end Try this >>>
Combine Strings
To concatenate, or combine, two strings you can use the + operator.Merge variable a with variable b into variable c:a = "Hello"
b = "World"
c = a + b
print(c)To add a space between them, add a " "c = a + " " + b print(c)
Previous
Next
Combine Strings
As we learned in the Python Variables chapter, we cannot combine strings and numbers but we can combine strings and numbers by using f-strings or the format() method! F-String was introduced in Python 3.6, and is now the preferred way of formatting strings. To specify a string as an f-string, simply put an f in front of the string literal, and add curly brackets {} as placeholders for variables and other operations.age = 36 txt = f"My name is John, I am {age}" print(txt)
Previous
Next
Previous
Next
Combine Strings
g = "Hello" h = "King" print(g + " " + h)#Combine the String i = 31print(f"I am { i } years old!") #Combine the String and Integer Try this >>>
Previous
Python Question
Lists
Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets:thislist = ["apple", "banana", "cherry"]
print(thislist)To determine how many items a list has, use the len() functionthislist = ["apple", "banana", "cherry"]
print(len(thislist))
Previous
Next
Access Lists
List items are indexed and you can access them by referring to the index number.The first item has index 0.thislist = ["apple", "banana", "cherry"]
print(thislist[1])You can specify a range of indexes by specifying where to start and where to end the range.thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
Previous
Next
Previous
Next
Lists
thisList = ["Apple","Banana","Cherry"] print(thisList)print(len(thisList))print(thisList[2])print(thisList[0])print(thisList[1]) print(thisList[0:2]) Try this >>>
Change Lists
To change the value of a specific item, refer to the index number.thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values.thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
Previous
Next
Add Item on Lists
To add an item to the end of the list, use the append() method:thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)To insert a list item at a specified index, use the insert() method.thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)To append elements from another list to the current list, use the extend() method.
Previous
Next
Remove Item on Lists
he remove() method removes the specified item.thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)The pop() method removes the specified index.thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist) The clear() method empties the list
Previous
Next
Previous
Python Question
Previous
Next
Lists
thisList = ["Apple","Banana","Cherry"] Challenge:* Add "Durian" on the List* Add "Atis" in the Middle of "Apple" and "Banana"* Change "Cherry" to "Carrot"* Remove 1 Item Try this >>>
Tuples
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable. thistuple = ("apple", "banana", "cherry")
print(thistuple)
Previous
Next
Access Tuples
You can access tuple items by referring to the index number, inside square brackets:thistuple = ("apple", "banana", "cherry")
print(thistuple[1]) The first item has index 0.You can specify a range of indexes by specifying where to start and where to end the range.thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
Previous
Next
Change Tuples
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created.
But there are some workarounds. You can convert the tuple into a list, change the list, and convert the list back into a tuple.x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Previous
Next
Previous
Python Question
Previous
Next
Lists
thisTuple = ["Honda","Toyota","Suzuki"] Challenge: * Print 1st Item* Add "Isuzu" * Change "Toyota" to "Kia" Try this >>>
Sets
Sets are used to store multiple items in a single variable.
Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.
A set is a collection which is unordered, unchangeable*, and unindexed. thisset = {"apple", "banana", "cherry"}
print(thisset)Sets are unordered, so you cannot be sure in which order the items will appear.
Previous
Next
Access Sets
You cannot access items in a set by referring to an index or a key.Check if "banana" is present in the set: thisset = {"apple", "banana", "cherry"} print("banana" in thisset)Check if "banana" is NOT present in the set:thisset = {"apple", "banana", "cherry"}
print("banana" not in thisset)
Previous
Next
Add Sets
Once a set is created, you cannot change its items, but you can add new items.Add an item to a set, using the add() method:thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)o add items from another set into the current set, use the update() method. thisset = {"apple", "banana", "cherry"} tropical = {"pineapple", "mango", "papaya"} thisset.update(tropical) print(thisset)
Previous
Next
Remove Sets
To remove an item in a set, use the remove(), or the discard() method.thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)If the item to remove does not exist, remove() will raise an error.Remove "banana" by using the discard() methodthisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
Previous
Next
Previous
Python Question
Previous
Next
Sets
thisSet = {"Honda","Toyota","Suzuki"} Challenge: * Print an Item* Add "Hyundai"* Remove "Honda" Try this >>>
Dictionary
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.Dictionary items are presented in key:value pairs, and can be referred to by using the key name.thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} print(thisdict)print(thisdict["brand")
Previous
Next
Previous
Python Question
Access Dictionary
You can access the items of a dictionary by referring to its key name, inside square brackets:There is also a method called get() that will give you the same resultthisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} x = thisdict["model"]y = thisdict.get("model")print (x)print (y)
Previous
Next
Change Item in Dictionary
You can change the value of a specific item by referring to its key name thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict["year"] = 2018 print(thisdict)The update() method will update the dictionary with the items from the given argument.thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict.update({"year": 2020}) print(thisdict)
Previous
Next
Add Item in Dictionary
Adding an item to the dictionary is done by using a new index key and assigning a value to it. thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict["color"] = "red"
print(thisdict)The update() method will update the dictionary with the items from a given argument. If the item does not exist, the item will be added. thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict.update({"color": "red"}) print(thisdict)
Previous
Next
Previous
Python Question
Remove Item in Dictionary
There are several methods to remove items from a dictionary. The pop() method removes the item with the specified key nameThe popitem() method removes the last inserted itemThe del keyword removes the item with the specified key name. The del keyword can also delete the dictionary completely.thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict.pop("model")thisdict.popitem()del thisdict["model"] print(thisdict)
Previous
Next
Previous
Next
Dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
} Challenge: * Print "model" * Add "Type" : "Sedan" * Remove "brand" Try this >>>
Previous
Code this
Project Time: Contact List
1.Create a Contact List Program. The Program can Add a Contact 2. Print All Contacts on the List3. Remove a Contact4. Print All Contacts again 5. You can use List, Tuples, Set or Dictionary (Create the Program using 2 of these) 6. Run your Code 7. Explain your Code to your Teacher
Previous
Submit
Project Time: Age Checker
Finish your Project and you will be given access by your Teacher.
Enter the password
Math Operators
Operators are used to perform operations on variables and values. Python divides the operators in the following groups:
- Arithmetic / Math operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Previous
Next
Math Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Previous
Next
Previous
Next
Math Operators
x = 20 y = 30 print( x + y ) Challenge: *Change the 2 Variables to different values *Use - , * , / to perform other basic artihmetic operations Try this >>>
Previous
Python Question
Comparison Operators
Comparison operators are used to compare two values:
Previous
Next
Previous
Next
Comparison Operators
x = 20 y = 30 print( x < y ) Challenge: *Compare the two variables *Use all of the Comparison Operators *Observe the Answer Try this >>>
Previous
Python Question
Logical Operators
Logical operators are used to combine conditional statements.
Previous
Next
Previous
Next
Logical Operators
x = 20 y = 25 z = 30 print( x < y and y > z) print( x < y and y < z) Challenge: *Compare the two variables *Use all of the Logical Operators *Observe the Answer Try this >>>
Previous
Python Question
If-Else Statement
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" and loops.
An "if statement" is written by using the if keyword.
Previous
Next
Previous
Next
If-Else Statement
a = 33
b = 200
if b > a:
print("b is greater than a") else: #used if first statement is false print("a is greater than b") Try this >>>
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose but NOT Python a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error If statement, without indentation will raise an error.
Previous
Next
ELIF
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition". a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Previous
Next
Previous
Next
If-Else Statement
age = 18 if age < 18: print("Still a minor) elif age < 18: print("Now in Legal Age") Try this >>>
Previous
Next
If-Else Statement
Time = 8 #8:00 AM in actual timeChallenge * Time is 8-12, Print("GoodMorning") * Time is 13-17, Print("GoodAfternoon") #1PM-5PM in actual time * Time is 18-23, Print("GoodAfternoon") #6PM-11PM in actual time * Use If, Elif* Change value of Time to test your code Try this >>>
Previous
Python Question
Match
The match statement is used to perform different actions based on different conditions. Instead of writing many if..else statements, you can use the match statement.
The match statement selects one of many code blocks to be executed. match expression:
case x:
code block
case y:
code block
case z:
code block
Previous
Next
Previous
Next
Match
day = 4
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday") Try this >>>
Match
Use the underscore character _ as the last case value if you want a code block to execute when there are not other matches. This is a Default Value day = 4
match day:
case 6:
print("Today is Saturday")
case 7:
print("Today is Sunday")
case _:
print("Looking forward to the Weekend")
Previous
Next
Match
Use the pipe character | as an or operator in the case evaluation to check for more than one value match in one case day = 4
match day:
case 1 | 2 | 3 | 4 | 5:
print("Today is a weekday")
case 6 | 7:
print("I love weekends!")
Previous
Next
Previous
Next
Match
operator = 1 Challenge; * Use Numbers inside the option variable * Use Match to determine the Conditions * Print("Add"), if operator's value is 1 * Print("Minus"), if operator's value is 2 * Print("Multiply"), if operator's value is 3 * Print("Divide"), if operator's value is 4 Try this >>>
Previous
Python Question
Wait for your Teacher
Your Teacher will check your Project. He / She will give you the Password for Lesson 2
Back to Home
Wait for your Teacher
Your Teacher will check your Project. He / She will give you the Password for Lesson 2
Back to Home
Python Basics
King Jasser Andaya
Created on May 26, 2025
For Students who find it hard to program. Learn Basic Programming Concepts
Start designing with a free template
Discover more than 1500 professional designs like these:
View
Essential Business Proposal
View
Project Roadmap Timeline
View
Step-by-Step Timeline: How to Develop an Idea
View
Artificial Intelligence History Timeline
View
Microlearning: Graphic Design
View
Microlearning: Enhance Your Wellness and Reduce Stress
View
Microlearning: Teaching Innovation with AI
Explore all templates
Transcript
Phyton Basics
Start
Create by King Jasser Andaya
Lesson Two
Lesson Three
Lesson One
Lesson Four
Lesson One: Basic Syntax
Python Syntax
Variables
Data Types
Boolean
Numbers
Python Basics
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
Previous
Next
What can Python Do?
Previous
Next
Fill in the Blanks
Why Python?
Previous
Next
Python Question
Previous
Next
Python Syntax
print("Hello, World!")Try it >>> Note: Python has the most simple and almost error free syntax
Variable
Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
x = 5 y = "John"
Variables do not need to be declared with any particular type, and can even change type after they have been set. x = 4 # x is of type integer x = "Sally" # x is now of type string print(x)
Previous
Next
Variable Names
Previous
Next
Previous
Python Question
Previous
Next
Variable
x = "Python " y = "is " z = "awesome" print(x, y, z)Try this >>>Note: Variable can store anything that you can use later on your Program
Previous
Next
Variable
x = 214594 y = 137960 z = 596070870 print(x + y + z)Try this >>>Note: Variables can be used to remember different data
Data Types
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories: Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview None Type: NoneType
Previous
Next
Previous
Python Question
Previous
Next
Data Types
x = "12"y = 24 print( x + y ) Try this >>>Can you add the 2 Variables?Note: Always remember, you cannot interact different Data Type
Booleans
In programming you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of two answers, True or False. When you compare two values, the expression is evaluated and Python returns the Boolean answer:print(10 > 9) print(10 == 9) print(10 < 9)
Previous
Next
Previous
Python Question
Previous
Next
Booleans
a = 200 b = 33 if b > a: print("b is greater than a") else: print("b is not greater than a") Try this >>>
Numbers
There are three numeric types in Python:
Previous
Next
Previous
Python Question
Previous
Next
Numbers
x = 1 # int y = 2.8 # float z = 1j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x) print(a) print(b) print(c) Try this >>>Note: Red Colored Texts are Comments, No need to include it
Previous
Code This
Project Time: Age Checker
1. Create a Variable with any Name and Integer Value 2. Write this Code after: if age >= 18: print("You are an adult.") else: print("You are a minor.") 3. Run your Code 4. Explain your Code
Submit
Previous
Project Time: Age Checker
Finish your Project and you will be given access by your Teacher.
Enter the password
Strings
Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". You can display a string literal with the print() function. Python has a set of built-in methods that you can use on strings. The upper() method returns the string in upper case.The lower() method returns the string in lower case.The strip() method removes any whitespace from the beginning or the end.The replace() method replaces a string with another string.The split() method splits the string into substrings if it finds instances of the separator.
Previous
Next
Previous
Next
Strings
a = " Hello, World! "print(a.upper()) print(a.lower())print(a.strip())print(a.replace("H", "J"))print(a.split(",")) Try this >>>
Previous
Python Question
Slicing Strings
You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string. Note: The first character has index 0.By leaving out the start index, the range will start at the first character. By leaving out the end index, the range will go to the end:
Previous
Next
Previous
Next
Slicing Strings
b = "Hello, World!" print(b[2:5])#Get characters from position 2 to 5 print(b[:5])#Get the characters from the start to position 5print(b[2:])#Get the characters from position 2, and all the way to the end Try this >>>
Combine Strings
To concatenate, or combine, two strings you can use the + operator.Merge variable a with variable b into variable c:a = "Hello" b = "World" c = a + b print(c)To add a space between them, add a " "c = a + " " + b print(c)
Previous
Next
Combine Strings
As we learned in the Python Variables chapter, we cannot combine strings and numbers but we can combine strings and numbers by using f-strings or the format() method! F-String was introduced in Python 3.6, and is now the preferred way of formatting strings. To specify a string as an f-string, simply put an f in front of the string literal, and add curly brackets {} as placeholders for variables and other operations.age = 36 txt = f"My name is John, I am {age}" print(txt)
Previous
Next
Previous
Next
Combine Strings
g = "Hello" h = "King" print(g + " " + h)#Combine the String i = 31print(f"I am { i } years old!") #Combine the String and Integer Try this >>>
Previous
Python Question
Lists
Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets:thislist = ["apple", "banana", "cherry"] print(thislist)To determine how many items a list has, use the len() functionthislist = ["apple", "banana", "cherry"] print(len(thislist))
Previous
Next
Access Lists
List items are indexed and you can access them by referring to the index number.The first item has index 0.thislist = ["apple", "banana", "cherry"] print(thislist[1])You can specify a range of indexes by specifying where to start and where to end the range.thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[2:5])
Previous
Next
Previous
Next
Lists
thisList = ["Apple","Banana","Cherry"] print(thisList)print(len(thisList))print(thisList[2])print(thisList[0])print(thisList[1]) print(thisList[0:2]) Try this >>>
Change Lists
To change the value of a specific item, refer to the index number.thislist = ["apple", "banana", "cherry"] thislist[1] = "blackcurrant" print(thislist)To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values.thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"] thislist[1:3] = ["blackcurrant", "watermelon"] print(thislist)
Previous
Next
Add Item on Lists
To add an item to the end of the list, use the append() method:thislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)To insert a list item at a specified index, use the insert() method.thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)To append elements from another list to the current list, use the extend() method.
Previous
Next
Remove Item on Lists
he remove() method removes the specified item.thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)The pop() method removes the specified index.thislist = ["apple", "banana", "cherry"] thislist.pop(1) print(thislist) The clear() method empties the list
Previous
Next
Previous
Python Question
Previous
Next
Lists
thisList = ["Apple","Banana","Cherry"] Challenge:* Add "Durian" on the List* Add "Atis" in the Middle of "Apple" and "Banana"* Change "Cherry" to "Carrot"* Remove 1 Item Try this >>>
Tuples
Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. thistuple = ("apple", "banana", "cherry") print(thistuple)
Previous
Next
Access Tuples
You can access tuple items by referring to the index number, inside square brackets:thistuple = ("apple", "banana", "cherry") print(thistuple[1]) The first item has index 0.You can specify a range of indexes by specifying where to start and where to end the range.thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(thistuple[2:5])
Previous
Next
Change Tuples
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created. But there are some workarounds. You can convert the tuple into a list, change the list, and convert the list back into a tuple.x = ("apple", "banana", "cherry") y = list(x) y[1] = "kiwi" x = tuple(y) print(x)
Previous
Next
Previous
Python Question
Previous
Next
Lists
thisTuple = ["Honda","Toyota","Suzuki"] Challenge: * Print 1st Item* Add "Isuzu" * Change "Toyota" to "Kia" Try this >>>
Sets
Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is unordered, unchangeable*, and unindexed. thisset = {"apple", "banana", "cherry"} print(thisset)Sets are unordered, so you cannot be sure in which order the items will appear.
Previous
Next
Access Sets
You cannot access items in a set by referring to an index or a key.Check if "banana" is present in the set: thisset = {"apple", "banana", "cherry"} print("banana" in thisset)Check if "banana" is NOT present in the set:thisset = {"apple", "banana", "cherry"} print("banana" not in thisset)
Previous
Next
Add Sets
Once a set is created, you cannot change its items, but you can add new items.Add an item to a set, using the add() method:thisset = {"apple", "banana", "cherry"} thisset.add("orange") print(thisset)o add items from another set into the current set, use the update() method. thisset = {"apple", "banana", "cherry"} tropical = {"pineapple", "mango", "papaya"} thisset.update(tropical) print(thisset)
Previous
Next
Remove Sets
To remove an item in a set, use the remove(), or the discard() method.thisset = {"apple", "banana", "cherry"} thisset.remove("banana") print(thisset)If the item to remove does not exist, remove() will raise an error.Remove "banana" by using the discard() methodthisset = {"apple", "banana", "cherry"} thisset.discard("banana") print(thisset)
Previous
Next
Previous
Python Question
Previous
Next
Sets
thisSet = {"Honda","Toyota","Suzuki"} Challenge: * Print an Item* Add "Hyundai"* Remove "Honda" Try this >>>
Dictionary
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.Dictionary items are presented in key:value pairs, and can be referred to by using the key name.thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} print(thisdict)print(thisdict["brand")
Previous
Next
Previous
Python Question
Access Dictionary
You can access the items of a dictionary by referring to its key name, inside square brackets:There is also a method called get() that will give you the same resultthisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} x = thisdict["model"]y = thisdict.get("model")print (x)print (y)
Previous
Next
Change Item in Dictionary
You can change the value of a specific item by referring to its key name thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict["year"] = 2018 print(thisdict)The update() method will update the dictionary with the items from the given argument.thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict.update({"year": 2020}) print(thisdict)
Previous
Next
Add Item in Dictionary
Adding an item to the dictionary is done by using a new index key and assigning a value to it. thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict["color"] = "red" print(thisdict)The update() method will update the dictionary with the items from a given argument. If the item does not exist, the item will be added. thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict.update({"color": "red"}) print(thisdict)
Previous
Next
Previous
Python Question
Remove Item in Dictionary
There are several methods to remove items from a dictionary. The pop() method removes the item with the specified key nameThe popitem() method removes the last inserted itemThe del keyword removes the item with the specified key name. The del keyword can also delete the dictionary completely.thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} thisdict.pop("model")thisdict.popitem()del thisdict["model"] print(thisdict)
Previous
Next
Previous
Next
Dictionary
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } Challenge: * Print "model" * Add "Type" : "Sedan" * Remove "brand" Try this >>>
Previous
Code this
Project Time: Contact List
1.Create a Contact List Program. The Program can Add a Contact 2. Print All Contacts on the List3. Remove a Contact4. Print All Contacts again 5. You can use List, Tuples, Set or Dictionary (Create the Program using 2 of these) 6. Run your Code 7. Explain your Code to your Teacher
Previous
Submit
Project Time: Age Checker
Finish your Project and you will be given access by your Teacher.
Enter the password
Math Operators
Operators are used to perform operations on variables and values. Python divides the operators in the following groups:
Previous
Next
Math Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Previous
Next
Previous
Next
Math Operators
x = 20 y = 30 print( x + y ) Challenge: *Change the 2 Variables to different values *Use - , * , / to perform other basic artihmetic operations Try this >>>
Previous
Python Question
Comparison Operators
Comparison operators are used to compare two values:
Previous
Next
Previous
Next
Comparison Operators
x = 20 y = 30 print( x < y ) Challenge: *Compare the two variables *Use all of the Comparison Operators *Observe the Answer Try this >>>
Previous
Python Question
Logical Operators
Logical operators are used to combine conditional statements.
Previous
Next
Previous
Next
Logical Operators
x = 20 y = 25 z = 30 print( x < y and y > z) print( x < y and y < z) Challenge: *Compare the two variables *Use all of the Logical Operators *Observe the Answer Try this >>>
Previous
Python Question
If-Else Statement
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" and loops. An "if statement" is written by using the if keyword.
Previous
Next
Previous
Next
If-Else Statement
a = 33 b = 200 if b > a: print("b is greater than a") else: #used if first statement is false print("a is greater than b") Try this >>>
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose but NOT Python a = 33 b = 200 if b > a: print("b is greater than a") # you will get an error If statement, without indentation will raise an error.
Previous
Next
ELIF
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition". a = 33 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal")
Previous
Next
Previous
Next
If-Else Statement
age = 18 if age < 18: print("Still a minor) elif age < 18: print("Now in Legal Age") Try this >>>
Previous
Next
If-Else Statement
Time = 8 #8:00 AM in actual timeChallenge * Time is 8-12, Print("GoodMorning") * Time is 13-17, Print("GoodAfternoon") #1PM-5PM in actual time * Time is 18-23, Print("GoodAfternoon") #6PM-11PM in actual time * Use If, Elif* Change value of Time to test your code Try this >>>
Previous
Python Question
Match
The match statement is used to perform different actions based on different conditions. Instead of writing many if..else statements, you can use the match statement. The match statement selects one of many code blocks to be executed. match expression: case x: code block case y: code block case z: code block
Previous
Next
Previous
Next
Match
day = 4 match day: case 1: print("Monday") case 2: print("Tuesday") case 3: print("Wednesday") case 4: print("Thursday") case 5: print("Friday") case 6: print("Saturday") case 7: print("Sunday") Try this >>>
Match
Use the underscore character _ as the last case value if you want a code block to execute when there are not other matches. This is a Default Value day = 4 match day: case 6: print("Today is Saturday") case 7: print("Today is Sunday") case _: print("Looking forward to the Weekend")
Previous
Next
Match
Use the pipe character | as an or operator in the case evaluation to check for more than one value match in one case day = 4 match day: case 1 | 2 | 3 | 4 | 5: print("Today is a weekday") case 6 | 7: print("I love weekends!")
Previous
Next
Previous
Next
Match
operator = 1 Challenge; * Use Numbers inside the option variable * Use Match to determine the Conditions * Print("Add"), if operator's value is 1 * Print("Minus"), if operator's value is 2 * Print("Multiply"), if operator's value is 3 * Print("Divide"), if operator's value is 4 Try this >>>
Previous
Python Question
Wait for your Teacher
Your Teacher will check your Project. He / She will give you the Password for Lesson 2
Back to Home
Wait for your Teacher
Your Teacher will check your Project. He / She will give you the Password for Lesson 2
Back to Home