PYTHON BASICS
INPUT in Python
🔹 What is Input?
Input is how a program gets information from the user using the keyboard.
In Python, we use:
input()
🔹 Example:
name = input("Enter your name: ")
print(name)
🔹 Explanation:
input() pauses the program
The user types something
The typed value is stored in a variable
🔹 Important Point:
input() always stores data as text (string)
If you want a number, you must convert it.
🔹 Input a Number:
age = int(input("Enter your age: "))
🔹 Common Conversions:
Function Meaning
int() Converts to whole number
float() Converts to decimal number
str() Converts to text
OUTPUT in Python
🔹 What is Output?
Output is how a program shows results on the screen.
In Python, we use:
print()
🔹 Example:
print("Hello World")
🔹 Printing Variables:
name = "Ali"
print(name)
🔹 Printing Text and Variables Together:
age = 12
print("Age is", age)
🔹 New Line Output:
print("Hello")
print("World")
Output:
Hello
World
Variables in Python
🔹 What is a Variable?
A variable is used to store data (information) in a program so it can be used later.
👉 Think of a variable as a label on a box that holds a value.
🔹 Creating a Variable
In Python, you do not need to declare a variable type.
age = 12
name = "Ali"
age is the variable name
12 is the value stored in it
🔹 Rules for Variable Names
✔ Must start with a letter or underscore
✔ Can contain letters, numbers, and underscores
❌ Cannot start with a number
❌ Cannot contain spaces
✅ Valid:
score
total_marks
student1
❌ Invalid:
1score
total marks
Data Types in Python
🔹 What is a Data Type?
A data type tells Python what kind of data is stored in a variable.
🟢 Integer (int)
Stores whole numbers
age = 14
marks = 85
Examples: 5, -10, 0
🟢 Float (float)
Stores decimal numbers
height = 5.6
price = 99.50
Examples: 2.5, 3.14, -7.8
🟢 String (str)
Stores text (letters, words, sentences)
✔ Must be inside quotes
name = "Sara"
message = "Hello World"
🟢 Boolean (bool)
Stores True or False
is_student = True
is_raining = False
Used mainly in conditions and decisions.
Operators in Python
🔹 What is an Operator?
An operator is a symbol used to perform operations on values.
🧮 Arithmetic Operators
Used for math calculations
Operator Meaning Example Result
+ Addition 5+3 8
- Subtraction 10 - 4 6
* Multiplication 6*2 12
/ Division 8/2 4.0
% Modulus (remainder) 7%2 1
total = 10 + 5
🔍 Comparison Operators
Used to compare values
Operator Meaning Example
== Equal to 5 == 5
!= Not equal 5 != 3
> Greater than 10 > 5
< Less than 3<7
>= Greater or equal 6 >= 6
<= Less or equal 4 <= 5
👉 Result is always True or False
🔗 Logical Operators
Used to combine conditions
Operator Meaning Example
and Both must be true age > 10 and age < 18
or One must be true rainy or cold
not Opposite result not True
Selection (Decision Making)
🔹 What is Selection?
Selection allows a program to make decisions based on conditions.
🟦 if Statement
Used when you want to check one condition
age = 15
if age >= 13:
print("You are a teenager")
✔ If condition is True, code runs
❌ If False, code is skipped
🟦 if–else Statement
Used when there are two choices
marks = 40
if marks >= 50:
print("Pass")
else:
print("Fail")
✔ One block will always run
🟦 if–elif–else Statement
Used when there are multiple conditions
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
Python checks conditions from top to bottom.
⚠ Important Rules for Conditions
✔ Use : at the end of condition
✔ Indentation (spaces) is very important
✔ Usually 4 spaces are used
❌ Wrong:
if age > 10
print("Hello")
✅ Correct:
if age > 10:
print("Hello")
FOR LOOP in Python
🔹 What is a For Loop?
A for loop is used when you know how many times you want to repeat something.
🔹 Syntax:
for variable in range(start, stop):
code
⚠️stop is not included
🔹 Example 1: Print numbers 1 to 5
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
🔹 Example 2: Print a message 3 times
for i in range(3):
print("Hello")
🔹 Example 3: Loop through a word
word = "CAT"
for letter in word:
print(letter)
Output:
C
A
T
🔹 Key Points:
✔ Used when repetitions are known
✔ Uses range()
✔ Indentation is very important
WHILE LOOP in Python
🔹 What is a While Loop?
A while loop repeats while a condition is true.
Used when you do not know how many times the loop will run.
🔹 Syntax:
while condition:
code
🔹 Example 1: Print numbers 1 to 5
count = 1
while count <= 5:
print(count)
count = count + 1
🔹 Example 2: Password Check
password = ""
while password != "python":
password = input("Enter password: ")
print("Access granted")
🔹 Important Rule:
⚠️The condition must become false, or the loop will run forever
This is called an infinite loop
🔹 Infinite Loop Example (Wrong):
while True:
print("Hello")
FOR LOOP vs WHILE LOOP
For Loop While Loop
Known number of repeats Unknown number
Uses range() Uses condition
Easier for counting Good for input checking