Python Coding Day 2 | Input, Length, and Variables
Python Fundamentals: Input, Length, and Variables Explained
Watch the lesson tutorial 🔻
Interactive programming is all about making your code talk to the user, and letting the user talk back! To do this in Python, you need to master three fundamental concepts: getting input, using functions like len(), and storing data with variables.
Let's break down each one with practical examples.
1. Getting User Input with input()
The input() function is your program's way of asking the user a question and waiting for a response. When Python hits the input() line, the program pauses, displays the text you put inside the parentheses (the prompt), and waits for the user to type something and press Enter.
Crucially, whatever the user types is returned by the input() function as a string of text.
Practical Example: Instant Greeting
Here is your concise example, which combines prompting the user with printing a result:
print("Hello "+input("what is name?"))
How this runs, step-by-step:
Python starts evaluating the
print()function.It needs to figure out the value of
"Hello " + input("what is name?").The
input("what is name?")function runs first, displayingwhat is name?and waiting for the user (e.g., they typeMaria).The
input()function returns the string"Maria".The expression becomes
"Hello " + "Maria", which results in"Hello Maria".The
print()function displays:Hello Maria.
2. Measuring Things with the len() Function
Functions in Python are powerful blocks of reusable code. The len() function (short for length) is one of the most useful built-in functions. It takes an object, such as a string, and returns the number of items it contains.
If you pass len() a string, it will return the number of characters in that string.
Practical Example: Measuring Your Name
In this example, we nest the functions, running them from the inside out:
print(len(input("What is your name?")))
How this runs, step-by-step:
Python starts with the innermost function:
input("What is your name?").The user types their name (e.g.,
Alex) and presses Enter. Theinput()function returns the string"Alex".Now the expression is
print(len("Alex")).The
len()function runs on"Alex". Since "Alex" has 4 characters,len()returns the number4.Finally,
print(4)runs, and the program displays:4.
3. The Power of Variables (Containers for Data)
While the previous examples work, they are messy because the user's input is never saved anywhere; it's used once and then lost.
Variables solve this problem. A variable is simply a named container that holds a value. You define a variable using a single equals sign (=), which is called the assignment operator.
Variables allow you to capture, name, and reuse data multiple times throughout your program.
Practical Example 1: Storing and Reusing Input
This code captures the user's name and prints it back:
name=input("What is Your Name?")
print(name)
The
input()function gets the name (e.g.,"Sarah").The assignment operator (
=) stores that string value in the variable namedname.The
print(name)line doesn't print the word "name"; it prints the value stored inside thenamecontainer ("Sarah").
Practical Example 2: Storing Multiple Results
This is where variables really shine—when you need to perform multiple steps and store the result of each step:
name=input("What is Your Name?")
length=len(name)
print(length)
name = input(...): The user input (e.g.,"CodingBlog") is stored in thenamevariable.length = len(name): Thelen()function calculates the length of the string stored inname(which is 10). This numerical result is then stored in a new variable calledlength.print(length): The final numerical value, 10, is printed to the console.
By using separate variables (name and length), you can easily access the original name or its calculated length at any point in your script!
- by Chirana Nimnaka

Comments
Post a Comment