๐ฏ Learning Objectives
By the end of this lesson, learners will be able to:
-
Use the
input()
function to receive user input -
Understand that
input()
returns a string by default -
Convert strings to other data types (
int
,float
,bool
) -
Handle basic type conversion errors
๐ Lesson Content
๐น What is input()
in Python?
The input()
function lets you get data from the user during program execution.
โ Whatever the user types is always returned as a string, even if it’s a number.
๐ธ Type Conversion (Type Casting)
Type conversion means changing the data type of a variable or value.
Python provides built-in functions:
-
int()
โ Convert to integer -
float()
โ Convert to float -
str()
โ Convert to string -
bool()
โ Convert to boolean
๐ Converting Input
Example: Get a number from the user and double it
โ You must convert input if you want to treat it as a number.
๐ฅ Common Mistake
โ Fix:
๐ Example: All Data Types with Input
โ ๏ธ Caution: Input Errors
If you try to convert a non-numeric string to int
or float
, it will raise a ValueError
.
๐ ๏ธ We’ll learn error handling later!
๐ Assignment
Create a small script that:
-
Asks the user for their name, age, and weight
-
Converts age to
int
and weight tofloat
-
Prints a summary like:
"Hello Alice, you are 24 years old and weigh 55.5 kg."