Object-Oriented Programming in Python
Introduction to Python
Say OL
[email protected]
November 3, 2024
What is Python?
High-level programming language
Easy to learn and use
Versatile: web, data science, automation
Setting Up Python
Download from python.org
IDEs: PyCharm, Jupyter Notebook, VS Code
Basic Syntax
1 # This is a comment
2 print("Hello, World!") # Print statement
Output: Hello, World!
Data Types: Strings
Text data is stored as strings.
1 name = "Alice"
2 print(name)
Output: Alice
Data Types: Integers
Whole numbers are integers.
1 age = 25
2 print(age)
Output: 25
Data Types: Floats
Decimal numbers are floats.
1 height = 5.5
2 print(height)
Output: 5.5
Variables
Variables store data values.
1 x = 10
2 y = 5
3 result = x + y
4 print(result)
Output: 15
Basic Arithmetic Operations
Addition, Subtraction, Multiplication, Division
1 a = 8
2 b = 4
3 sum = a + b
4 product = a * b
Outputting Results
Using print() to display results.
1 print("Sum:", sum)
2 print("Product:", product)
Output:
Sum: 12
Product: 32
Say ([email protected]) OOP November 3, 2024 10 / 23
Input from Users
Use input() to get user input.
1 user_name = input("Enter your name: ")
2 print("Hello,", user_name)
Say ([email protected]) OOP November 3, 2024 11 / 23
String Manipulation
Strings can be concatenated.
1 greeting = "Hello, " + user_name
2 print(greeting)
Say ([email protected]) OOP November 3, 2024 12 / 23
String Formatting
Using f-strings for formatted output.
1 age = 25
2 print(f"{user_name} is {age} years old.")
Say ([email protected]) OOP November 3, 2024 13 / 23
Lists
Lists store multiple values.
1 fruits = ["apple", "banana", "cherry"]
2 print(fruits[0])
Output: apple
Say ([email protected]) OOP November 3, 2024 14 / 23
List Operations
Adding and removing elements.
1 fruits.append("orange")
2 fruits.remove("banana")
3 print(fruits)
Output: ["apple", "cherry", "orange"]
Say ([email protected]) OOP November 3, 2024 15 / 23
Tuples
Tuples are immutable lists.
1 my_tuple = (1, 2, 3)
2 print(my_tuple[1])
Output: 2
Say ([email protected]) OOP November 3, 2024 16 / 23
Dictionaries
Key-value pairs for data storage.
1 my_dict = {’name’: ’Alice’, ’age’: 25}
2 print(my_dict[’name’])
Output: Alice
Say ([email protected]) OOP November 3, 2024 17 / 23
Dictionary Operations
Adding and accessing values.
1 my_dict[’height’] = 5.5
2 print(my_dict)
Output: "name": "Alice", "age": 25, "height": 5.5
Say ([email protected]) OOP November 3, 2024 18 / 23
Sets
Sets store unique values.
1 my_set = {1, 2, 3}
2 my_set.add(4)
3 print(my_set)
Say ([email protected]) OOP November 3, 2024 19 / 23
Basic Input/Output
Using input and print functions.
1 number = input("Enter a number: ")
2 print(f"You entered: {number}")
Say ([email protected]) OOP November 3, 2024 20 / 23
Basic Error Handling
Using try-except to handle errors.
1 try:
2 num = int(input("Enter a number: "))
3 print(num)
4 except ValueError:
5 print("That’s not a valid number!")
Say ([email protected]) OOP November 3, 2024 21 / 23
Summary of Week 1
Python basics: syntax, data types, and variables
Control structures: input, output, and error handling
Say ([email protected]) OOP November 3, 2024 22 / 23
Next Week
Control structures: conditionals and loops
More advanced data structures
Say ([email protected]) OOP November 3, 2024 23 / 23