Python Day 1
Python Day 1
Python Day 1
Notes
Introduction to Python
Overview of Python:
Python is a high-level, interpreted programming language known for its readability and simplicity. It
supports multiple programming paradigms, including procedural, object-oriented, and functional
programming. Python has a large standard library and an active community, making it suitable for a
wide range of applications.
History of Python:
Python was created by Guido van Rossum and first released in 1991. The language was named after
the British comedy series "Monty Python's Flying Circus," not the snake. Python 2.0 was released in
2000, introducing many new features, including garbage collection and list comprehensions. Python
3.0 was released in 2008, with significant changes and improvements, making it incompatible with
Python 2.x.
Features of Python:
- Readable and Maintainable Code: Python’s syntax is designed to be readable and straightforward.
- Dynamic Typing: Variables in Python do not need explicit declaration to reserve memory space.
- Interpreted Language: Python code is executed line by line, making debugging easier.
- Extensive Standard Library: Python's standard library supports many common programming tasks.
- Open Source and Community Development: Python is developed under an open-source license,
making it free to use and distribute.
- Portability: Python can run on various hardware platforms and has the same interface on all
platforms.
- GUI Programming Support: Python supports GUI applications that can be created and ported to
many system calls, libraries, and windows systems.
- Integration: Python can easily integrate with C, C++, COM, ActiveX, CORBA, and Java.
Installing Python:
Python can be installed from the official Python website (https://www.python.org/). For Windows, a
Windows installer is provided. For macOS and Linux, Python can be installed via package managers
like `brew` for macOS and `apt` or `yum` for Linux.
- Interactive Mode: Useful for testing and debugging code snippets. Start the Python interpreter by
typing `python` or `python3` in the command line.
- Script Mode: Useful for writing longer programs. Python scripts are saved with a `.py` extension and
can be executed by typing `python script_name.py` or `python3 script_name.py` in the command line.
Basic Syntax
Python code is organized into modules, which are files containing Python definitions and statements.
The basic elements of a Python program include expressions, statements, functions, and classes.
Indentation:
Python uses indentation to define the scope of loops, functions, and classes. Consistent indentation
is crucial. Indentation level is indicated using spaces or tabs, but mixing spaces and tabs is not
allowed.
```python
if True:
```
Comments:
Comments are used to explain code and are ignored by the interpreter. Single-line comments start
with the `#` symbol. Multi-line comments can be written using triple quotes (`'''` or `"""`).
```python
"""
"""
```
Variables in Python are dynamically typed, meaning you don’t have to declare their type. Common
data types include:
- Strings: `str`
- Boolean: `bool`
- Mappings: `dict`
- None: `NoneType`
```python
x = 10 # Integer
y = 3.14 # Float
```
Input and Output:
The `input()` function reads a line from the input (usually from the user), and the `print()` function
prints output to the console.
```python
```
Operators:
- Arithmetic Operators: `+`, `-`, `*`, `/`, `%`, `**` (exponentiation), `//` (floor division)
- Assignment Operators: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `//=`
```python
num1 = float(num1)
num2 = float(num2)
```
Task 2
Here are some questions related to the notes on Introduction to Python and Basic Syntax:
Introduction to Python
1. What are the key features of Python that make it a popular programming language?
4. What are the major differences between Python 2.x and Python 3.x?
6. What is the difference between interactive mode and script mode in Python?
Basic Syntax
5. What are some common data types in Python, and how are they used?
8. How do you read input from the user in Python? Provide an example.
10. List and describe the different types of operators available in Python.
11. Write a Python program that reads two numbers from the user, adds them, and prints the
result.
12. What are the differences between the following operators in Python: `==` and `=`, `and` and
`&`, `is` and `==`?