Python Theory
Python Theory
Python Theory
Boolean Expressions
o Boolean expressions return True or False.
Logical Operators
o Logical operators include and, or, and not.
Conditional Operators
o Conditional operators like ==, !=, >, <, are used for comparison.
Return Statements
o return is used in functions to return a value.
Loops
o While Loops: Repeat as long as a condition is true.
Math Functions
o Python provides built-in math functions like abs(), pow(), and the
math module for more complex operations.
Adding New Functions
o Define a function using def with a name, parameters, and a body.
String Length
o The len() function returns the number of characters in a string.
String Traversal
o You can loop through the characters of a string using a for loop.
String Slices
o Slicing allows access to a substring using s[start:end].
Comparison
o Strings can be compared using relational operators.
Find Function
o The find() method searches for a substring and returns the index of
its first occurrence.
Looping and Counting
o You can loop through a string and count occurrences of a character
using a loop.
2. Lists
List Values
o Lists are ordered collections of values (like [1, 2, 3]).
Length
o The len() function returns the number of elements in a list.
Membership
o You can check if an item is in a list using in.
Operations
o Lists support concatenation (+) and repetition (*).
Slices
o You can access parts of a list using slicing (e.g., list[start:end]).
Deletion
o Elements can be deleted using del or remove().
Accessing Elements
o Elements are accessed by their index (e.g., list[0]).
If you are working on a specific project or have particular areas where you'd like
more focus, let me know!
4o
You said:
example python code for each
ChatGPT said:
ChatGPT
Here are Python code examples for each of the topics you mentioned:
# Operations on strings
greeting = "Hello"
name = "World"
print(greeting + " " + name) # Concatenation
# Comments
# This is a comment and it won't be executed by Python
3. Conditionals and Iteration
python
Copy code
# Conditional statements
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
# While loop
count = 0
while count < 5:
print("Count:", count)
count += 1
# For loop
for i in range(5):
print("For loop iteration:", i)
4. Functions and Recursion
python
Copy code
# Function definition and call
def add_numbers(a, b):
return a + b
result = add_numbers(2, 3)
print("Sum:", result)
# String comparison
print("apple" > "banana") # False, because 'a' comes before 'b'
# Find function
sentence = "Hello, welcome to Python!"
index = sentence.find("Python")
print("Index of 'Python':", index) # Output: 18
2. Lists
python
Copy code
# List values and operations
numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # Output: 1
# List membership
print(3 in numbers) # True
# List slicing
print(numbers[1:3]) # Output: [2, 3]
# Deleting element
del numbers[2] # Deletes the element at index 2
print(numbers) # Output: [1, 2, 4, 5]
# Tuple assignment
x, y = (5, 10)
print(x, y) # Output: 5 10
# Dictionary operations
student_grades = {"Alice": 90, "Bob": 85}
print(student_grades["Alice"]) # Output: 90
# Random numbers
import random
random_number = random.randint(1, 100)
print(random_number)