Lecture on Python Syntax and Basics
Introduction
- بسم الله الرحمن الرحيم والصلاة والسلام على سيدنا محمد
- Learn syntax (البنية) of Python
- Create a simple “Hello Python ” application
Command Line Usage
- Use command line often; it will be very helpful
- Navigate to project directory (e.g.,
Documents)
- Create a new folder using
mkdir Python
- Create a new file using
touch first.py
- Open files with Notepad or Visual Studio Code
Visual Studio Code Tips
- Install Python extensions when prompted
- Use the integrated terminal (
New Terminal | Add New Terminal)
- The
Run Python File in Terminal button runs Python scripts directly
First Python Function
print(“I love Python”)
print is a built-in function in Python
- Used to print output to the screen
- Save file and run script using
python first.py in command line or VS Code terminal
Semicolon Usage
- Python typically doesn’t use semicolons
- Semicolons needed when combining multiple statements on one line
- Example:
print(“I love Python”); print(“I love programming”)
- VS Code formatter might reorganize based on settings (
format on save)
Syntax: Indentation
- Critical in Python, indicates blocks of code
- Example with
if statement:
if true:
print(“Hello, Osama”)
- Proper indentation required, otherwise syntax errors will occur
- All statements under the same block should maintain the same indentation level
Debugging and Errors
- Common to forget indentations; can lead to unexpected indent block errors
- VS Code helps identify incorrect indentations
Conclusion
- Recap of
print function and basic indentation rules
- Encouragement to practice and learn through continuous coding
- Preview of upcoming lessons
End of Lesson
السلام عليكم