Table of Contents

Python Programming Language

 

Chapter 1: Introduction to Python

What is Python?

Python is a high-level, interpreted programming language known for its simplicity and readability. It was created by Guido van Rossum and released in 1991.

Python is used in a variety of applications including web development, data analysis, artificial intelligence, scientific computing, and more.

History of Python

  • Python was conceived in the late 1980s as a successor to the ABC language.
  • The first version, Python 1.0, was released in January 1994.
  • Python 2.0 introduced new features such as list comprehensions and garbage collection in 2000.
  • Python 3.0, a major revision, was released in 2008 to address and fix fundamental design flaws of the language.

Features of Python

  • Simple and Easy to Learn: Python has a clean and straightforward syntax.
  • Interpreted Language: Python code is executed line-by-line, which makes debugging easier.
  • Cross-Platform: Python runs on various operating systems like Windows, macOS, and Linux.
  • Extensive Libraries: Python has a rich set of libraries and frameworks that facilitate many tasks.
  • Open Source: Python is freely available to use and distribute.
  • Community Support: Python has a large and active community that contributes to its growth and development.

Setting Up the Python Environment

Installing Python

Go to the official Python website and download the latest version of Python for your operating system. Follow the installation instructions specific to your OS.

Integrated Development Environment (IDE)

You can write Python code in any text editor, but using an IDE like PyCharm, VS Code, or IDLE can enhance productivity with features like syntax highlighting, debugging tools, and project management.

Writing Your First Python Program

Hello, World!

Open your text editor or IDE and write the following code:

print("Hello, World!")

Save the file with a .py extension, for example, hello.py.

Open a terminal or command prompt, navigate to the directory where the file is saved, and run the program using the command:

python hello.py

You should see the output: Hello, World!

Using the Python Interpreter

Interactive Mode

You can use the Python interpreter in interactive mode by simply typing python or python3 in your terminal or command prompt. In this mode, you can type Python commands and see immediate results.

$ python
Python 3.x.x (default, Date, Time)
[GCC x.x.x] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!
>>> 2 + 3
5
>>> exit()

Script Mode

In script mode, you write your code in a file and execute the file using the Python interpreter. This mode is useful for writing and running more complex programs.

Conclusion

Python is a versatile and powerful programming language suitable for beginners and experienced programmers alike. The simplicity and readability of Python make it an excellent choice for learning programming.

Setting up Python and writing basic programs are the first steps in your journey to mastering Python programming. Make sure to practice writing and running simple Python programs to get comfortable with the language.

Chapter 2: Basic Syntax

Python Keywords and Identifiers

Keywords: Reserved words in Python that have special meanings. They cannot be used as identifiers (names of variables, functions, etc.). Examples include if, else, while, for, break, continue, def, return, class, try, except, finally, import, from, as, with, pass, yield, and global.

Identifiers: Names given to variables, functions, classes, etc. Rules for identifiers:

  • Must begin with a letter (a-z, A-Z) or an underscore (_).
  • Followed by letters, underscores, or digits (0-9).
  • Case-sensitive (myVar and myvar are different).

Variables and Data Types

Variables: Containers for storing data values. Assignment is done using the = operator.

Example:

x = 5
y = "Hello"

Data Types: Types of values that can be stored in variables. Common data types include:

  • Numeric Types: int, float, complex
    • int: Integer values (e.g., 1, -2, 100)
    • float: Floating-point numbers (e.g., 3.14, -0.001)
    • complex: Complex numbers (e.g., 1 + 2j)
  • Text Type: str
    • Strings (e.g., “Hello”, ‘Python’)
  • Sequence Types: list, tuple, range
    • list: Ordered, mutable collections (e.g., [1, 2, 3])
    • tuple: Ordered, immutable collections (e.g., (1, 2, 3))
    • range: Immutable sequences of numbers (e.g., range(5))
  • Mapping Type: dict
    • Dictionaries, key-value pairs (e.g., {‘name’: ‘Alice’, ‘age’: 25})
  • Set Types: set, frozenset
    • set: Unordered collections of unique elements (e.g., {1, 2, 3})
    • frozenset: Immutable sets (e.g., frozenset([1, 2, 3]))
  • Boolean Type: bool
    • Boolean values True or False
  • None Type: None
    • Represents the absence of a value or a null value

Basic Input and Output Operations

Input

Reading user input using the input() function.

Example:

name = input("Enter your name: ")
print("Hello, " + name)

Output

Displaying output using the print() function.

Example:

print("Hello, World!")

Print multiple values:

print("The answer is", 42)

Comments in Python

Single-line Comments

Use the # symbol.

Example:

# This is a single-line comment
print("Hello, World!")  # This is also a comment

Multi-line Comments

Use triple quotes (''' or """).

Example:

"""
This is a multi-line comment
spanning multiple lines.
"""
print("Hello, World!")

Indentation in Python

Python uses indentation to define the scope of loops, functions, classes, and other code blocks. Consistent indentation is crucial; typically, four spaces are used per indentation level.

Example:

if True:
    print("This is inside an if block")
if False:
    print("This won't be printed")
print("This is outside the if block")

Conclusion

Understanding the basic syntax of Python is essential for writing correct and readable code. Familiarize yourself with keywords, identifiers, variables, data types, input/output operations, comments, and indentation. Practice writing small programs to reinforce these concepts.

 
Scroll to Top