INTRODUCTION TO PYTHON
History and evolution
Installing Python and setting up the environment
Your first Python program
PYTHON
Python is an interpreted and object-oriented programming language.
Interpreted: -
Interpreted in simple terms means running code line by line. It also
means that the instruction is executed without earlier compiling the whole
program into machine language.
The source code of a python program is converted into byte code that is
then executed by the python virtual machine.
Bytecode: It is type of a code that is intermediate from source code and
machine code.
(or)
It is computer object code that an interpreter converts into binary
machine and so it can be read by a computer ‘s hardware processor.
Object oriented programming language
Because it uses classes, objects and other oop concepts.
1
History and evolution of PYTHON
Python was created by Guido van Rossum and first released in 1991. It
was designed to emphasize code readability and simplicity. Over the
years, Python has evolved through major versions:
Python 1.x: Initial release focused on basic functionality.
Python 2.x: Introduced significant improvements but eventually
deprecated.
Python 3.x: Current version with modern features and ongoing support.
Python's widespread adoption is due to its versatility and the active
development of its ecosystem.
Installing Python and Setting Up the Environment
To start coding in Python, follow these steps:
1. Download Python: Visit the official Python website and download
the latest version compatible with your operating system.
2. Install Python: Follow the installation instructions for your
platform. Ensure you check the box to add Python to your system's
PATH during installation.
3. Set Up an IDE: Use an Integrated Development Environment (IDE)
like PyCharm or VSCode, or start with the default IDLE that comes
with Python.
4. Verify Installation:
o Open a terminal or command prompt.
o Type python --version or python3 --version to check the
installed version.
Your First Python Program
Write and run your first Python program:
1. Open your IDE or text editor.
2. Create a new file named [Link].
3. Add the following code:
print("Hello, World!")
Save the file and run it:
In the terminal, navigate to the file's directory and type python [Link].
Congratulations! You've written your first Python program.
2
Basic Syntax and Data Types
Variables and data types
Operators
Input and output
Variables and Data Types
Variables in Python are used to store data values. Python is dynamically
typed, meaning you don't need to declare the type of a variable explicitly.
Example:
# Variable declaration
x = 10 # Integer
y = 3.14 # Float
name = "Alice" # String
is_active = True # Boolean
print(x, y, name, is_active)
Common Data Types:
1. int: Integer values (e.g., 5, -10).
2. float: Decimal values (e.g., 3.14, -0.5).
3. str: Text data (e.g., "Hello").
4. bool: Boolean values (True or False).
5. list: Ordered collection (e.g., [1, 2, 3]).
6. tuple: Immutable ordered collection (e.g., (1, 2, 3)).
7. dict: Key-value pairs (e.g., {"name": "Alice", "age": 25}).
8. set: Unordered unique elements (e.g., {1, 2, 3}).
Operators
Python provides a variety of operators to perform operations on variables:
1. Arithmetic Operators: +, -, *, /, %, **, //
a = 10
b=3
print(a + b) # Addition
print(a ** b) # Exponentiation
2. Comparison Operators: ==, !=, <, >, <=, >=
print(a > b) # True
3
print(a == b) # False
3. Logical Operators: and, or, not
print(a > 5 and b < 5) # True
print(not (a > b)) # False
4. Assignment Operators: =, +=, -=, *=, /=, %=
x=5
x += 3 # x = x + 3
print(x) # 8
Input and Output
Python allows user input and provides flexible output formatting.
Example:
# Input
name = input("Enter your name: ")
print("Hello, " + name + "!")
# Output formatting
age = 25
print(f"{name} is {age} years old.")
***************