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

Phyton Basic Handout

The document provides an introduction to Python, highlighting its features, use cases, and setup instructions. It covers basic syntax, operators, control flow, data structures, functions, file handling, and error handling. Additionally, it suggests next steps for further learning and project ideas.

Uploaded by

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 views7 pages

Phyton Basic Handout

The document provides an introduction to Python, highlighting its features, use cases, and setup instructions. It covers basic syntax, operators, control flow, data structures, functions, file handling, and error handling. Additionally, it suggests next steps for further learning and project ideas.

Uploaded by

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/ 7

1.

Introduction to Python
Python is a high-level, interpreted programming language known for:
✔ Simple & readable syntax
✔ Versatility (Web, Data Science, AI, Automation)
✔ Large standard library

Python Use Cases

 Web Development (Django, Flask)


 Data Analysis (Pandas, NumPy)
 Machine Learning (TensorFlow, PyTorch)
 Scripting & Automation

2. Python Setup
Install Python

Download from python.org

Run Python Code

1. Interactive Shell (REPL)

bash

Copy

Download

python
>>> print("Hello World!")

2. Script File (hello.py)


python
Copy

Download

print("Hello World!")

Run with:
bash

Copy

Download

python hello.py

3. Basic Syntax
Variables & Data Types
python

Copy
Download
name = "Alice" # String
age = 25 # Integer
price = 9.99 # Float
is_student = True # Boolean

Printing Output
python

Copy
Download
print("Hello,", name) # Hello, Alice
print(f"Age: {age}") # f-strings (Python 3.6+)

Comments
python

Copy
Download
# Single-line comment
"""
Multi-line
comment
"""

4. Operators
Arithmetic Operators
python

Copy
Download
x = 10
y = 3

print(x + y) # 13 (Addition)
print(x - y) # 7 (Subtraction)
print(x * y) # 30 (Multiplication)
print(x / y) # 3.333... (Division)
print(x // y) # 3 (Floor Division)
print(x % y) # 1 (Modulus)
print(x ** y) # 1000 (Exponent)

Comparison Operators
python

Copy
Download
print(x == y) # False (Equal)
print(x != y) # True (Not Equal)
print(x > y) # True (Greater Than)

Logical Operators
python

Copy
Download
print((x > 5) and (y < 10)) # True (AND)
print((x < 5) or (y < 2)) # False (OR)
print(not is_student) # False (NOT)

5. Control Flow
If-Else Statements
python

Copy
Download
if age >= 18:
print("Adult")
elif age >= 13:
print("Teen")
else:
print("Child")

Loops

1. for Loop
python

Copy
Download
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

2. while Loop
python

Copy
Download
count = 0
while count < 5:
print(count)
count += 1
6. Data Structures
Lists (Mutable Arrays)
python

Copy
Download
numbers = [1, 2, 3]
numbers.append(4) # [1, 2, 3, 4]
numbers.pop() # [1, 2, 3]

Tuples (Immutable Lists)


python

Copy
Download
point = (10, 20)
x, y = point # Unpacking

Dictionaries (Key-Value Pairs)


python

Copy
Download
person = {"name": "Alice", "age": 25}
print(person["name"]) # Alice

Sets (Unique Elements)


python

Copy
Download
unique_numbers = {1, 2, 2, 3} # {1, 2, 3}

7. Functions
Basic Function
python

Copy
Download
def greet(name):
return f"Hello, {name}!"

print(greet("Bob")) # Hello, Bob!

Default Arguments
python

Copy
Download
def power(base, exponent=2):
return base ** exponent

print(power(3)) # 9 (3^2)
print(power(3, 3)) # 27 (3^3)

8. File Handling
Reading a File
python

Copy
Download
with open("file.txt", "r") as file:
content = file.read()
print(content)

Writing to a File
python

Copy
Download
with open("file.txt", "w") as file:
file.write("Hello, Python!")
9. Error Handling (Try-Except)
python

Copy
Download
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")

10. Next Steps


 Learn Object-Oriented Programming (OOP)
 Explore Python Libraries (NumPy, Pandas, Flask)
 Build projects:

o Calculator
o To-Do List App
o Web Scraper

You might also like