0% found this document useful (0 votes)
60 views12 pages

Python #3: Let's Get Interactive

This document discusses taking user input in Python programs and converting between data types. It explains how to use the input() function to get input from the user as a string, and how to wrap input() in functions like int() and float() to convert the input to integer or float data types. It also covers type casting in Python, including converting between integers, floats and strings. Examples are provided of taking integer, float and descriptive user input and printing the values and data types.

Uploaded by

Darkbox
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views12 pages

Python #3: Let's Get Interactive

This document discusses taking user input in Python programs and converting between data types. It explains how to use the input() function to get input from the user as a string, and how to wrap input() in functions like int() and float() to convert the input to integer or float data types. It also covers type casting in Python, including converting between integers, floats and strings. Examples are provided of taking integer, float and descriptive user input and printing the values and data types.

Uploaded by

Darkbox
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python #3 Let’s get interactive

User Inputs for Interactive Experience


• Get inputs from user.
• Console will wait for user input
• User can input characters, numbers, white space and special
characters
• Syntax input()
• By default user input is a string type.
• Try it! Ask for user input and store in a variable ‘a’. Print a along with
the data type.
Making User Input descriptive
• Within the input brackets you can write any description, to let user
know what should he/she do.
• input(“Enter your name: “)
• Try it! Ask your to input name, store it in a variable and print the
name along with the data type.

• Tip: Always leave a space after colon in the description. WHY?


• WHY? Try with and without space after colon.
• User may get confuse. It is not presentable.
Taking only integer input from the user
• What are the situations when you need only integer input from the
user? Please discuss.
• How to do it?
• Wrap input() with int().
• int is the data type for integers. Remember!

• Syntax: int(input(“Please enter your pin code: “))


• What would happen if user tries to enter text in this case? Try it and
note down any errors.
Explain type casting
• How to convert one data type into another
• Valid rules – what can you convert
• Refer next two slides
Data Type Conversion
● Type conversion is the process of converting value of one data type (int,
float, str etc.) to other data type.
○ Integer to float
○ Float to integer
○ Integer to string
○ Float to string
● Example:
int(any_number) float(any_number)
Try all data type (int, float, str) conversion.
Type Conversion
Int to float

Type of Type of
data data
Taking only float input from the user
• What are the situations when you need only float input from the user? Please
discuss.
• How to do it?
• Wrap input() with float().
• float is the data type for float. Remember!

• Syntax: float(input(“Please enter your weight: “))


• What would happen if user tries to enter text in this case? Try it and note down
any errors.
• What would happen if user tries to enter integer in this case? Try it and note
down any errors.
Consider • What would happen if the
you are user enter only integers in the
using the input. Will it work?
default • Can you change the user input
from string to integer? Try it
syntax of and print it in float datatype
user input
Exercise
Consider • What would happen if the
you are user enter only float in the
using the input. Will it work?
default • Can you change the user input
from string to float? Try it and
syntax of print it in float data type
user input
What are the benefits of user
input?
What are difference ways in
which a user can input?
• Discuss how are modern address books
created. You may refer to Microsoft
Outlook or any email service provider
such as Hotmail, Gmail, Rediffmail,
Yahoo, etc.
• Note down what are the different fields
Exercise in the address book when you create a
new contact. You look at advanced
option or add more fields.
• Write a Python Program to create a
new contact. Store it in variables with
the correct data types. Print each
variable along with the correct data
type.
What did you learn today?
Please summarize following:
Thank you! • User Inputs
• Type Casting

You might also like