An Introduction to Python
1. History of Python:
Python was created by Guido van Rossum and first released in 1991. It is designed
as a high-level, interpreted(interpreter translates and executes the code line by line)
programming language readability and simplicity. Python's syntax allows
programmers to express concepts in fewer lines of code compared to languages
like C++ or Java.
Why Python?
● Python works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc).
● Python runs on an interpreter system, meaning that code can be executed as
soon as it is written. This means that prototyping can be very quick.
● Python can be treated in a procedural way, an object-oriented way or a
functional way.
2. Features of Python:
● Readable and Simple Syntax: Python code is designed to be easily
readable and expressive.
● Interpreted and Dynamic Typing: Python is interpreted at runtime, making
development faster. It supports dynamic typing, which means variables don't
need to be declared before use.(my_var = 42 print(my_var) # Output: 42)
● Rich Standard Library: Python comes with a large standard library that
supports many common programming tasks, from string manipulation to
networking.
● Cross-platform: Python runs on various platforms such as Windows,
macOS, and Linux, making it highly portable.
● Extensible and Embeddable: Python can be extended by writing C or C++
code and can be embedded within C/C++ programs.
● Large Community and Ecosystem: Python has a vast community and
ecosystem of libraries and frameworks for various applications like web
development, scientific computing, machine learning, etc.
● Object-Oriented: Python supports Object Oriented Programming (OOP)
concepts that encapsulate code within objects. All concepts in OOPs like
data hiding,Operator Overloading, inheritance etc.
3. Applications of Python:
Python is versatile and widely used in various domains, including:
● Web Development: Frameworks like Django and Flask are popular for
building web applications.
● Data Science and Machine Learning: Libraries like NumPy, Pandas,
SciPy, and scikit-learn are widely used in data analysis and machine
learning.
● Scripting: Python is excellent for writing scripts to automate tasks.
● Game Development: Python can be used for game development with
libraries like Pygame.
● Desktop GUI: Libraries like Tkinter and PyQt allow developers to create
desktop applications.
● Embedded Systems: Python is used for programming embedded systems
and IoT devices.
● Education: Python is widely used as a teaching language due to its
simplicity and readability.
4. Installing Python:
To install Python on your system, follow these steps:
● Go to the official Python website and download the installer for your
operating system.
● Run the installer and follow the prompts to install Python. Make sure to
check the option to add Python to your PATH during installation.
5. Running a Simple Python Program:
Once Python is installed, you can write and run a simple program. Here’s an
example of a "Hello, World!" program
python
Copy code
print("Hello, World!")
To run this program:
● Save the code in a file with a .py extension, for example, [Link].
● Open a terminal or command prompt.
● Navigate to the directory where [Link] is saved.
● Type python [Link] and press Enter.
This will execute the Python script and print "Hello, World!" to the console.
Data Types
Python's simplicity, versatility, and robust community support have contributed to its popularity
among developers worldwide.
1. Numbers: Represents numeric data to perform mathematical operations.
Numbers can be integers (like 1 and 2), floats (like 1.1 and 1.2), fractions (like 1/2
and 2/3), or even complex numbers.
2. String: Represents text characters, special symbols or alphanumeric data. String is a sequence
of Unicode characters.
x = "Hello World"
3. List: Represents sequential data that the programmer wishes to sort, merge etc.
x = ["apple", "banana", "cherry"]
4. Tuple: Represents sequential data with a little difference from list.
x = ("apple", "banana", "cherry")
5. Dictionary: Represents a collection of data that associate a unique key with each
Value.
x = {"name" : "John", "age" : 36}
6. Boolean: Represents truth values (true or false).
>>> size = 1
>>> size < 0
False
7. Set: Set is an unordered collection of unique data items.
x = {"apple", "banana", "cherry"}
8. Dictionary: Dictionary is an unordered collection of key-value pairs.
# Numeric Types
a = 10 # int
b = 3.14 # float
a = complex(2, 3) # Creates a complex number 2 + 3j
b = 4 + 5j # Another way to create a complex number
print(a) # Output: (2+3j)
print(b) # Output: (4+5j)
# Sequence Types
d = "Hello" # str
e = [1, 2, 3] # list
f = (4, 5, 6) # tuple
# Mapping Type
g = {"key1": "value1", "key2": "value2"} # dict
# Set Types
h = {1, 2, 3} # set
i = frozenset([1, 2, 3]) # frozenset
# Boolean Type
j = True # bool
# None Type
k = None # NoneType
Variables
Variables are containers for storing data values.
age = 25 # Integer variable
height = 5.9 # Float variable
grade = 'A' # String variable (since Python doesn't have a separate char type)
name = "Alice" # String variable
Variable Names
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
● A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
● Variable names are case-sensitive (age, Age and AGE are three different
variables)
Constants in Python
PI = 3.14 # Floating-point constant
GRADE = 'A' # Character constant
NAME = "Alice" # String constant
print("PI:", PI)
print("Grade:", GRADE)
print("Name:", NAME)
Identifier:A value-holding Python variable is also known as an identifier.
Must begin with a letter (a-z, A-Z) or an underscore (_):
● Correct: age, _age, Age
● Incorrect: 1age, @age
Subsequent characters can be letters, digits (0-9), or underscores:
● Correct: age1, my_var, _temp
● Incorrect: my-var, my var
Case-sensitive (most languages): Identifiers age, Age, and AGE would be
considered different identifiers.
No spaces or special characters (except the underscore):
● Correct: my_var, temp2
● Incorrect: my var, temp-2
Types of Comments
Single-line Comments
Single-line comments are used for brief explanations or notes and occupy only one
line.
Python: Use #
age = 25 # This is a single-line comment
print("Age:", age) # Print the age
Multi-line Comments
Multi-line comments are used for longer explanations that span multiple lines.
age = 25
# This is a multi-line comment
# that spans multiple lines
print("Age:", age)
"""
This is a multi-line comment
using triple quotes. However,
This is actually a docstring.
"""
Python Operators
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Bitwise operators
● Arithmetic operators
Assignment Operators
result = 2 ** 3 # 2 raised to the power of 3
print(result) # Output: 8
remainder = 10 % 3
print(remainder) # Output: 1
result = 7 // 2
print(result) # Output: 3
Types of Comments
Single-line Comments
Single-line comments are used for brief explanations or notes and occupy only one
line.
Python: Use #
age = 25 # This is a single-line comment
print("Age:", age) # Print the age
Multi-line Comments
Multi-line comments are used for longer explanations that span multiple lines.
age = 25
# This is a multi-line comment
# that spans multiple lines
print("Age:", age)
"""
This is a multi-line comment
using triple quotes. However,
This is actually a docstring.
"""
Assignment Operators
Dry Run
● A dry run is the process of a programmer manually working through their code to trace
the value of variables.
● Dry run is a manual way of testing for a process or algorithms for its correctness and
functionality.
● In the dry run method a table is prepared with different columns for each variable used in
the program and the values of each variable are updated in the table as we proceed
through the algorithm, one step at a time.
● Success of the dry run method depends upon the ability to step through the instructions
exactly as a computer would execute them.
# Get the number from the user
number = int(input("Enter the number : "))
# Print the header
print("The Multiplication Table of:", number)
# Using a for loop to iterate the multiplication 10 times
for count in range(1, 11):
print(number, 'x', count, '=', number * count)