0% found this document useful (0 votes)
3 views4 pages

Introduction to Python

This document serves as an introduction to Python, covering fundamental concepts such as variables, data types, operators, control flow, loops, and functions. It provides practical examples, including a simple calculator program, to help beginners understand how to write basic Python code. The lecture emphasizes the importance of readability and versatility in Python programming.

Uploaded by

omarabdulaziz750
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)
3 views4 pages

Introduction to Python

This document serves as an introduction to Python, covering fundamental concepts such as variables, data types, operators, control flow, loops, and functions. It provides practical examples, including a simple calculator program, to help beginners understand how to write basic Python code. The lecture emphasizes the importance of readability and versatility in Python programming.

Uploaded by

omarabdulaziz750
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

Introduction to Python

October 22, 2025

1 Introductory Practical Python Lecture: Fundamentals Only


This lecture introduces Python fundamentals — enough to get a beginner writing simple, useful
programs. We’ll focus on understanding how Python thinks, not on advanced topics like object-
oriented programming.

2 1. Getting Started with Python


What is Python? Python is a high-level, interpreted language known for its readability and versa-
tility. You can run code interactively (in a REPL or Jupyter Notebook) or by saving it in a .py
file. Running Python Code You can use:
• Interactive mode: type python in your terminal.
• Script mode: save your code in a .py file, then run it with python3 [Link].

[1]: print("Hello, world!")

Hello, world!
This prints text to the console.

3 2. Variables and Data Types


Variables hold data. You create them by assignment:

[2]: name = "Alice"


age = 25
height = 1.68
is_student = True

Basic Types
Type Example Description
int 5 Whole number
float 3.14 Decimal number
str ‘hello’ Text
bool True / False Logical value

1
Use type() to check what kind of data a variable holds:

[3]: print(type(age))

<class 'int'>

4 3. Operators
Operators let you perform actions: Arithmetic: +, -, *, /, //, %, ** python x = 10 y = 3

[4]: x = 10
y = 3
print(x + y) # 13
print(x // y) # 3 (integer division)
print(x ** y) # 1000 (exponentiation)

13
3
1000
Comparison: ==, !=, <, >, <=, >=
Logical: and, or, not

5 4. Input and Output


To get user input:

[5]: name = input("Enter your name: ")


print("Hello,", name)

Hello, Wasim
All input is received as a string. Convert if needed:

[6]: age = int(input("Enter your age: "))

6 5. Control Flow: If Statements


Use if / elif / else to make decisions:

[7]: if age < 18:


print("You’re a minor.")
elif age < 65:
print("You’re an adult.")
else:
print("You’re a senior.")

You’re an adult.
Indentation shows code blocks. (Typically 4 spaces.)

2
7 6. Loops
for Loop
Iterate over sequences:

[8]: for i in range(5):


print(i)

0
1
2
3
4
range(5) means numbers 0–4.
while Loop
Run code while a condition is true:

[9]: n = 0
while n < 5:
print(n)
n += 1

0
1
2
3
4

8 7. Lists and Basic Collections


Lists hold ordered data:

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


print(fruits[0]) # first item

apple
You can modify, add, or remove items:

[11]: [Link]("orange")
[Link]("banana")

Other useful types:


• Tuple: immutable list — (‘a’, ‘b’)
• Set: unique unordered items — {‘a’, ‘b’, ‘c’}
• Dictionary: key-value pairs — {“name”: “Alice”, “age”: 25}

3
9 8. Functions
Functions organize reusable logic:

[13]: def greet(name):


print("Hello,", name)

greet("Wasim")

Hello, Wasim
You can return values:

[14]: def add(a, b):


return a + b

10 9. Practical Example: Simple Calculator


Try combining what you’ve learned:

[15]: def calculator():


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")

if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '*':
print(num1 * num2)
elif op == '/':
print(num1 / num2)
else:
print("Invalid operator")

calculator()

3.0

11 10. Mini Review Activity


Try to answer these on your own:
1. What’s the difference between a list and a tuple?
2. How can you loop through a dictionary’s keys and values?
3. What happens if you forget to indent under an if statement?

You might also like