Core Building Blocks
Python uses two simple built-in functions for basic I/O:
print()→ displays output on the screeninput()→ reads text typed by the user
Both work with the data types you already know: strings, numbers, booleans, and None.
The print() Function
print() shows data on the console. It works with any data type.
# Printing different data types
print("Hello, Python!") # str
print(42) # int
print(3.1415) # float
print(True) # bool
print(None) # NoneTypeYou can print multiple values at once (separated by spaces by default):
name = "Layla"
age = 19
print(name, age) # Output: Layla 19The input() Function
input() always returns a string, even if the user types a number.
# Example 1: No prompt
response = input()
print("You entered:", response)# Example 2: With a prompt message
city = input("What city do you live in? ")
print("City:", city)Converting Input to Numbers
Since input() gives a string, use int() or float() to convert it if you need a number.
# Reading an integer
age_text = input("Enter your age: ") # e.g., "20"
age_number = int(age_text) # now it's an int
print("Next year you will be", age_number + 1)# Reading a decimal number
price_text = input("Enter price: ") # e.g., "19.99"
price = float(price_text) # now it's a float
print("Price is:", price)Important Notes
input()always returns astr.- If you try
int("abc"), Python will show an error (but error handling is covered later). - For now, assume the user enters valid data (e.g., a number when asked for a number).
- All printed values are automatically converted to strings by
print().
Real-Life Scenarios (Using Only Variables & I/O)
Scenario 1: Personal Introduction
name = input("Your name: ")
country = input("Your country: ")
print("Hello! My name is", name, "and I am from", country)Scenario 2: Simple Calculator (Addition)
num1_str = input("First number: ")
num2_str = input("Second number: ")
num1 = int(num1_str)
num2 = int(num2_str)
result = num1 + num2
print("Sum is:", result)Scenario 3: Product Info Display
product = "Laptop"
price = 1299.99
in_stock = True
print("Product:", product)
print("Price:", price)
print("Available:", in_stock)