Programming Fundamentals
Lab 2
Introduction to Python
1
Lab Instructor : Ijaz Alam
[Link]@[Link]
2 Python
Versatile, general- purpose programming language created by Guido van Rossum
in 1991.
High-level language
Allows faster development without compilation
Supports multiprogramming paradigms [OOP and Functional]
Used for AI, Data Science, Automation and Web Development
Open source with vast community and libraries
[Link]@[Link]
3 Why Python?
Simple: Easy syntax, ideal for beginners. Embeddable: Integrates with other languages like C/C+
• Example: "print('Hello, World!’)” +.
Portable: Write once, run anywhere without
Cross-Platform: Runs on Windows, Linux.
modification.
Interpreted: No need to compile; code • Example: Python code can be shared across different platforms
without change.
is executed directly.
Libraries: Vast collection of pre-built tools Robust: Strong error handling and dynamic typing.
• Example: Python can handle runtime errors efficiently with try-except
•Example: "import math“ ”NumPy”
[Link]@[Link]
blocks.
4 Programming Basics
Code/Source Code:
Sequence of instructions in a program.
Syntax:
Set of structures and commands used in a particular programming
language.
Output:
The messages printed to the user by a program.
Console:
[Link]@[Link]
5 Compiling and Interpreting
[Link]@[Link]
6 Rules for Variable Naming
Start with a letter or an underscore: Variables must
begin with a letter (a-z, A-Z) or an underscore (_).
Followed by letters, numbers, or underscores: The
rest of the variable name can contain letters,
numbers, and underscores.
Case Sensitive: Variable names are case-sensitive,
meaning variable and Variable would be considered
two different variables.
No spaces or special characters: Spaces and special
characters (like @, $, %, etc.) are not allowed.
[Link]@[Link]
7 Data Types in Python
Integer (int) String (str)
• Represents whole numbers without • Represents a sequence of
C/C++
Str[ ]=“Hello World“
a decimal point. C/C++
characters enclosed in quotes.
int age=30
Example: age = 30 Example: str = "Hello, World!"
Float (float) Boolean (bool)
• Represents numbers with a • Represents one of two values: True
C/C++ C/C++
decimal point. float or False. Bool student=True
price=19.99
[Link]@[Link]
Example: price = 19.99 Example: student = True
8 Instances of Variable Declaration
[Link]@[Link]
9
Arithmetic
Operations Addition
Subtracti
on
Exponent
Modulus
[Link]@[Link] Multiplication Division
10
Logical
Operators AND OR
(A and B )or
NOT
Logical Operations:
C
Operations performed on
Boolean values (True and
False) to make decisions in
NAND
programs.
NOR
[Link]@[Link]
11
Relational
Operators
Relational operators
are used to compare
values
[Link]@[Link]
12
Bitwise
Operator a = 10 # Binary: 1010
b = 4 # Binary: 0100
# Bitwise AND
res = a & b
# Bitwise OR
res = a | b
# Bitwise XOR
[Link]@[Link]
res = a ^ b
13 List, Array , String , Tuple and Set in Python
List is a mutable, ordered collection
Tuple is an immutable, ordered
collection of items. Tuples cannot be
of items in Python. changed after their creation, making
Contain elements of different data types them useful when you want to ensure
(integers, strings, objects, etc.) data integrity.
Set in Python is an unordered
Array is a collection of items of collection of unique, immutable
the same data type. Libraries NumPy. [Link], meaning you can
add or remove items, but duplicates
are automatically removed.
String is a sequence of characters
and is also immutable in Python, Dictionary stores data in key-value
meaning once created, it cannot be pairs. Each key must be unique, and it is
[Link]@[Link]
used to access the corresponding value.
modified.
14 List Functions
[Link](x) Add item at the end of the list.
[Link](i,x) Insert item at a given position. Similar to a[i:i]=[x]
[Link](x) Removes first item from the list with value x
[Link](i) Remove item at position I and return it. If no index I is given then remove the first
item in the list.
[Link](x) Return the index in the list of the first item with value x.
[Link](x) Return the number of time x appears in the list
[Link]() Sorts items in the list in ascending order
[Link]@[Link]
[Link]() Reverses items in the list
15
Input from
User:
List: The program prompts the user to enter fruits separated
by commas, which are split into a list.
Tuple: The user inputs coordinates in the format x, y, and these values
are converted into a tuple of floats.
Dictionary (dict): The user is asked to input their name and age,
which are stored as key-value pairs in a dictionary.
Set: The user inputs numbers, and they are converted into a set to
[Link]@[Link] ensure uniqueness.