high-level programming language
Giving instructions to a computer.
(Python, Java, or C++, …)
Created by Guido van Rossum in 1991, a Dutch Programmer.
high-level programming language
Why we have to learn Python?
• Easy to learn (Simple Syntax, Less Rules, Fast Learning)
• Used to make games, apps, AI
• Very popular programming language
PYTHON
First Python Program
print( ) - function (the task is output data & new line)
print ( data ) ---------> (text or number)
print ("Hello World")
print (‘ Hello World ’)
print (100)
Python is a case-sensitive language.
Escape Character
An escape character is a backslash ( \ ) followed by a character you want to insert into a
string. They are used to handle "illegal" characters.
\n (new line)
\t (tab – 4 spaces)
e.g – print (“Hello Python”) -> print (“One\n Two\nThree”) ->print (“Hello\tPython”)
Arithmetic Operators
Operators Name Example
+ Addition 10 + 3 = 13
- Subtraction 10 - 3 = 7
* Multiplication 10 * 3 = 30
/ Division 10 / 3 = 3.3333…..
// Floor division 10 // 3 = 3
% Modulus 10 % 3 = 1
** Exponentiation 10 ** 3 = 1000
Arithmetic Operators
Join the Data
1. Using a Comma (For Printing)
If you only need to print the values, separate them with a comma in the print()
function. This automatically adds a space between the text and the number.
print("Score:", 95)
2. The + Operator with string
To join them into a single string, convert the number to a string using string
before joining them with +
print (“Welcome Sarah!” + “Your ID is 9845.”)
Join the Data
3. F-Strings
F-string allows you to format selected parts of a string. To specify a string as an
f-string, simply put an f in front of the string.
To format values in an f-string, add placeholders {},
a placeholder can contain variables, operations, functions, and modifiers to format
the value.
print(f"The price is {10} dollars")
Data Types
Category Type Name Description Example
Numeric Int Whole numbers (no limit on size) 5, -10
float Floating-point (decimal) numbers 3.14, -0.01
Complex Numbers with real and imaginary parts 1+2j
Text str Sequences of Unicode characters "Hello", 'Python'
Sequence list Ordered, mutable (changeable) collection [1, 2, "a"]
tuple Ordered, immutable collection (1, 2, "a")
range A sequence of numbers often used in loops range(6)
Boolean bool Represents logical values True, False
Getting the Data Type
You can get the data type of any object by using the type() function:
print(type("Hello"))
print(type(3))
print(type(3.14)) Output:
print(type(True)) <class ‘str’>
<class ‘int’>
<class ‘float’>
<class ‘bool’>
Variables
• Variable are containers for storing data values in memory.
• Python has no command for declaring a variable.
• Variables are created the moment you first assign a value to them using the
assignment operator (=).
myvar = “John”
my_var = “John”
_my_var = “John”
myVar = “John”
MYVAR = “John”
myvar2 = “John”
Do’s Don’t s
Rules of Naming Variable
Assign to the Variable
x = 10
y = 10
#Multiple variables, same value
x = y = z = 100
#Multiple variables, different values
age, status = 25, "Active“
x = 10
x = x + 1 #increment by 1
or
x += 1
Input function [ input( ) ]
▪ Accept user input from the keyboard using
the input() function. #input data and assign to variable
var = input(“Prompt message”)
▪ Always returns a string (str). #output data
print (var)
Python's input() always returns text (str).
✓ To use it as a number for math, you must convert
it using int() or float() or bool().
Example Of Input
age = int(input(“Enter your current age”))
print(“In next 10 years, you’ll be”, age+10)
Type Casting
Python type casting is the process of converting a variable from one data type to another. It ensures
compatibility when performing operations across different types.
You manually convert the types using
built-in functions:
int() - Converts to an integer.
float() - Converts to a float.
str() - Converts to a string.
Comparison Operator
Python comparison operators (also known as relational operators) compare two values and evaluate to a
Boolean result: either True or False. They are fundamental components of control flow tools like if
statements and loops.
If Statement or Decision-making Statement
A Python if statement evaluates whether a condition is true or false, allowing your program to make
decisions and execute specific blocks of code.
Basic Syntax & Rule Python if statement code:
age = 20
• the if keyword before condition
• colon ( : ) after condition if age >= 18:
• indented block of code when the condition is print("You are an adult.")
true. (4 spaces or Tab key )
Advantages of decision-making statements in Python
Interaction: The computer guides the user on what he is
doing, like showing a wrong password message if the password
is wrong.
Clean and tidy: By using statements, you can keep your code
organized and easy to read.
Efficient: It makes sure that the program runs faster as it
avoids working on parts that are not required.
Safety network: In a program, it is made sure that if an
unexpected problem occurs, there is a backup.
If-Else statement in Python
An if-else statement lets your program make decisions. If a condition is
true, one block of code runs and if it is false, another block runs instead.
age = 18
if condition:
if age >= 18:
# Runs if condition is True
print("You can vote.")
else:
else:
# Runs if condition is False
print("You are too young to vote.")
Key Points
Indentation: The code inside the if and else blocks must be indented (usually 4 spaces).
Colons: Do not forget the : at the end of the if and else lines.
If-Elif-Else statement in Python
In Python, the if, elif, and else statements are used for conditional
decision-making. The program checks conditions from top to bottom and
executes the first block that evaluates to True.
if condition1: score = 85
# Executes if condition1 is True if score >= 90:
elif condition2: print("Grade: A")
# Executes if condition1 is False and condition2 is True elif score >= 80:
else: print("Grade: B")
# Executes if all previous conditions are False else:
print("Grade: C")
If-Elif-Else statement in Python
Key Points
if: Required. It's the starting point.
elif: Optional. Short for "else if". You can have multiple elif blocks.
else: Optional. It catches anything not caught by the previous conditions.
Indentation: Python uses 4 spaces (indentation) to define the code blocks.