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:

  1. Python starts evaluating the print() function.

  2. It needs to figure out the value of "Hello " + input("what is name?").

  3. The input("what is name?") function runs first, displaying what is name? and waiting for the user (e.g., they type Maria).

  4. The input() function returns the string "Maria".

  5. The expression becomes "Hello " + "Maria", which results in "Hello Maria".

  6. 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:

  1. Python starts with the innermost function: input("What is your name?").

  2. The user types their name (e.g., Alex) and presses Enter. The input() function returns the string "Alex".

  3. Now the expression is print(len("Alex")).

  4. The len() function runs on "Alex". Since "Alex" has 4 characters, len() returns the number 4.

  5. 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)

  1. The input() function gets the name (e.g., "Sarah").

  2. The assignment operator (=) stores that string value in the variable named name.

  3. The print(name) line doesn't print the word "name"; it prints the value stored inside the name container ("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)

  1. name = input(...): The user input (e.g., "CodingBlog") is stored in the name variable.

  2. length = len(name): The len() function calculates the length of the string stored in name (which is 10). This numerical result is then stored in a new variable called length.

  3. 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

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python