- What is Python?
- Python is a popular programming language.
- Created by Guido van Rossum and released in 1991.
- Uses of Python:
- Web development (server-side).
- Software development.
- Mathematics.
- System scripting.
What can Python do?
- Python can be used on a server to create web applications.
- Python can be used alongside software to create workflows.
- Python can connect to database systems and also read and modify files.
- Python can handle big data and perform complex mathematics.
- Python is suitable for rapid prototyping or production-ready software development.
Why Python?
- Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.).
- Python has a simple syntax similar to the English language.
- Python’s syntax allows developers to write programs with fewer lines than many other programming languages.
- Python runs on an interpreter system, enabling immediate code execution (ideal for quick prototyping).
- Python supports different programming approaches:
- Procedural.
- Object-Oriented.
- Functional.
Good to Know:
- The most recent major version of Python is Python 3 (used in this tutorial).
- Python 2 is no longer updated except for security fixes but remains popular.
- Python code can be written in:
- Text editors.
- Integrated Development Environments (IDEs) such as Thonny, Pycharm, NetBeans, or Eclipse (useful for managing larger projects).
Python Syntax Compared to Other Programming Languages:
- Python was designed for readability with similarities to the English language and influences from mathematics.
- Python uses new lines to complete a command instead of semicolons or parentheses.
- Python relies on indentation (whitespace) to define scope (e.g., loops, functions, and classes), unlike other languages that use curly brackets.
Example Code
print("Hello, World!")
💥 YouTube https://www.youtube.com/channel/UCJojbxGV0sfU1QPWhRxx4-A
💥 Blog https://localedxcelcambridgeictcomputerclass.blogspot.com/
Here is a detailed list of all the points from the content:
Python Getting Started
Python Installation
- Many PCs and Macs come with Python pre-installed.
- To check if Python is installed:
- On Windows:
- Search in the Start menu for “Python.”
- Or run the command:
python --version
- On Linux/Mac:
- Open the command line or Terminal and type:
python --version
- If Python is not installed, download it for free from https://www.python.org/.
Python Quickstart
- Python is an interpreted programming language, meaning you write Python files (
.py
) in a text editor and execute them with the Python interpreter. - To run a Python file from the command line:
python helloworld.py
- Here,
helloworld.py
is the name of the Python file.
- Example of a simple Python program:
- Create a file named
helloworld.py
: print("Hello, World!")
- Save the file, open the command line, navigate to the directory where the file is saved, and run:
python helloworld.py
- Output:
Hello, World!
Python Version
- To check the Python version in the editor or your system:
import sys print(sys.version)
- You will learn more about importing modules in the Python Modules chapter.
The Python Command Line
- Python can be run directly from the command line, which is useful for testing small snippets of code.
- To start Python in the command line:
- For Windows, Mac, or Linux:
python
- If the
python
command does not work, try: py
- Example of running Python code directly in the command line:
- Start Python:
python
- Then enter:
print("Hello, World!")
- Output:
Hello, World!
- To exit the Python command line interface:
exit()
Execute Python Syntax
-
Executing Python Syntax in the Command Line
- Python code can be executed directly in the Command Line interface.
- Example:
>>> print("Hello, World!") Hello, World!
-
Executing Python Syntax via a File
- Create a
.py
file (e.g.,myfile.py
) and run it in the Command Line:C:\Users\Your Name>python myfile.py
- Create a
Python Indentation
-
Definition
- Indentation refers to the spaces at the beginning of a code line.
- While in other programming languages indentation is for readability only, in Python, it is mandatory and used to indicate a block of code.
-
Example of Proper Indentation
if 5 > 2: print("Five is greater than two!")
-
What Happens Without Indentation?
- Python will throw a Syntax Error if indentation is missing.
- Example:
if 5 > 2: print("Five is greater than two!") # Syntax Error
-
Number of Spaces
- The number of spaces for indentation is flexible but must be consistent within the same block of code.
- Common practice: Use 4 spaces.
- Examples of proper indentation:
if 5 > 2: print("Five is greater than two!") # 4 spaces if 5 > 2: print("Five is greater than two!") # More spaces (valid)
-
Error for Inconsistent Indentation
- Mixing different numbers of spaces in the same block leads to a Syntax Error.
- Example:
if 5 > 2: print("Five is greater than two!") # 4 spaces print("This will cause an error!") # 8 spaces (Syntax Error)
Python Variables
-
Creating Variables
- Variables are created when you assign a value to them.
- Example:
x = 5 y = "Hello, World!"
-
No Declaration Needed
- Python does not require a specific command to declare variables.
- You will learn more in the "Python Variables" chapter.
Python Comments
-
Purpose of Comments
- Comments are used for in-code documentation and to make code more readable.
-
How to Write Comments
- Start a comment with the
#
symbol. - Python ignores the rest of the line after the
#
.
- Start a comment with the
-
Example of Comments
# This is a comment. print("Hello, World!")
Purpose of Comments in Python
-
Explain Python Code
- Comments help clarify what the code does.
-
Improve Code Readability
- They make the code easier to understand for others (or your future self).
-
Prevent Code Execution
- Comments can be used to temporarily disable code for testing purposes.
Creating a Comment
-
Single-Line Comments
- Start with
#
, and Python will ignore the rest of the line. - Example:
# This is a comment print("Hello, World!")
- Start with
-
Comments at the End of a Line
- Add a comment after the code on the same line.
- Example:
print("Hello, World!") # This is a comment
-
Prevent Execution with Comments
- Use
#
to comment out lines of code. - Example:
# print("Hello, World!") print("Cheers, Mate!")
- Use
Multiline Comments
-
Using
#
for Each Line- Write a
#
symbol before each line of the comment. - Example:
# This is a comment # written in # more than just one line print("Hello, World!")
- Write a
-
Using Multiline Strings as Comments
- Use triple quotes (
"""
or'''
) to create a multiline string. - As long as the string is not assigned to a variable, Python will treat it as a comment.
- Example:
""" This is a comment written in more than just one line """ print("Hello, World!")
- Use triple quotes (
Exercise
- Question: Which character is used to define a Python comment?
- Correct Answer:
#
- Correct Answer:
No comments:
Post a Comment