Python Programming Tutorial Notes
Introduction
- Learn everything about Python programming.
- Suitable for data science, machine learning, web development.
- No prior knowledge required.
- Instructor: Mosh Hamadani.
What You Can Do with Python
- Machine Learning & AI: Python is the top language.
- Web Development: Popular frameworks like Django.
- Examples of websites: YouTube, Instagram, Spotify, Dropbox, Pinterest.
- Automation: Automate repetitive tasks, saving time and increasing productivity.
Setting Up Python
- Go to python.org to download the latest version of Python.
- Ensure "Add Python to PATH" is checked during installation (for Windows users).
- Install a code editor; recommended: PyCharm.
- Download the Community Edition (free).
- Create a new project in PyCharm.
- Create a Python file named
app.py
for coding.
Writing Your First Code
- Use the
print
function to output text:
print("Hello, World!")
- Strings: Textual data surrounded by quotes.
- Run Code: Use the Run menu or shortcuts.
Variables
age = 20
- Use variables to store data temporarily.
- Change variable values:
age = 30
- Variable types: integers, floats, strings, booleans.
User Input
- Use the
input
function to receive user input:
name = input("What is your name? ")
- Combine strings for output with concatenation:
print("Hello, " + name)
Type Conversion
- Convert types using built-in functions:
int()
, float()
, str()
, bool()
.
- Important when working with user input.
Arithmetic Operations
- Operators:
+
, -
, *
, /
, //
(floor division), %
, **
(exponentiation).
- Example:
result = 10 + 3 * 2 # result is 16
Comparison Operators
- Used for comparisons:
>
, <
, >=
, <=
, ==
, !=
.
- Produce boolean values (True/False).
Logical Operators
- Combine boolean expressions:
and
, or
, not
.
- Example:
if (condition1 and condition2):
# do something
If Statements
- Create decisions in your code:
if temperature > 30:
print("It's a hot day")
elif temperature > 20:
print("It's a nice day")
else:
print("It's cold")
Loops
- While Loops: Repeat code until a condition is false.
- For Loops: Iterate over sequences (e.g., lists, range objects).
Lists
names = ["John", "Bob", "Mosh"]
- Access elements using indexes:
names[0]
- Use methods like
append()
, insert()
, remove()
, clear()
.
Tuples
- Similar to lists, but immutable:
coordinates = (10.0, 20.0)
- Use when you want to ensure data integrity.
Range Function
- Generate sequences of numbers:
for number in range(5):
print(number)
- Can specify start, end, and step.
Conclusion
- Python is versatile and used in many domains.
- Recommended to explore further through online courses for deeper understanding.