Python Programming Tutorial Notes
Introduction
- The tutorial is designed for beginners with no prior knowledge of Python or programming.
- Focuses on programming for data science, machine learning, and web development.
- Instructor: Mosh Hamadani.
What You Can Do with Python
- Machine Learning and AI: Python is the leading language for these fields.
- Web Development: Use frameworks like Django; notable websites include YouTube, Instagram, Spotify, Dropbox, Pinterest.
- Automation: Simplify repetitive tasks to save time.
Setup Instructions
-
Download Python
- Visit python.org.
- Choose the latest version from the downloads section.
- Important: Check "Add Python to PATH" during installation.
-
Install a Code Editor
- Recommended: PyCharm (available at jetbrains.com).
- Choose Community Edition (free).
- Follow installation instructions based on OS.
-
Create a New Project in PyCharm
- Name your project and confirm its location.
- Right-click to create a new Python file for coding.
Your First Python Program
- Writing "Hello, World!" in Python:
print("Hello, World!")
- To run the code: Go to Run > Run or use a shortcut.
Variables
- Definition: Used to store data temporarily in memory.
- Example of declaring a variable:
age = 20
- To print a variable's value:
print(age)
- Types of Variables:
- Integer: whole numbers (e.g.,
age
)
- Float: numbers with decimals (e.g.,
price = 19.95
)
- String: textual data (e.g.,
name = "Mosh"
)
- Boolean: True or False (e.g.,
is_online = True
)
User Input
Data Types
-
Primitive Types:
- Numbers (Integers and Floats)
- Strings
- Booleans
-
Type Conversion:
- Use functions like
int()
, float()
, str()
to convert between types.
Arithmetic Operations
- Basic operators:
+
, -
, *
, /
, //
(integer division), %
(modulus), **
(exponentiation).
- Operator precedence: Multiplication and division before addition and subtraction.
Comparison Operators
- Common operators:
>
, >=
, <
, <=
, ==
, !=
Logical Operators
- Combine boolean expressions:
and
: true if both are true.
or
: true if at least one is true.
not
: reverses the boolean value.
Conditionals (If Statements)
Loops
-
While Loop: Repeat a block of code while a condition is true.
while i <= 5:
print(i)
i += 1
-
For Loop: Iterate over a sequence (such as a list):
for item in list:
print(item)
Lists
Range Function
Tuples
- Similar to lists but immutable:
numbers = (1, 2, 3)
- Cannot change elements after creation.
Exercise Sections
- Several hands-on exercises are included to practice concepts.
Additional Resources
- Consider enrolling in Mosh's online Python course at codewoodmosh.com, which includes more comprehensive training and a certificate of completion.
Note: Review each section in detail for deeper understanding, especially coding examples and functions mentioned.
Ask questions if anything is unclear as you learn!