Python Fundamentals - Lesson 4_ Getting Started With Python
Python Fundamentals - Lesson 4_ Getting Started With Python
WITH PYTHON
LEARNING OBJECTIVES
By the end of this lesson, you will be able to:
LESSON OVERVIEW
Python is a high-level, easy-to-read language, popular among beginners and professionals alike. In this lesson, you’ll learn the
foundation of Python programming: how to write code correctly, store information using variables, and work with different types of
data.
PYTHON SYNTAX
Python is known for its clean and simple syntax. That means fewer symbols and punctuation marks compared to many other
programming languages like Java or C++.
Python Code
#My first Python code
print("Hello, World!")
"""
This is a comment
written in
more than just one line
"""
✅ Try It:
Open Google Colab, create a new notebook, and run the examples above in new code cells.
2. Indentation
Indentation refers to the spaces at the beginning of line of code (use space bar or tab). Python uses indentation to indicate a block
of code.
Python Code
if 5 > 3:
print("Five is greater than three")
if 5 > 3:
print("Five is greater than three")
if 10 > 3:
if 15 > 3:
print("All numbers are greater than 3")
✅ Try It:
Run the examples above in new code cells.
3. Lines
Backslash can be used to place long statements on multiple lines. Semicolon can be used to put multiple short statements on the
same line.
Python Code
#This is a long statement broken into two lines by using a backslash
result = 1 + 2 + 3 + 4 + \
5 + 6 + 7 + 8 + 9
print(result)
#These are two short statements placed on the same line using a semicolon
name = "Aminah"; age = 30; print(name, age)
✅ Try It:
Run the examples above in new code cells.
4. Semicolons
Statements end at the end of the line. No semicolons used in the codes to indicate the end of the line.
Python Code
#No semicolon at the end of line
print("Hello")
✅ Try It:
Run the examples above in new code cells.
VARIABLES
A variable is like a container that holds a value. Python has no command for declaring a variable.
x = 3
y = "Hello"
print(x)
print(y)
✅ Try It:
Run the examples above in new code cells.
Value Assignment
Assigning a value to a variable is done using the assignment operator (=).
Assignments can be done on more than one variable "simultaneously" on the same line or assign the same value to multiple
variable.
Python Code
#assign values to multiple variable
x, y, z = 5,10,15
print(x)
print(y)
print(z)
✅ Try It:
Python Code
# Addition (+)
x = 5
y = 10
print(x + y)
a = 3 + 6
print(a)
# Subtraction (-)
b = 5 - 2
print(b)
# Multiplication (*)
c = 2 * 4
print(c)
# Division (/)
d = 15 / 3
print(d)
# Modulus (%)
e = 7 % 3
print(e)
# Exponent (**)
f = 2 ** 4
print(f)
✅ Try It:
DATA TYPES
Python automatically detects the type of value you assign to a variable.
Python Code
x = 5 # int
y = 3.14 # float
z = 1 + 2j # complex
✅ Try It:
Python Code
name = "Alice"
print(name)
✅ Try It:
These are often used in conditional statements, comparisons, and logic operations.
Python Code
x = True
y = False
print(y) # Output: False
is_sunny = True
is_raining = False
print(is_sunny) # Output: True
# +++ Python automatically returns a Boolean when you perform a comparison +++
# Equal (==)
result1 = 5 == 5
print(result1)
number = "1234"
print(number.isdigit()) # True - only digits
✅ Try It:
Python Code
x = 3
y = 3.0
z = "Hello"
print(type(x))
print(type(y))
print(type(z))
✅ Try It:
Python Code
x = int(3.7) # Float to int
print(x) # Output: 3
# If the string cannot be converted to an integer (e.g., "ten"), it will raise an error
# int("ten") # ❌ This will cause an error
name = str(100)
print(name) # Output: "100"
print(type(name)) # Output: <class 'str'>
print(bool(1)) # True
print(bool(0)) # False
print(bool("")) # False (empty string)
print(bool("Hi")) # True (non-empty string)
print(bool( Hi )) # True (non empty string)
✅ Try It:
✅ Try It:
Lists, tuples, sets, and dictionaries are built-in data types in Python. They are all collection data types that is used to store multiple
values in a single variable.
1. List
A list is a mutable, ordered collection of items.
🔹 List Characteristics:
Defined using square brackets [ ]
Allows duplicates
Python Code
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Output: banana
✅ Try It:
✅ Try It:
✅ Try It:
🔹 .extend(iterable) - adds multiple items from another interable (eg:list, tuple) to the end of the list
Python Code
fruits = ["apple", "banana", "cherry", "orange"]
fruits.insert(1,"grapes") # Insert item into index 1
print(fruits) # Output: ['apple', 'grapes', 'banana', 'cherry', 'orange']
fruits.extend(["papaya","watermelon"]) # Insert items at the end
print(fruits) # Output: ['apple', 'grapes', 'banana', 'cherry', 'orange', 'papaya', 'watermelon']
✅ Try It:
✅ Try It:
🔹 .pop(index) - removes item at given index (default:last), returns the removed item, useful if want to use removed value
Python Code
fruits = ["apple", "banana", "cherry", "orange", "banana", "papaya", "watermelon"]
remove_item = fruit.pop(1)
print(remove_item)
print(fruits)
✅ Try It:
🔹 del - deletes an item by index or a slice of items, does not return the value, can also delete entire lists or variables.
Python Code
fruits = ["apple", "banana", "cherry", "orange", "banana", "papaya", "watermelon"]
del fruits[0]
print(fruits)
✅ Try It:
🔹 .clear() - removes all items from the list or other collection (like set and dictionary)
Python Code
fruits = ["apple", "banana", "orange"]
fruits.clear()
print(fruits) # Output: []
✅ Try It:
🔹 Tuple Characteristics:
Defined using parentheses ()
Item have a fixed order
Cannot be modified after creation
Faster than lists
Allows duplicates
Python Code
dimensions = (10, 20)
print(dimensions[0]) # Output: 10
✅ Try It:
✅ Try It:
✅ Try It:
🔹 Set Characteristics:
Defined using curly braces {}
Items are unordered - no indexing
Can add / remove items
No duplicates (Useful for removing duplicates or checking membership)
Python Code
numbers = {1, 2, 3, 3, 4}
print(numbers) # Output: {1, 2, 3, 4} — duplicates removed
✅ Try It:
Python Code
fruits = {"apple", "banana"}
fruits.add("orange")
print(fruits) # Output: {'apple', 'banana', 'orange'}
✅ Try It:
🔹 .update(iterable) – Adds multiple items from an iterable (list, tuple, or another set).
New elements are added one by one, and duplicates are ignored.
Python Code
fruits = {"apple", "banana"}
fruits.update(["orange", "grape"])
print(fruits) # Output: {'apple', 'banana', 'orange', 'grape'}
fruits.update({"melon", "apple"})
print(fruits) # Output: {'apple', 'banana', 'orange', 'grape', 'melon'}
✅
✅ Try It:
Python Code
fruits = {"apple", "banana", "orange"}
fruits.remove("banana")
print(fruits) # {'apple', 'orange'}
✅ Try It:
🔹 .discard(item) – Removes an item, but does not raise an error if the item is not found.
Python Code
fruits = {"apple", "banana", "orange"}
fruits.discard("banana")
print(fruits) # Output: {'apple', 'orange'}
✅ Try It:
Python Code
fruits = {"apple", "banana", "orange"}
removed_item = fruits.pop()
print(removed_item) # Random item, e.g., 'apple'
print(fruits) # Output: Remaining items
✅ Try It:
✅ Try It:
✅ Try It:
🔹 Dictionary Characteristics:
Defined using curly braces {} with key : value format
Keys must be unique
Ordered (as of Python 3.7+)
Can change or update values
Values can be of any data type
Useful for structured data (like JSON)
Python Code
# Example dictionary
student = {
"name": "Ali",
"age": 12,
"class": "6A"
}
✅ Try It:
✅ Try It:
✅ Try It:
✅ Try It:
✅ Try It:
🔹 .popitem() – Removes and returns the last inserted item (Python 3.7+).
Python Code
last = student.popitem()
print(last) # Output: ('height', 140)
i t( t d t) # O t t {' ' 'Ali' ' h l' 'SK K j ' ' d ' 'A' 'h bb ' 'b d i t '}
print(student) # Output: {'name': 'Ali', 'school': 'SK Kajang', 'grade': 'A', 'hobby': 'badminton'}
✅ Try It:
🔹 .popitem() – Removes and returns the last inserted item (Python 3.7+).
Python Code
last = student.popitem()
print(last) # Output: ('height', 140)
print(student) # Output: {'name': 'Ali', 'school': 'SK Kajang', 'grade': 'A', 'hobby': 'badminton'}
✅ Try It:
✅ Try It:
✅ Try It:
SELF-CHECK QUIZ
🧠 Answer these to test your understanding.
1. Which of the following is a valid variable name?
A) 3total
B) userName
C) def
D) print
A) <class 'bool'>
B) <class 'str'>
C) <class 'int'>
D) <class 'float'>
HANDS-ON TASK
🔧 Instructions:
1. Open Google Colab.
2. Create a new notebook.
3. Practice the example codes in sections (1) Python Syntax, (2) Variables, (3) Data Types
4. Write a small program that: