print("Hello, " + input("What is your name? ") + "!")Explanation:
input()takes input from the user.print()displays output.+is used for string concatenation.
Example Output:
What is your name? Mathew
Hello, Mathew!
name = input("What's your name? ")
print(name)Explanation:
- Variables are used to store values.
- Here, the variable
namestores the user's input.
print(len(input("Tell me your name: ")))Explanation:
len()returns the number of characters.- The input is passed directly into
len().
username = input("What is your name? ")
length = len(username)
print("Your name has " + str(length) + " characters")Explanation:
len()returns an integer.- Integers must be converted to strings using
str()before concatenation.
Question: We have two variables:
glass1 = "milk"
glass2 = "juice"Swap the contents without typing "milk" or "juice".
Solution:
temp = glass1
glass1 = glass2
glass2 = tempResult:
glass1 = juice
glass2 = milk
username = input("What is your name? ")
petname = input("What is your pet name? ")
print("Hello, " + username + "!")
print("Your band name is " + username + " " + petname)Example Output:
What is your name? Mathew
What is your pet name? Bruno
Hello, Mathew!
Your band name is Mathew Bruno
name = "Mathew"Stores text data.
age = 25Stores whole numbers.
salary = 25000.50Stores decimal numbers.
is_active = TrueStores either True or False.
print("Hello"[0])Output:
H
Explanation:
- Strings are sequences of characters.
- Index starts from
0.
| Character | H | e | l | l | o |
|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 |
Examples:
print("Hello"[1]) # e
print("Hello"[4]) # oprint(type(1234))
print(type(12.34))
print(type("Hello, World!"))
print(type([1, 2, 3, 4, 5]))
print(type((1, 2, 3, 4, 5)))
print(type({1, 2, 3, 4, 5}))
print(type({"a": 1, "b": 2, "c": 3}))
print(type(True))
print(type(None))
print(type(1 + 2j))
print(type(len))Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'bool'>
<class 'NoneType'>
<class 'complex'>
<class 'builtin_function_or_method'>
length = 10
print("Length is " + str(length))num = "100"
print(int(num) + 50)Output:
150
user = input("Enter your name: ")
print(f"Hello, {user}!")
length = len(user)
print(f"Your name has {length} characters.")
print("Number converted to string: " + str(length))
print("Goodbye!")Example Output:
Enter your name: Mathew
Hello, Mathew!
Your name has 6 characters.
Number converted to string: 6
Goodbye!
Python automatically converts some data types when needed.
num1 = 10
num2 = 5.5
result = num1 + num2
print(result)
print(type(result))Output:
15.5
<class 'float'>
Explanation:
- Integer (
10) is automatically converted to float (10.0). - Result becomes a float.
✅ print()
✅ input()
✅ Variables
✅ len() function
✅ String concatenation
✅ f-Strings
✅ Data Types (str, int, float, bool)
✅ String indexing
✅ type() function
✅ Type Casting (str(), int(), float())
✅ Implicit Type Conversion
✅ Variable Swapping
✅ Mini Project - Band Name Generator
a = float(input("What's the bill amount? "))
b = float(input("What's the tip percentage? "))
c = int(input("How many people are splitting the bill? "))
tip = a * (b / 100)
total = a + tip
per_person = total / c
print(f"Each person should pay: ${per_person:.2f}")Converts input into a decimal number.
price = float("150.75")Converts input into a whole number.
people = int("5")| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| // | Floor Division | 10 // 3 |
| % | Modulus | 10 % 3 |
| ** | Power | 2 ** 3 |
name = "Mathew"
print(f"Hello {name}")price = 25.6789
print(f"{price:.2f}")Output:
25.68
Conditional statements allow a program to make decisions.
if condition:
# code
else:
# codeprint("Welcome to the roller coaster!")
height = int(input("What is your height in cm? "))
if height >= 120:
print("You can ride the roller coaster!")
else:
print("Sorry, you have to grow taller before you can ride.")Condition True -> Execute IF block
Condition False -> Execute ELSE block
Comparison operators compare two values and return either True or False.
| Operator | Meaning |
|---|---|
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| == | Equal to |
| != | Not equal to |
print(10 > 5)Output:
True
print(10 == 5)Output:
False
=Assignment operator
==Comparison operator
Example:
age = 25Assigns 25 to age.
age == 25Checks if age equals 25.
The modulus operator returns the remainder after division.
10 % 2Output:
0
10 % 3Output:
1
a = int(input("Enter a number: "))
if a % 2 == 0:
print("The number is even")
else:
print("The number is odd")Number % 2 = 0 -> Even
Number % 2 = 1 -> Odd
An if statement inside another if statement.
height = int(input("What is your height in cm? "))
age = int(input("What is your age? "))
if height >= 120:
print("You can ride the rollercoaster!")
if age <= 18:
print("Please pay $7.")
else:
print("Please pay $12.")
else:
print("Sorry, you have to grow taller before you can ride.")Height Check
|
├── False → Not allowed
|
└── True
|
Age Check
|
├── <=18 → $7
└── >18 → $12
Used when there are multiple possible outcomes.
if condition1:
code
elif condition2:
code
elif condition3:
code
else:
codemarks = 85
if marks >= 90:
print("Grade A")
elif marks >= 80:
print("Grade B")
elif marks >= 70:
print("Grade C")
else:
print("Fail")Output:
Grade B
BMI (Body Mass Index) measures body weight relative to height.
BMI = \frac{weight}{height^2}
weight = int(input("Enter your weight in kg: "))
height = float(input("Enter your height in meters: "))
bmi = weight / (height ** 2)
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Normal weight")
elif bmi < 30:
print("Overweight")
else:
print("Obese")| BMI Range | Category |
|---|---|
| Below 18.5 | Underweight |
| 18.5 - 24.9 | Normal |
| 25 - 29.9 | Overweight |
| 30 and above | Obese |
2 ** 3Output:
8
Used in BMI calculation:
height ** 2means:
height × heightUsed to combine multiple conditions.
| Operator | Meaning |
|---|---|
| and | Both conditions must be True |
| or | At least one condition must be True |
| not | Reverses the result |
age = 20
height = 170
if age >= 18 and height >= 150:
print("Allowed")if age >= 18 or height >= 150:
print("Allowed")is_banned = False
if not is_banned:
print("Access Granted")✅ print()
✅ input()
✅ Variables
✅ len()
✅ Strings
✅ Integers
✅ Floats
✅ Booleans
✅ String Indexing
✅ type()
✅ Type Casting
✅ Arithmetic Operators
✅ Comparison Operators
✅ Modulus Operator
✅ Exponent Operator
✅ Conditional Statements
✅ if
✅ else
✅ elif
✅ Nested if
✅ Logical Operators (and, or, not)
✅ f-Strings
✅ Tip Calculator Project
✅ BMI Calculator Project
✅ Even/Odd Checker Project
✅ Roller Coaster Project