Python is a high-level, interpreted programming language created by Guido van Rossum and first released in 1991. Designed with an emphasis on code readability and simplicity, Python allows developers to express concepts in fewer lines of code compared to other languages like Java or C++. Its versatility and ease of use have led to widespread adoption across various domains, including:
- Web Development: Frameworks like Django and Flask facilitate the creation of robust web applications.
- Data Analysis and Machine Learning: Libraries such as Pandas, NumPy, and scikit-learn are used for data manipulation and building machine learning models.
- Automation: Python scripts can automate repetitive tasks, enhancing efficiency.
- Software Development: It’s employed in developing both desktop and mobile applications.
- Scientific Computing: Tools like SciPy support complex scientific calculations.
Python’s extensive standard library and supportive community contribute to its status as one of the most popular programming languages worldwide.
Python’s extensive standard library and supportive community contribute to its status as one of the most popular programming languages worldwide.
Python is a high-level, interpreted programming language renowned for its simplicity and versatility. Its key features include:
- Easy to Learn and Read: Python’s clean and straightforward syntax emphasizes readability, making it accessible for beginners and reducing the cost of program maintenance.
- Object-Oriented: Python supports object-oriented programming (OOP), allowing developers to create classes and objects, which promotes modular and reusable code.
- Dynamically Typed: In Python, variable types are determined at runtime, providing flexibility in coding.
- Extensive Standard Library: Python offers a vast standard library that supports many common programming tasks, such as file I/O, system calls, and even Internet protocols.
- Cross-Platform Compatibility: Python code can run on various operating systems, including Windows, mac OS, and Linux, without modification.
- Interpreted Language: Python code is executed line by line by the interpreter, which simplifies debugging and enhances development speed.
- GUI Programming Support: Python supports the development of graphical user interfaces through libraries such as ‘Tkinter’, ‘PyQt’, and ‘Kivy’, facilitating the creation of user-friendly desktop applications.
- Automatic Memory Management: Python manages memory automatically through a built-in garbage collector, reducing the likelihood of memory leaks and errors.
- Extensibility and Integration: Python can be extended with modules written in languages like C or C++, and it can integrate with other languages, enhancing its versatility.
- Large Community Support: Python boasts a large and active community, providing a wealth of resources, tutorials, and forums for support.
- Versatile Applications: Python is used in various domains, including web development, data analysis, artificial intelligence, machine learning, automation, and scientific research.
These features contribute to Python’s widespread adoption and its reputation as a powerful and user-friendly programming language.
These features contribute to Python’s widespread adoption and its reputation as a powerful and user-friendly programming language.
These features contribute to Python’s widespread adoption and its reputation as a powerful and user-friendly programming language.
For Example, we create a simple “Hello World“ Program in Python.
Write the Program: Open a text editor and type the following code:
Print (“Hello, World!”)
This code uses Python’s built-in print() function to display the string “Hello, World!” on the screen.
Save the Program: Save the file with a .py extension, for example, hello_world.py
Execution Mode of Python:
Python offers two primary execution modes for running code:
1. Interactive Mode:
- Description: Provides an interactive environment where Python statements are executed line by line, offering immediate feedback.
- Usage: Ideal for testing small code snippets, performing quick calculations, or experimenting with Python features.
- How to Access:
Open a terminal or command prompt.
Type python or python3 (depending on your installation) and press Enter.
You will see a prompt like >>>, indicating that you can start typing Python commands.
Example:
>>> 2 + 3
5
>>> print (“Hello, Python!”)
Hello, Python!
2. Script Mode:
- Description: Allows writing Python code in a file (typically with a .py extension) and executing the entire script.
- Usage: Suitable for developing larger programs, automating tasks, or when code needs to be saved for future use.
- Create a new file using a text editor (e.g., Notepad).
- Write your Python code in the file and save it with a .py extension, e.g., script.py.
- Open a terminal or command prompt.
- Navigate to the directory where your script is saved.
- Run the script by typing python script.py and pressing Enter.
Feature | Interactive Mode | Script Mode |
Execution | Executes code line by line with immediate feedback. | Executes the entire script after reading it completely. |
Usage | Suitable for short tasks, testing, and learning. | Ideal for developing larger applications and saving code for future use. |
Code Saving | Does not save code; each session is independent. | Code is saved in .py files and can be reused or modified later. |
Environment | Provides a REPL (Read-Eval-Print Loop) environment for dynamic execution. | Runs in a standard script execution environment without the interactive prompt. |
1. Categories of Python Tokens:
a. Keywords:
These are reserved words in Python that have special meaning. You can’t use them as variable names.
Example keywords:
if, else, for, while, def, return, class, import, try, except, with, as, lambda, pass, break, continue, global, nonlocal, assert, yield, async, await.
b. Identifiers :
These are names used to identify variables, functions, classes, modules, etc.
Rules:
a) Must start with a letter (A-Z or a-z) or underscore (_)
b) Can be followed by letters, digits (0-9), or underscores
3) Case-sensitive (e.g., myVar ≠ MyVar)
Example:
name =”Alice”
def greet(): pass
c. Literals
These are constant values used in code.
Types:
a) String literals: “hello”, ‘world’, “””multiline”””
b) Numeric literals: 123, 3.14, 0b1010 (binary), 0xFF (hex)
c) Boolean literals: True, False
d) Special literal: None
d.Operators
Symbols that perform operations on variables and values.
Examples:
a) Arithmetic: +, -, *, /, //, %, **
Comparison: ==, !=, >, <, >=, <=
b) Logical: and, or, not
c) Bitwise: &, |, ^, ~, <<, >>
d) Assignment: =, +=, -=, etc.
e. Punctuators / Delimiters
These separate statements define the code structure.
Examples:
a) Brackets: (), {}, []
b) Comma,, colon :, period .
c) Semicolon ;
d) At symbol @
e) Equals =, arrow ->
If you’re diving into how the Python interpreter tokenizes code
import tokenize
fromio import BytesIO
code = b’fori in range(3): print(i)’
tokens = tokenize.tokenize(BytesIO(code).readline)
for token in tokens:
print(token)