๐Ÿ“˜ Lesson 4 examples: Input, Output, and Type Casting

๐Ÿ”น 1. Output in Python (print())

The print() function is used to display output on the screen.


Example 1: Simple Output

print("Hello Python")

Output

Hello Python

๐Ÿ“Œ Explanation

  • Text inside quotes is called a string
  • print() shows it on the screen

Example 2: Printing Multiple Values

name = "Sameer"
age = 25

print(name, age)

Output

Sameer 25

๐Ÿ“Œ Explanation

  • Python automatically adds a space between values

Example 3: Output with Text

name = "Ayesha"
city = "Hyderabad"

print("Name:", name)
print("City:", city)

Output

Name: Ayesha
City: Hyderabad

๐Ÿ”น 2. Input in Python (input())

The input() function is used to take input from the user.

โš  Important:
input() always returns string data


Example 4: Simple Input

name = input("Enter your name: ")
print("Hello", name)

Sample Output

Enter your name: Chandini
Hello Chandini

๐Ÿ“Œ Explanation

  • User types the name
  • Value is stored in name
  • Printed using print()

Example 5: Input Without Message

city = input()
print("City:", city)

Sample Output

Bangalore
City: Bangalore

๐Ÿ”น 3. Problem Without Type Casting

age = input("Enter age: ")
print(age + 1)

โŒ Error Output

TypeError: can only concatenate str (not "int") to str

๐Ÿ“Œ Explanation

  • input() returns string
  • Python cannot add number to string

๐Ÿ”น 4. Type Casting (Type Conversion)

Type casting means converting one data type to another.


Common Type Casting Functions

FunctionConverts To
int()Integer
float()Decimal
str()String
bool()Boolean

๐Ÿ”น 5. Input with Type Casting (MOST IMPORTANT)


Example 6: Converting Age to Integer

age = int(input("Enter age: "))
print("Next year age:", age + 1)

Sample Output

Enter age: 24
Next year age: 25

๐Ÿ“Œ Explanation

  • input() โ†’ string
  • int() converts it to number
  • Mathematical operation becomes possible

Example 7: Salary Input

salary = float(input("Enter salary: "))
bonus = 5000

total = salary + bonus
print("Total salary:", total)

Sample Output

Enter salary: 25000
Total salary: 30000.0

๐Ÿ”น 6. Multiple Inputs

Example 8: Student Marks

marks1 = int(input("Enter marks 1: "))
marks2 = int(input("Enter marks 2: "))

total = marks1 + marks2
average = total / 2

print("Total:", total)
print("Average:", average)

Sample Output

Enter marks 1: 80
Enter marks 2: 90
Total: 170
Average: 85.0

๐Ÿ”น 7. Type Casting Between Variables

Example 9: Integer to String

age = 22
text = "Age is " + str(age)

print(text)

Output

Age is 22

Example 10: String to Float

price = "99.5"
new_price = float(price)

print(new_price + 10)

Output

109.5

๐Ÿ”น 8. Boolean Type Casting

Example 11

print(bool(1))
print(bool(0))
print(bool(""))
print(bool("Python"))

Output

True
False
False
True

๐Ÿ“Œ Explanation

  • 0 or empty values โ†’ False
  • Non-zero or non-empty โ†’ True

๐Ÿ”น 9. Real-Life Example: Employee Details

name = input("Enter name: ")
age = int(input("Enter age: "))
salary = float(input("Enter salary: "))

print("Employee Name:", name)
print("Employee Age:", age)
print("Employee Salary:", salary)

Sample Output

Enter name: Gousya
Enter age: 26
Enter salary: 32000
Employee Name: Gousya
Employee Age: 26
Employee Salary: 32000.0

๐Ÿ”น 10. Common Mistakes (IMPORTANT)

โŒ Forgetting type casting
โŒ Adding number with string
โŒ Wrong data type conversion


๐Ÿง  Final Summary

โœ” print() โ†’ output
โœ” input() โ†’ user input (string)
โœ” Type casting converts data types
โœ” Required for calculations
โœ” Very important for real programs


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *