What is Python Programming?

Python is a high-level, easy-to-read programming language used to create software, websites, mobile applications, games, artificial intelligence (AI), data analysis applications, and many other types of programs.

Why is Python Called Versatile?

Python is called versatile because it can be used for many different types of work. Instead of learning a different programming language for every task, you can use Python for a wide variety of applications.

  • Web Development
  • Artificial Intelligence (AI)
  • Machine Learning
  • Data Science
  • Data Analysis
  • Automation
  • Game Development
  • Desktop Applications
  • Mobile App Development
  • Cybersecurity
  • Web Scraping
  • Cloud Computing
  • Internet of Things (IoT)
  • Robotics
  • Scientific Computing
  • Software Development
  • Networking
  • Blockchain
  • Image Processing
  • Computer Vision

What is Computer Programming?

Computer programming is the process of writing instructions (called code) that tell a computer what to do and how to do it.

A computer cannot think or make decisions on its own. It only follows the instructions given by a programmer.

It is also called coding, programming, or software in everyday language.

Setting Up Python Development Environment

Setting up a Python development environment means installing and configuring the tools needed to write and run Python programs.

Examples of Python Development Environments

These tools are commonly called Integrated Development Environments (IDEs).

  • Visual Studio Code (VS Code)
  • PyCharm
  • Google Colab (Colab)
  • Jupyter Notebook
  • IDLE
  • Spyder

What is Google Colab?

Google Colab is a cloud-based Python development environment. It lets you write and run Python code directly in your web browser, so you do not need to install Python on your computer.

Python Comments

What are Comments?

Comments are notes written inside a Python program to explain the code. They are ignored by the Python interpreter, so they are not executed.

Purpose of Comments

Comments are used to:

  • Explain what the code does.
  • Make the code easier to read and understand.
  • Help other programmers understand the program.
  • Remind yourself why you wrote a particular piece of code.

Types of Comments

Python mainly has two types of comments:

1. Single-Line Comment

A single-line comment starts with the # symbol.

Syntax
# This is a single-line comment
Example
# Print a welcome message
print("Welcome")

2. Multi-Line Comment

Python does not have a special multi-line comment symbol. A common way is to use triple quotes (''' or ''') to write comments that span multiple lines.

Example
"""
This program
prints a welcome
message.
"""

print("Welcome")

Variables in Python

What is a Variable?

A variable is a named location in memory that is used to store data. The value stored in a variable can be used or changed during the execution of a program.

Python Variables Example


x = 55
y = 66.6
name = "Salman"
age = 28
is_student = True

print(x)
print(y)
print(name)
print(age)
print(is_student)

Output:


55
66.6
Salman
28
True

Rules for Naming Variables

Python has some rules for naming variables.

1. Variable names must start with a letter or an underscore (_).

βœ… Correct


name = "Ali"
_age = 20

❌ Incorrect


2name = "Ali"

2. Variable names can contain letters, numbers, and underscores.

βœ… Correct


student1 = "Ahmed"
total_marks = 450

❌ Incorrect


student-name = "Ahmed"

(- is not allowed.)

3. Variable names cannot contain spaces.

βœ… Correct


first_name = "Ali"

❌ Incorrect


first name = "Ali"

4. Variable names are case-sensitive.

This means uppercase and lowercase letters are treated as different names.


name = "Ali"
Name = "Ahmed"

print(name)
print(Name)

Output:


Ali
Ahmed

name and Name are two different variables.

5. Do not use Python keywords as variable names.

❌ Incorrect


if = 10
class = 5

These words are reserved by Python.

Note: Reserved words (keywords) are predefined words in Python that have a special meaning. They cannot be used as variable names, function names, or class names.

Types of Variables in Python

In Python, variables are commonly classified based on the type of value they store.

1. Integer (int)

An integer stores whole numbers (without decimal points).

Example


age = 20
marks = 95

Explanation

20 and 95 are integers because they are whole numbers.

2. Float (float)

A float stores decimal numbers.

Example


height = 5.8
price = 99.99

Explanation

5.8 and 99.99 are floats because they contain decimal points.

3. String (str)

A string stores text. Text is written inside single (' ') or double (" ") quotes.

Example


name = "Ali"
city = 'Lahore'

Explanation

"Ali" and 'Lahore' are strings because they contain text.

4. Boolean (bool)

A boolean stores only two values:

  • True
  • False

Example


is_student = True
is_logged_in = False

Explanation

  • True means Yes or Correct.
  • False means No or Incorrect.

Real-Life Example

Imagine a student registration form.


name = "Ali"
age = 20
height = 5.8
is_student = True

Here:

  • name β†’ String
  • age β†’ Integer
  • height β†’ Float
  • is_student β†’ Boolean

βœ… Correct


marks = 90
student = "Ali"
Note: You can skip the explanation. It is just added for better understanding.

1. Input Operation

An input operation allows the user to enter data into a program.

In Python, the input() function is used to take input from the user.

Input an Integer (int)

Use int(input()) to take a whole number as input.


age = int(input("Enter your age: "))

print(age)

Sample Output:


Enter your age: 20
20

Input a Float (float)

Use float(input()) to take a decimal number as input.


height = float(input("Enter your height: "))

print(height)

Sample Output:


Enter your height: 5.8
5.8

Input a String (str)

Use str(input()) to take text as input.


name = str(input("Enter your name: "))

print(name)

Sample Output:


Enter your name: Ali
Ali

2. Output Operation

An output operation displays information to the user.

In Python, the print() function is used to display output.

Syntax


print(value)

Example


print("Hello, World!")

Output:


Hello, World!

Operators and Expressions in Python

What are Operators?

Operators are special symbols that perform operations on values or variables.

Example


a = 10
b = 5

print(a + b)

Output:


15

Here:

  • + is the operator.
  • It adds the values of a and b.

Types of Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Meaning Example
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2.0
% Modulus (Remainder) 10 % 3 = 1
** Exponent (Power) 2 ** 3 = 8
// Floor Division 10 // 3 = 3

Python Arithmetic Operators Example


a = 10
b = 5

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % 3)
print("Exponent:", 2 ** 3)
print("Floor Division:", 10 // 3)

Output:


Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 1
Exponent: 8
Floor Division: 3

2. Comparison (Relational) Operators

These operators compare two values and return True or False.

Operator Meaning Example
== Equal to 5 == 5 β†’ True
!= Not equal to 5 != 3 β†’ True
> Greater than 10 > 5 β†’ True
< Less than 5 < 10 β†’ True
>= Greater than or equal to 10 >= 10 β†’ True
<= Less than or equal to 5 <= 10 β†’ True

Python Comparison (Relational) Operators Example


a = 10
b = 5

print("Equal to:", a == b)
print("Not equal to:", a != b)
print("Greater than:", a > b)
print("Less than:", a < b)
print("Greater than or equal to:", a >= b)
print("Less than or equal to:", a <= b)

Output:


Equal to: False
Not equal to: True
Greater than: True
Less than: False
Greater than or equal to: True
Less than or equal to: False

3. Assignment Operators

These operators assign values to variables.

Operator Meaning Example
= Assign x = 10
+= Add and assign x += 5
-= Subtract and assign x -= 5
*= Multiply and assign x *= 2
/= Divide and assign x /= 2
%= Modulus and assign x %= 3
//= Floor divide and assign x //= 2
**= Exponent and assign x **= 2

Python Assignment Operators Example


x = 10
x += 5
print(x)

x = 10
x -= 5
print(x)

x = 10
x *= 2
print(x)

x = 10
x /= 2
print(x)

x = 10
x %= 3
print(x)

x = 10
x //= 3
print(x)

x = 5
x **= 2
print(x)

Output:


15
5
20
5.0
1
3
25

4. Logical Operators

Logical operators combine two or more conditions into a single expression and return True or False.

Operator Meaning
and True if both conditions are True.
or True if at least one condition is True.
not Reverses the result.

Python Logical Operators Example


x = 10
y = 5

print(x > 5 and y < 10)
print(x > 20 or y < 10)
print(not(x > 5))

Output:


True
True
False

Operator Precedence in Python

What is Operator Precedence?

Operator precedence is the order in which Python evaluates operators in an expression.

Simple Definition:

Operator precedence is the set of rules that determines which operation is performed first in an expression.

Operator Precedence Table

Precedence Operator Description
1 (Highest) () Parentheses
2 ** Exponent
3 *, /, //, % Multiplication, Division, Floor Division, Modulus
4 +, - Addition, Subtraction
5 ==, !=, >, <, >=, <= Comparison Operators
6 not Logical NOT
7 and Logical AND
8 (Lowest) or Logical OR

Multiple Choice Questions (MCQs) on Python Programming

1. What is Python?
a. A database software
b. A high-level programming language
c. A web browser
d. An operating system
Answer: b. A high-level programming language
2. Python is mainly used to develop:
a. Only games
b. Only websites
c. Many different types of applications
d. Only mobile apps
Answer: c. Many different types of applications
3. Why is Python called a versatile language?
a. It works only on Windows
b. It can be used for many different purposes
c. It is used only for AI
d. It supports only one programming style
Answer: b. It can be used for many different purposes
4. Which of the following is NOT a common use of Python?
a. Data Science
b. Artificial Intelligence
c. Machine Learning
d. Manufacturing Computer Hardware
Answer: d. Manufacturing Computer Hardware
5. Computer programming is the process of:
a. Repairing computers
b. Writing instructions for computers
c. Installing software only
d. Building hardware
Answer: b. Writing instructions for computers
6. A computer performs tasks by:
a. Thinking like humans
b. Guessing answers
c. Following programmed instructions
d. Making random decisions
Answer: c. Following programmed instructions
7. Setting up a Python development environment means:
a. Buying a new computer
b. Installing and configuring Python tools
c. Learning networking
d. Formatting the hard drive
Answer: b. Installing and configuring Python tools
8. Which of the following is a Python IDE?
a. MS Paint
b. Visual Studio Code
c. VLC Media Player
d. Adobe Photoshop
Answer: b. Visual Studio Code
9. Google Colab is:
a. A desktop operating system
b. A cloud-based Python development environment
c. A web browser
d. A programming language
Answer: b. A cloud-based Python development environment
10. Google Colab allows you to:
a. Run Python code in a web browser
b. Install Windows
c. Build computer hardware
d. Design graphics
Answer: a. Run Python code in a web browser
11. Comments in Python are used to:
a. Execute code faster
b. Explain the code
c. Store data
d. Display output
Answer: b. Explain the code
12. Python ignores:
a. Variables
b. Functions
c. Comments
d. Strings
Answer: c. Comments
13. Which symbol starts a single-line comment in Python?
a. //
b. /*
c. #
d. &
Answer: c. #
14. Multi-line comments are commonly written using:
a. Double slashes //
b. Triple quotes
c. Parentheses ()
d. Square brackets []
Answer: b. Triple quotes
15. A variable is:
a. A hardware device
b. A named location used to store data
c. A programming error
d. A comment
Answer: b. A named location used to store data
16. Which variable name is valid?
a. 2name
b. first name
c. _age
d. student-name
Answer: c. _age
17. Variable names in Python are:
a. Case-insensitive
b. Case-sensitive
c. Always uppercase
d. Always lowercase
Answer: b. Case-sensitive
18. Which of the following cannot be used as a variable name?
a. total_marks
b. student1
c. class
d. first_name
Answer: c. class
19. Which data type stores whole numbers?
a. float
b. string
c. int
d. bool
Answer: c. int
20. Which data type stores decimal numbers?
a. int
b. bool
c. float
d. str
Answer: c. float
21. Which data type stores text?
a. int
b. str
c. bool
d. float
Answer: b. str
22. Boolean values are:
a. Yes and No
b. 1 and 0
c. True and False
d. High and Low
Answer: c. True and False
23. Which function is used to take input from the user?
a. print()
b. input()
c. output()
d. display()
Answer: b. input()
24. Which function displays output on the screen?
a. input()
b. show()
c. print()
d. echo()
Answer: c. print()
25. Which expression takes an integer input?
a. float(input())
b. str(input())
c. int(input())
d. print(input())
Answer: c. int(input())
26. Operators are:
a. Programming errors
b. Special symbols that perform operations
c. Comments
d. Variables
Answer: b. Special symbols that perform operations
27. Which operator performs addition?
a. -
b. *
c. +
d. %
Answer: c. +
28. Which operator gives the remainder?
a. /
b. %
c. *
d. //
Answer: b. %
29. Which operator is used for exponentiation?
a. ^
b. **
c. //
d. %
Answer: b. **
30. Which operator performs floor division?
a. //
b. /
c. %
d. **
Answer: a. //
31. Which operator checks equality?
a. =
b. ==
c. !=
d. >=
Answer: b. ==
32. Which comparison operator means "Not Equal"?
a. ==
b. >
c. !=
d. <=
Answer: c. !=
33. Which operator is used to assign a value?
a. ==
b. =
c. +=
d. !=
Answer: b. =
34. Which assignment operator adds and assigns a value?
a. -=
b. +=
c. *=
d. /=
Answer: b. +=
35. Which logical operator returns True only if both conditions are True?
a. or
b. and
c. not
d. ==
Answer: b. and
36. Which logical operator returns True if at least one condition is True?
a. and
b. or
c. not
d. %
Answer: b. or
37. The not operator:
a. Adds values
b. Multiplies values
c. Reverses the logical result
d. Assigns values
Answer: c. Reverses the logical result
38. Operator precedence determines:
a. Variable names
b. The order of execution of operators
c. Output format
d. Data types
Answer: b. The order of execution of operators
39. Which operator has the highest precedence in Python?
a. +
b. *
c. () Parentheses
d. and
Answer: c. () Parentheses
40. Which logical operator has the lowest precedence?
a. not
b. and
c. or
d. ==
Answer: c. or

Test Yourself: Interactive MCQs (Operating System)

Multiple Choice Questions (MCQs) on Python Programming

1. Which of the following is a feature of Python?
2. Python can be used for which of the following?
3. Which field commonly uses Python?
4. Python is commonly used for:
5. Which application can be developed using Python?
6. Which technology is supported by Python?
7. Which of the following is an IDE?
8. Which IDE comes with Python by default?
9. Google Colab requires:
10. Comments help programmers to:
11. Which symbol begins a single-line comment?
12. Multi-line comments are commonly written using:
13. A variable stores:
14. Which variable name is valid?
15. Which statement about variables is true?
16. Which of these is a Python keyword?
17. Which data type stores text?
18. Which data type stores decimal values?
19. Which data type stores True or False?
20. Which data type stores whole numbers?
21. Which function is used for user input?
22. Which function displays output?
23. Which function converts input into an integer?
24. Which function converts input into a decimal number?
25. Which operator performs subtraction?
26. Which operator performs multiplication?
27. Which operator performs division?
28. Which operator returns the remainder?
29. Which operator means "greater than"?
30. Which operator means "less than or equal to"?
31. Which assignment operator multiplies and assigns?
32. Which assignment operator divides and assigns?
33. Which logical operator requires both conditions to be True?
34. Which logical operator is True if one condition is True?
35. Which logical operator reverses a Boolean value?
36. Which operator has the highest precedence?
37. Which operator is evaluated before addition?
38. Which operator has lower precedence than 'and'?
39. Which of the following is NOT an arithmetic operator?
40. Which of the following is NOT a comparison operator?

FAQs

Python Programming FAQs

Python is a high-level, easy-to-read programming language used to develop websites, software, mobile apps, games, AI applications, and many other types of programs.

Python is called versatile because it can be used for many different tasks such as web development, AI, data science, automation, game development, and more.

Computer programming is the process of writing instructions (code) that tell a computer what to do and how to perform specific tasks.

A Python development environment is a collection of tools used to write, edit, run, and test Python programs.

Popular Python IDEs include Visual Studio Code (VS Code), PyCharm, Google Colab, Jupyter Notebook, IDLE, and Spyder.

Google Colab is a cloud-based Python development environment that allows you to write and run Python code directly in a web browser without installing Python.

Comments are notes added to Python code to explain its purpose. They are ignored by the Python interpreter and are not executed.

A variable is a named memory location used to store data that can be used or changed during program execution.

The basic Python data types are Integer (int), Float (float), String (str), and Boolean (bool).

The input() function is used to receive data entered by the user during program execution.

The print() function is used to display output or information on the screen.

Operators are special symbols that perform operations on variables and values, such as addition, comparison, and logical operations.

Arithmetic operators perform mathematical calculations. Examples include +, -, *, /, %, **, and //.

Logical operators (and, or, not) are used to combine or reverse conditions and return True or False.

Operator precedence is the set of rules that determines the order in which Python evaluates operators in an expression.

Download Notes

Download PDF