[ Web Proxy ]
URL:
Viewing: https://python-course.eu/python-tutorial/input.php [Back]  [Original]

15. Keyboard Input | Python Tutorial | python-course.eu
python-course.eu
Search site:

Live Python classes by highly experienced instructors:

instructor-led training course [instructor-led training course]
Instructor-led training courses by Bernd Klein

  1. Intro to Python Tutorial
  2. History and Philosophy of Python
  3. The Interpreter, an Interactive Shell
  4. Execute a Script
  5. Structuring with Indentation
  6. Assignment Expressions
  7. Data Types and Variables
  8. Type Annotations
  9. Operators
  10. Sequential Data Types
  11. List Manipulation
  12. Shallow and Deep Copy
  13. Dictionaries
  14. Sets and Frozen Sets
  15. Sets Examples
  16. Keyboard Input
  17. Conditional Statements
  18. Structural Pattern Matching
  19. Adventure Game with Structural Pattern Matching
  20. While Loops
  21. For Loops
  22. Output with Print
  23. Formatted Output
  24. Working with Dictionaries and while Loops
  25. Functions
  26. Passing Arguments
  27. Parameters And Arguments
  28. Namespaces
  29. Global vs. Local Variables and Namespaces
  30. File Management
  31. Modular Programming and Modules
  32. Packages
  33. Errors and Exception Handling

input mini [input mini]

This website contains a free and extensive online tutorial by Bernd Klein, using material from his classroom Python training courses.

If you are interested in an instructor-led classroom training course, have a look at these Python classes:

instructor-led training course [instructor-led training course]
Instructor-led training course by Bernd Klein at Bodenseo

Image kabliczech - Fotolia.com

This page was written by Bernd Klein.

Bernd is an experienced computer scientist with a history of working in the education management industry and is skilled in Python, Perl, Computer Science, and C++. He has a Dipl.-Informatiker / Master Degree focused in Computer Science from Saarland University.

PDF versions of this site [PDF versions of this site]

PDF logo [PDF logo] PDF version of this site

This website is free of annoying ads. We want to keep it like this. You can help with your donation:

[Go]

The need for donations

15. Keyboard Input

By Bernd Klein. Last modified: 29 Jun 2022.

The input Function

There are hardly any programs without any input. Input can come in various ways, for example, from a database, another computer, mouse clicks and movements or from the internet. Yet, in most cases the input stems from the keyboard. For this purpose, Python provides the function input(). input has an optional parameter, which is the prompt string.

If the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return key. The text of the optional parameter, i.e. the prompt, will be printed on the screen.

Input via keyboard [Input via keyboard]

The input of the user will be returned as a string without any changes. If this raw input has to be transformed into another data type needed by the algorithm, we can use either a casting function or the eval function.

Let's have a look at the following example:

name = input("What's your name? ")
print("Nice to meet you " + name + "!")

OUTPUT:

What's your name? Melisa
Nice to meet you Melisa!
age = input("Your age? ")
print("So, you are already " + age + " years old, " + name + "!")

OUTPUT:

Your age? 26
So, you are already 26 years old, Melisa!

We save the program as "input_test.py" and run it:

 $ python input_test.py 
What's your name? "Frank"
Nice to meet you Frank!
Your age? 42
So, you are already 42 years old, Frank! 

We will further experiment with the input function in the following interactive Python session:

cities_canada = input("Largest cities in Canada: ")
print(cities_canada, type(cities_canada))

OUTPUT:

Largest cities in Canada: Toronto, Calgary, Montreal, Ottawa
Toronto, Calgary, Montreal, Ottawa <class 'str'>
cities_canada = eval(input("Largest cities in Canada: "))
print(cities_canada, type(cities_canada)) 

OUTPUT:

Largest cities in Canada: Toronto, Calgary, Montreal, Ottawa
population = input("Population of Toronto? ")
print(population, type(population))

OUTPUT:

Population of Toronto? 2615069
2615069 <class 'str'>
population = int(input("Population of Toronto? "))
print(population, type(population))

OUTPUT:

Population of Toronto? 2615069
2615069 <class 'int'>

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

Differences between Python2 and Python3

The usage of input or better the implicit evaluation of the input has often lead to serious programming mistakes in the earlier Python versions, i.e. 2.x Therefore, in Python 3 the input function behaves like the raw_input function from Python2.

The changes between the versions are illustrated in the following diagram:

Differences between input, raw_input and the Python versions [Differences between input, raw_input and the Python versions]

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

top


Web Proxy Viewer  |  New URL  |  Original Page