#18 Python Tutorial for Beginners | User input in Python | Command Line Input

#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.argv can be used to pass multiple values from the command line when running a Python code.
  • The index number of sys.argv starts 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.

Video description

Check out our courses: AI Powered DevOps with AWS - Live Course :- https://go.telusko.com/AIDevOps-AWS Coupon: TELUSKO10 (10% Discount) Complete Java Developer Course Batch-4: https://go.telusko.com/Complete4 Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) Udemy Courses: Spring: https://go.telusko.com/udemyteluskospring Java:- https://go.telusko.com/udemyteluskojava Java Spring:- https://go.telusko.com/Udemyjavaspring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 TELUSKO Android App : https://bit.ly/TeluskoApp In this lecture we are discussing about: - How to get user input - input function - printing input message - Type of input data - When to use index value - eval function - Passing values from command line - Argument line input #1 How to get user input -- Getting user input in Python is straightforward. You can use the input() function to get input from the user. The input function takes a single argument, which is the prompt message displayed to the user. e.g name = input("Please enter your name: ") x=input("Enter first number: "); y=input("Enter second number: "); z=x+y; print(z); #2 input function -- In Python, the input() function is used to accept user input from the command line or console. name=input("Enter your name:"); print(name); -- In this example, the input() function prompts the user to enter their name. Whatever the user types in response is stored in the name variable. -- Note that the input() function always returns a string, so if we want to use the user's input as a number, we'll need to convert it using the appropriate type-casting function (e.g., int() for integers or float() for floating-point numbers). #3 Types of input data -- The input() function always returns a string, regardless of what the user enters. we may need to convert the input to a different data type if you want to perform calculations or other operations on it. e.g x=input("Enter first number: "); a=int(x); the input entered by the user is converted to an integer using the int() function in this example. #4 when to use index value -- If you want to get a single character from the user, we can use the input() function and index the result. e.g ch=input('enter a character: '); print(ch[0]) ch=input('enter a character: ')[0]; print(ch); #5 eval function eval function -- The eval() function in Python is used to evaluate an expression entered by the user as a string. The eval() function returns the result of the expression as a value. e.g x=eval(input("Enter an expression: ")); typeOf = type(x); print(typeOf); #6 Passing values from command line -- sys module provides access to any command-line arguments via the sys.argv list. we can pass arguments to a Python script from the command line using the sys.argv list. The first argument in the list is always the name of the script itself. suppose we have a file named Mycode.py in file we have written code import sys # without this line you will get error x=sys.argv[1]; y=sys.argv[2]; z=x+y; print(z); in command line we have to run this file #python3 Mycode.py 9 5 0 1 2 Note: Mycode is count as 0th argument 9 is count as 1st argument 5 is count as 2nd argument Github :- https://github.com/navinreddy20/Python- Python for Beginners :- http://bit.ly/3JOLQhl Editing Monitors : https://amzn.to/2RfKWgL https://amzn.to/2Q665JW https://amzn.to/2OUP21a. Java:- https://bit.ly/JavaUdemyTelusko Spring:- https://bit.ly/SpringUdemyTelusko More Learning : Java :- https://bit.ly/3x6rr0N Python :- https://bit.ly/3GRc7JX Django :- https://bit.ly/3MmoJK6 JavaScript :- https://bit.ly/3tiAlHo Node JS :- https://bit.ly/3GT4liq Rest Api :-https://bit.ly/3MjhZwt Servlet :- https://bit.ly/3Q7eA7k Spring Framework :- https://bit.ly/3xi7buh Design Patterns in Java :- https://bit.ly/3MocXiq Docker :- https://bit.ly/3xjWzLA Blockchain Tutorial :- https://bit.ly/3NSbOkc Corda Tutorial:- https://bit.ly/3thbUKa Hyperledger Fabric :- https://bit.ly/38RZCRB NoSQL Tutorial :- https://bit.ly/3aJpRuc Mysql Tutorial :- https://bit.ly/3thpr4L Data Structures using Java :- https://bit.ly/3MuJa7S Git Tutorial :- https://bit.ly/3NXyCPu Donation: PayPal Id : navinreddy20 https://www.telusko.com