Python Day 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Python

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.

Running Python Programs:

- 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

Structure of a Python Program:

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:

print("Hello, World!") # Correct indentation

# print("This will cause an error") # Incorrect indentation

```

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

# This is a single-line comment

"""

This is a multi-line comment

spanning multiple lines.

"""

```

Variables and Data Types:

Variables in Python are dynamically typed, meaning you don’t have to declare their type. Common
data types include:

- Numbers: `int`, `float`, `complex`

- Strings: `str`

- Boolean: `bool`

- Sequences: `list`, `tuple`, `range`

- Mappings: `dict`

- Sets: `set`, `frozenset`

- None: `NoneType`

```python

x = 10 # Integer

y = 3.14 # Float

name = "Alice" # String

is_active = True # Boolean

```
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

name = input("Enter your name: ")

print("Hello, " + name)

```

Operators:

- Arithmetic Operators: `+`, `-`, `*`, `/`, `%`, `**` (exponentiation), `//` (floor division)

- Relational Operators: `==`, `!=`, `>`, `<`, `>=`, `<=`

- Logical Operators: `and`, `or`, `not`

- Bitwise Operators: `&`, `|`, `^`, `~`, `<<`, `>>`

- Assignment Operators: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `//=`

- Membership Operators: `in`, `not in`

- Identity Operators: `is`, `is not`

Example of a Simple Python Program:

```python

# This program adds two numbers provided by the user

Get input from the user

num1 = input("Enter first number: ")

num2 = input("Enter second number: ")

Convert inputs to floats

num1 = float(num1)
num2 = float(num2)

Calculate the sum

sum = num1 + num2

Display the sum

print("The sum is:", sum)

```

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?

2. Who created Python, and in what year was it first released?

3. Why was Python named "Python"?

4. What are the major differences between Python 2.x and Python 3.x?

5. Describe the process of installing Python on a Windows operating system.

6. What is the difference between interactive mode and script mode in Python?

7. How do you start the Python interpreter in interactive mode?

8. How do you execute a Python script in script mode?

9. What is the role of the Python Standard Library?

10. Explain the concept of dynamic typing in Python.

Basic Syntax

1. How is the structure of a Python program typically organized?


2. Why is indentation important in Python, and what is the consequence of incorrect
indentation?

3. Provide an example of a correctly and incorrectly indented block of code in Python.

4. How do you write a single-line comment and a multi-line comment in Python?

5. What are some common data types in Python, and how are they used?

6. Write a Python statement to declare and initialize an integer variable.

7. Write a Python statement to declare and initialize a string variable.

8. How do you read input from the user in Python? Provide an example.

9. How do you print output to the console 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 `==`?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy