Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Special Methods
Python basic
The input() function
The input() function in Python is used to take user input from the console. It allows you to interact with the user by prompting them to enter data, which can then be processed in your program.
Basic usage
The input() function pauses program execution and waits for the user to type something in the console. Once the user presses Enter, the function returns the input as a string.
Basic usage
The input() function pauses program execution and waits for the user to type something in the console. Once the user presses Enter, the function returns the input as a string.
user_input = input("Enter a value: ")
- The string passed as an argument to input() is the prompt that appears to the user.
- The returned value is always of type string, regardless of what the user enters.
Example:
name = input("What is your name? ") print(f"Hello, {name}!")
Output:
What is your name? Alice Hello, Alice!
In this example:
- The program prompts the user for their name.
- After entering their name, it greets them using the input.
This function is fundamental in creating interactive programs that respond to user input, making your applications more dynamic and engaging.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What is the purpose of the input() function in Python?
DTo create a function
ATo take user input from the console
BTo print output to the console
CTo declare a variable
Check Answer
What type does input() return?
AInteger
BFloat
CString
DBoolean
Check Answer
What will the following code output: name = input("What is your name? "); print(f"Hello, {name}!")
DNone
A"Hello, name!"
B"What is your name?"
C"Hello, {name}!"
Check Answer
What happens when input() is called?
AThe program waits for user input
BThe program terminates
CThe program skips to the next line
DThe program generates a random number
Check Answer
How can input() make a program interactive?
CBy displaying messages to the user
DBy calculating results internally
ABy running automatically without user input
BBy allowing users to provide data for processing
Check Answer