Introduction to Python
Introduction to Programming
Programming is the process of creating a set of instructions that tell a computer how to perform a task.
A program is a sequence of instructions written in a programming language that the computer can
understand and execute. Programming allows us to make computers solve problems, perform
calculations, and automate tasks efficiently.
Examples of programming languages include Python, C, J ava, and JavaScript.
Introduction to Python
Python is a high-level, interpreted programming language developed by Guido van Rossum in 1991. It
is known for its simplicity and readability, making it ideal for beginners. Python is used in various fields
such as web development, data science, artificial intelligence, and automation.
Features of Python
• Simple and Easy to Learn: Python is very easy to read and write. The commands look like English
words, so it is simple for beginners to understand.
• Flexible: Python can be used for many purposes — like making games, websites, mobile apps, and
doing Artificial Intelligence (AI).
• Large Library Support: Python has many built-in libraries that help in doing different tasks
quickly, like maths, graphics, and data handling.
• Platform Independent: Python works on any operating system such as Windows, Mac, or Linux.
We can write code on one computer and run it on another without changing anything.
• Extendable and Great Community Support: Python allows adding extra tools and has a large
online community of programmers who help each other by sharing knowledge.
• Supports Many Databases: Python can easily connect with different databases like MySQL, MS-
Access to store and manage data.
Advantages of Python
• Easy to learn and use: Python uses simple English-like words. It is beginner-friendly and easy to read
and write.
• Free and open source: Python is completely free to download and use. Anyone can improve or share
its code.
• Portable language: Python programs can run on any computer (Windows, Mac, Linux) without
changes.
• Large library support: It has many built-in modules and packages to do different tasks — like
math, graphics, or games.
• Object-oriented and powerful: Python supports object-oriented programming, which makes big
programs easy to manage.
• Used in many fields: Python is used in AI, data science, web development, robotics, and
automation.
Disadvantages of Python
• Slower speed: Python runs slower than some other languages (like C++ or Java) because it is
interpreted line by line.
• Not great for mobile apps: Python is rarely used for creating mobile applications.
• Takes more memory: Python uses more memory, so it’s not always suitable for very large
programs or devices with low storage.
• Runtime errors: Because it’s easy to write, beginners may make small mistakes that cause errors
while running.
TOKENS
The smallest part of a Python program that has a meaning is called a token. Every program is made up
of these tokens. The main types of tokens in Python are: identifiers, keywords, operators, delimiters, and
literals. We must give spaces between two words (like identifiers or keywords), otherwise Python will
read them as one single word by mistake.
Identifiers
An identifier is a name (Employee, salary, roll_no, address1, address2) used to identify a variable,
function, class, module, or any other object. An identifier starts with a letter (A to Z or a to z) or an
underscore (_) followed by zero or more letters, underscores, and digits (0 to 9). Python is case sensitive
which means: lowercase and uppercase letters are different.
Keywords
Python keywords are special reserved words that
have specific meanings and purpose. They cannot
be used as a function or variable name. All the
keywords in python are written in lower case
expect True, False and None.
Variables
A variable is a reference to a location in memory which can hold a value of a specific type.
Example: x = 10 y=5 price = 200 message = ''be happy”
The variable message stores a string value, which contains text. That’s why its value is written inside
double quotes (or single quotes can also be used).
Data Types in Python
The data stored in a variable can be of many types. For example, a student's roll number is stored as a
numeric value and his or her address is stored as alphanumeric characters. Python has various standard
data types that are used to define the operations possible on them and the storage method for each of
them.
Python has five standard data types:
* Numeric
* Set
* Boolean
* Sequence
* Dictionary
1. Numeric Type:
Number data types store numeric values. Number objects are created when you assign a value to
them. There are several types of numeric types in Python:
• Integer: It is a whole number with no decimal places. Example: int = 5 , var1 = 1 , x = 30
• Float: A number with decimal places. Example: b = 3.14 , y = 2.5
• Complex: It consists of a real part and an imaginary part, represented in the form: a+bj
2. Set Type:
A set is a collection of unique items (no duplicates). They are unordered and mutable, but
elements can’t be changed individually. Example: numbers = {1, 2, 3, 3, 2, 4}
3. Boolean Type:
If we compare two values, the expression is evaluated and Python returns the boolean value
(True or False). Example: print (10 > 9) = True
print (10 < 9) = False
4. Sequence Type:
Sequence is the ordered collection of similar or different data types. Sequences allow storing
multiple values in an organized and efficient way. There are several sequence types in Python:
• String: It is identified as a contiguous set of characters represented in the quotation marks.
Example: str = ''Hello World”
• List: It is used to store multiple values in a single variable. It is declared with in square
brackets and it is mutable. Example: fruits = ['apple', 'banana', 'cherry']
• Tuple: It is ordered and immutable in nature, although duplicate entries can be there inside
a tuple. It is used with parentheses or round brackets. Example: tuple1 = (10, 20, 25)
The main difference between tuple and list is that tuples are immutable objects, whereas
lists are mutable objects.
5. Dictionary Type:
It stores data in the form of key-value pairs and is mutable, meaning its contents can be changed
after creation. Example: student = {'name': 'John', 'age': 14}
Operators in Python
Operators are symbols used to perform operations on variables and values.
a) Arithmetic Operators: They are used to perform mathematical operations.
b) Assignment Operators: They are used to assign values to variables.
c) Comparison Operators: They are used to compare two variables. The output will be True or False.
d) Logical Operators: They are used with conditional statements.
e) Identity Operators: They are used to determine whether a value is of certain class or type. They
are used to compare the objects.
f) Membership Operators: They are used to test if an object is present in a sequence or not.
g) Bitwise Operators: The integers are first converted into binary form and then operations are
performed on bits. The result is returned in decimal format.
**************