Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python Tutorial_ Keyboard Input

The document provides an overview of the input function in Python, explaining how it captures user input from the keyboard and returns it as a string. It includes examples demonstrating the use of the input function and its behavior in different Python versions. Additionally, it highlights the importance of understanding input handling to avoid programming mistakes.

Uploaded by

Sentinel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Tutorial_ Keyboard Input

The document provides an overview of the input function in Python, explaining how it captures user input from the keyboard and returns it as a string. It includes examples demonstrating the use of the input function and its behavior in different Python versions. Additionally, it highlights the importance of understanding input handling to avoid programming mistakes.

Uploaded by

Sentinel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Course

Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact

Previous Chapter: An Extensive Example Using Sets


Next Chapter: Conditional Statements

Input from Keyboard

The input Function


Follow Bernd Klein,
the author of this
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
website, at Google+:
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
Bernd Klein on
prompt string.
Python 3 Google
Tutorial 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
Bernd Klein on
parameter, i.e. the prompt, will be printed on the screen.
The Origins of Facebook
Python 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
Starting with either a casting function or the eval function.
Search this website:
Python: The
Interactive Shell Let's have a look at the following example:
Go
Executing a
name = input("What's your name? ")
Script print("Nice to meet you " + name + "!") This topic in German
Indentation age = input("Your age? ") / Deutsche
Data Types and print("So, you are already " + age + " years old, " + name + "!") Übersetzung:
Variables Eingabe von der
Operators Tastatur
We save the program as "input_test.py" and run it:
Sequential Data
Types: Lists and $ python input_test.py Python 3
Strings What's your name? "Frank"
Nice to meet you Frank! This is a tutorial in
List
Your age? 42
Manipulations Python3, but this
So, you are already 42 years old, Frank!
Shallow and chapter of our course
is available in a
Deep Copy
We will further experiment with the input function in the following interactive Python session: version for Python
Dictionaries
2.x as well: Keyboard
Sets and Frozen >>> cities_canada = input("Largest cities in Canada: ") Input in Python 2.x
Sets Largest cities in Canada: ["Toronto", "Montreal", "Calgara", "Ottawa"]
An Extensive >>> print(cities_canada, type(cities_canada))
Classroom
Example Using ["Toronto", "Montreal", "Calgara", "Ottawa"] <class 'str'>
>>> Training
Sets >>> cities_canada = eval(input("Largest cities in Canada: ")) Courses
input via the Largest cities in Canada: ["Toronto", "Montreal", "Calgara", "Ottawa"]
keyboard >>> print(cities_canada, type(cities_canada)) The goal of this
['Toronto', 'Montreal', 'Calgara', 'Ottawa'] <class 'list'>
Conditional website is to provide
>>>
Statements >>> population = input("Population of Toronto? ") educational material,
Loops, while Population of Toronto? 2615069 allowing you to learn
Loop >>> print(population, type(population)) Python on your own.
2615069 <class 'str'> Nevertheless, it is
For Loops
>>> faster and more
Difference >>> population = int(input("Population of Toronto? ")) efficient to attend a
between Population of Toronto? 2615069 "real" Python course
interators und >>> print(population, type(population))
in a classroom, with
2615069 <class 'int'>
Iterables >>> an experienced
Output with Print trainer. So why not
Formatted output attend one of the live
with string Python courses in
Strasbourg, Paris,
modulo and the
Luxembourg,
format method Differences to Python2
Amsterdam, Zürich /
Functions Zurich, Vienna /
Recursion and 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, there the input function behaves like the raw_input Wien, London, Berlin,
Recursive function from Python2. Munich, Hamburg,
Functions Frankfurt, Stuttgart,
The changes between the versions are illustrated in the following diagram: or Lake Constance by
Parameter
Passing in Bernd Klein, the
Functions author of this
tutorial?
Namespaces
Global and Local
You can book on-site
Variables classes at your
Decorators company or
Memoization with organization, e.g. in
Decorators England, Switzerland,
Read and Write Austria, Germany,
Files France, Belgium, the
Netherlands,
Modular
Luxembourg, Poland,
Programming
UK, Italy and other
and Modules locations in Europe
Packages in and in Canada.
Python
Regular We had courses in
Expressions the following cities:
Regular Amsterdam (The
Expressions, Netherlands), Berlin
(Germany), Bern
Advanced
(Switzerland), Basel
Lambda
(Switzerland), Zurich
Operator, Filter, (Switzerland),
Reduce and Map Locarno
List (Switzerland), Den
Comprehension Haag (The Hague),
Iterators and Hamburg, Toronto
Previous Chapter: An Extensive Example Using Sets (Canada), Edmonton
Generators
Next Chapter: Conditional Statements (Canada), Munich
Exception
(Germany) and many
Handling
other cities.
Tests, DocTests,
UnitTests Contact us so we can
Object Oriented define and find the
Programming best course
Class and curriculum to meet
Instance your needs, and
Attributes schedule course
sessions to be held at
Properties vs.
your location.
getters and
setters
Inheritance
Skilled Python
Multiple Programmers
Inheritance
Magic Methods You are looking for
and Operator experienced Python
Overloading developers or
OOP, Inheritance programmers? We
Example can help you, please
Slots contact us.
Classes and
Class Creation Quote of the
Road to
Day:
Metaclasses
"Clearly,
Metaclasses
programming courses
Metaclass Use
should teach
Case: Count methods of design
Function Calls and construction, and
Abstract Classes the selected
examples should be
such that a gradual
development can be
nicely
demonstrated."
(Niklaus Wirth)

Data Protection
Declaration

Data Protection
Declaration

© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein

You might also like