Day 2 Python Lect 1
Day 2 Python Lect 1
Programming Language
AI-102 COURSE
LECT # 1
1
Overview
Python is one of most popular language.
Python is a high-level, versatile programming language
known for its simple and readable syntax, making it an
excellent choice for beginners as well as experts.
2
Features of Python
Simple Syntax, Easy to Code
High-Level Language
Open Source & Free
Cross-Platform Compatibility
Multi-Paradigm
Wide Range of Applications
3
Features of Python …
Dynamic Typing
Extensive Standard Library
Rapid Development
Strong Community Support
Interpreted Language
Case Sensitive
4
Basic Data Types
Python has Dynamic Typing property, So no need to tell data
type.
Integer (int) : Whole numbers, either positive, negative, or zero.
Example: x = 15 , y = -3
5
Basic Data Types …
Strings (str): Sequences of characters, enclosed in quotes (either single
quotes ' ' or double quotes " ").
Example: name = “Ali” , city = ‘Multan’
6
Expression
Expressions are combinations of values, variables, and operators that
the Python interpreter can evaluate to produce another value. An
expression is a piece of code that returns a value.
Example:
◦ 2+5 arithmetic operation, evaluates to 7
◦ len(‘hello’) a function call expression, evaluates to 5
7
Statement
Statements are instructions that the Python interpreter executes. A
statement performs an action, such as creating a variable, displaying
a value, or controlling the flow of execution.
Example:
• x = 10 # Assignment operator
◦ print(“Welcome”) # Output statement
8
Arithmetic Operators
Addition +
Subtraction –
Multiplication *
Division / (gives result in float) 5/2 gives 2.5
Floor Division // (gives result in integer) 17//5 gives 3
Exponent **, 2 ** 3 gives 8
Modulus %, 17 % 5 gives 2
9
Input
input() function is used to take input from users. By
default input() returns input as string:
name = input("Enter your name: ")
Converting Input to Other Data Types
• If you need a different data type, you must explicitly convert it.
num = int ( input("Enter a number: ") )
Or
num = input("Enter a number: ")
num = int(num)
10
String Operations
Concatenation: Joining two or more strings together using the + operator.
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: "Hello World"
11
String Operations …2
Slicing: Extracting a portion of a string using square brackets []
and indices.
str1 = "Hello, World!"
print(str1[0:5]) # Output: "Hello"
print(str1[7:12]) # Output: "World"
print(str1[:5]) # Output: "Hello" (from start to index 4)
print(str1[7:]) # Output: "World!" (from index 7 to end)
Length: Getting the number of characters in a string using the
len() function.
str1 = "Hello, World!"
print(len(str1)) # Output: 13
12
String Operations …3
Case Conversion: Changing the case of characters in a string.
str1 = "Hello, World!"
print(str1.upper()) # Output: "HELLO, WORLD!"
print(str1.lower()) # Output: "hello, world!"
print(str1.capitalize()) # Output: "Hello, world!"
print(str1.title()) # Output: "Hello, World!"
print(str1.swapcase()) # Output: "hELLO, wORLD!"
str2 = "one,two,three"
parts = str2.split(",") # Split by comma
print(parts) # Output: ["one", "two", "three"]
13
String Operations …4
Replacing: Replace a sub string with another sub string
str1 = "Hello, World!"
14
Q&A
15