[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

G08 Revision Notes

The document provides an overview of fundamental Python programming concepts, including selection, iteration, assignment, and output. It covers creating and manipulating lists, debugging errors, and using loops to process user input. Additionally, it includes examples of list operations and dynamic modifications such as adding, removing, and sorting items.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

G08 Revision Notes

The document provides an overview of fundamental Python programming concepts, including selection, iteration, assignment, and output. It covers creating and manipulating lists, debugging errors, and using loops to process user input. Additionally, it includes examples of list operations and dynamic modifications such as adding, removing, and sorting items.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PYTHON PROGRAMMING

1. Understanding selection, iteration, assignment, and output in Python:

o Selection: Utilizes if, elif, and else statements to execute certain code
based on conditions.

 Example:

age = 18
if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult.")
else:
print("You are an adult.")

o Iteration: Uses loops to repeat code blocks.

o Assignment: Assigning values to variables using the = operator.

o Output: Displaying information to the user via the print() function.

2. Creating lists (arrays) and adding string values:

o Lists are ordered collections that can hold a variety of data types,
including strings.

o Example:

fruits = ["apple", "banana", "cherry"]


fruits.append("orange") # Adding a new string to the list
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
3. Reading and predicting program output:

o Understanding the flow of a program and determining what output will


occur based on given input.

o Example:

x = 10
if x > 5:
print("Greater than five")
else:
print("Not greater than five")
#Predict the output: "Greater than five".
4. Using loops (for, while) to process user input dynamically:

o For loop example:

for i in range(5):
print("Iteration", i)
o While loop example:

count = 0
while count < 5:
print("Count is", count)
count += 1
5. Counting items in a list using len():

o The len() function returns the number of items in a list.

o Example:

o colors = ["red", "blue", "green"]


o print(len(colors)) # Output: 3
6. Debugging errors in code and fixing them:

o Identify syntax errors and logical errors by running the code and observing
the output, using print statements or debuggers.

o Example:

o result = 10 / 0 # This will raise an error: ZeroDivisionError

o Fixing it might involve checking for division by zero beforehand.

7. Using list operations (append(), remove(), sort(), modifying elements):

o Append: Adds an item to the end of the list.

o Remove: Deletes the first occurrence of a specified value.

o Sort: Organizes the list in ascending order.

o Example:

numbers = [5, 3, 8]
numbers.append(1)
numbers.remove(3)
numbers.sort()
print(numbers)
# Output: [1, 5, 8]
Writing programs that modify lists dynamically:

 Deleting an item from a list:

chores = ["laundry", "dishes", "vacuum"]


chores.remove("dishes")
print(chores)
# Output: ['laundry', 'vacuum']
 Correcting misspelled words in a list:

misspelled = ["aple", "bannana", "cherry"]


corrected = ["apple" if word == "aple" else word for word in misspelled]
print(corrected)
# Output: ['apple', 'bannana', 'cherry']
 Adding new items to a list:

tasks = ["write report", "fix bugs"]


tasks.append("review code")
print(tasks) # Output: ['write report', 'fix bugs', 'review code']
 Sorting lists alphabetically:

names = ["John", "Alice", "Bob"]


names.sort()
print(names)
# Output: ['Alice', 'Bob', 'John']

You might also like