Python Programming Tutorial Notes
Introduction
- Learn everything needed to start programming in Python.
- Suitable for data science, machine learning, and web development.
- No prior knowledge required.
- Instructor: Mosh Hamadani.
Applications of Python
- Multi-purpose language: Can be used for various tasks.
- Machine Learning & AI: Most popular language for ML and data science.
- Web Development: Used with Django framework to build websites (e.g., YouTube, Instagram).
- Automation: Helps save time by automating repetitive tasks.
Setting Up Python
- Download Python: Visit python.org, go to downloads, and get the latest version.
- For Windows, check "Add Python to PATH" during installation.
- Install a Code Editor: Recommended: PyCharm.
- Download the Community Edition (free).
- Follow installation steps (drag to Applications for Mac).
- Create a New Project: Open PyCharm, skip configuring settings, create a project.
Writing Your First Python Code
- Create a new Python file (e.g.,
app.py
).
- Write a simple program:
print("Hello, World!")
- Run the code to see the output in the terminal.
Variables
- Definition: Store data temporarily in memory.
- Declaration:
- Example:
age = 20
- Use variables without quotes to reference them.
- Changing Values: e.g.,
age = 30
- Types of Values:
- Integers, floating-point numbers, strings, booleans.
- Naming Conventions: Use underscores for multi-word variables.
User Input
Data Types in Python
- Primitive Types:
- Numbers (integers and floats), Booleans, Strings.
- Type Conversion:
- Functions:
int()
, float()
, str()
, bool()
.
Example Exercise
- Create a program for a hospital to check in a patient using variables.
Arithmetic Operations
- Operators:
+
, -
, *
, /
, //
, %
, and **
(exponentiation).
- Augmented Assignment: e.g.,
x += 3
.
- Operator Precedence: Multiplication and division before addition.
Comparison Operators
- Used to compare values:
>
, >=
, <
, <=
, ==
, !=
.
- Boolean Expression: Results in
True
or False
.
Logical Operators
- Used for complex conditions:
and
, or
, not
.
Control Structures: If Statements
- If Statements: Make decisions based on conditions.
if temperature > 30:
print("It's a hot day")
- Use
elif
for additional conditions and else
for fallback.
Looping: While Loops
- While Loops: Repeat a block of code while a condition is true.
while i <= 5:
print(i)
i += 1
Lists
- Definition: Ordered collection of items.
- Creating a List:
names = ["John", "Bob", "Mary"]
- Accessing Elements: Use index notation (0-based).
- List Methods:
append()
, insert()
, remove()
, clear()
, in
operator to check existence.
- Length of List: Use
len()
function.
For Loops
- Definition: Iterate over items in a collection.
for item in names:
print(item)
Range Function
- Generates a sequence of numbers:
range(start, stop, step)
.
Tuples
- Definition: Similar to lists but immutable.
- Creating a Tuple:
numbers = (1, 2, 3)
.
- Methods:
count()
, index()
.
Conclusion
- Further Learning: Consider enrolling in comprehensive courses for deeper understanding.