#18 Python Tutorial for Beginners | User input in Python | Command Line Input
Python Input Function
In this video, the presenter discusses how to get input from a user in Python using the input function.
Using Functions in Python
- The presenter explains that everything done in Python so far has been done with the help of functions.
- Examples of functions used include print and calculations like finding the sum or minimum value of a list.
Getting User Input
- The presenter introduces the input function as a way to get user input.
- To use the input function, simply call it and assign its value to a variable.
- The presenter demonstrates how to prompt the user for input by adding messages before calling the input function.
- For example: "Enter first number:"
- However, when using the input function, it always returns a string type even if you enter an integer value.
- To convert this string into an integer, use the int() function.
Troubleshooting User Input
- If there is an issue with getting user input, such as unexpected spaces between values entered by users, it can cause errors when trying to perform calculations on those values.
- To avoid these issues, make sure to properly format and convert user inputs before performing any calculations.
Python Input Function Overview
In this video, the instructor explains how to use the input function in Python. The video covers how to convert string format into the desired format using functions like float and integer. It also shows how to take input from a user in character format and evaluate expressions using the eval function.
Converting String Format
- Use functions like float and integer to convert string format into desired format.
- Avoid wasting lines of code by using int instead of converting input from string to integer.
Taking Input in Character Format
- Use input function with a variable assigned as a character (e.g., CH) to take input in character format.
- To print only one character, use index values when fetching the input.
- Use double quotes or single quotes for characters. Both work in Python.
Evaluating Expressions Using Eval Function
- Use eval function to evaluate expressions entered by users through input function.
- Pass expression entered by user as an argument to eval function and assign result to a variable (e.g., result).
Passing Values to Python Code
In this section, the instructor explains how to pass values from the command line when running a Python code.
Using sys.argv to Pass Values
- The special keyword
sys.argvcan be used to pass multiple values from the command line when running a Python code.
- The index number of
sys.argvstarts at 0, with index 0 being the file name and subsequent indices representing passed values.
- To access a particular value in
sys.argv, use its corresponding index number.
- When passing values using
sys.argv, ensure that they are converted into integers before performing any arithmetic operations on them.
Conclusion
In this section, the instructor concludes the video by encouraging viewers to subscribe for more Python tutorials and like the video if they found it helpful.
- Viewers are encouraged to subscribe for more Python tutorials and like the video if they found it helpful.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.