0% found this document useful (0 votes)
4 views31 pages

Interacting With Python Programs

The document provides an overview of input and output operations in Python, emphasizing the importance of user interaction through input and output functions. It explains how to use the print() function for displaying output and the input() function for receiving user data, along with type conversion techniques. Additionally, it includes practical examples, best practices, and exercises to reinforce the concepts presented.

Uploaded by

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

Interacting With Python Programs

The document provides an overview of input and output operations in Python, emphasizing the importance of user interaction through input and output functions. It explains how to use the print() function for displaying output and the input() function for receiving user data, along with type conversion techniques. Additionally, it includes practical examples, best practices, and exercises to reinforce the concepts presented.

Uploaded by

kavya.gupta.ug25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Interacting with Python Programs

Input and Output Operations

1 / 31
Contents

1 Introduction

2 Output in Python

3 Input in Python

4 Type Conversion

5 Complete Examples

6 Best Practices

7 Practice Exercises

8 Summary

2 / 31
What is Program Interaction

Definition
Interaction means communication between user and program.

Two types of interaction:


Input - User gives data to program
Output - Program shows results to user

3 / 31
Why Interaction is Important

Benefits:
Makes programs dynamic
Different results for different inputs
User-friendly programs
Real-world applications

4 / 31
Output in Python

print() function
Used to display output on the screen.

Purpose:
Show results to user
Display messages
Debug programs

5 / 31
Simple print Examples

# Example 1: Print text


print ( " Hello , World ! " )

# Example 2: Print numbers


print (10)
print (3.14)

# Example 3: Print multiple items


print ( " Python " , " is " , " fun " )

# Example 4: Print calculation


print (5 + 3)

6 / 31
Output of Examples

When you run the code:


Hello , World !
10
3.14
Python is fun
8

7 / 31
print with Multiple Values

# Printing multiple values


name = " John "
age = 20
city = " Delhi "

print ( " Name : " , name )


print ( " Age : " , age )
print ( " City : " , city )

# All in one line


print ( " Name : " , name , " Age : " , age )

8 / 31
Formatted Output

Using comma:
print ( " Python " , " Programming " , " Language " )
# Output : Python Programming Language

Using plus:
print ( " Python " + " " + " Programming " )
# Output : Python Programming

9 / 31
print Special Parameters

sep parameter:
print ( " Apple " , " Banana " , " Cherry " , sep = " , " )
# Output : Apple , Banana , Cherry

end parameter:
print ( " Hello " , end = " " )
print ( " World " )
# Output : Hello World

10 / 31
Input in Python

input() function
Used to get data from the user.

Important:
input() always returns a string
Need to convert for numbers
Program waits for user input

11 / 31
Simple input Examples

# Example 1: Getting name


name = input ( " Enter your name : " )
print ( " Hello " , name )

# Example 2: Getting age


age = input ( " Enter your age : " )
print ( " You are " , age , " years old " )

# Example 3: Getting color


color = input ( " Favorite color ? " )
print ( " Your favorite color is " , color )

12 / 31
How input Works

When program runs:


1 Shows the message
2 Cursor waits for input
3 User types and presses Enter
4 Value stored in variable
5 Program continues

13 / 31
Why Type Conversion

Problem
input() returns everything as string

Solution:
Use type conversion functions
Convert string to required type

14 / 31
Type Conversion Functions

Function Purpose
int() Convert to integer
float() Convert to decimal
str() Convert to string
bool() Convert to boolean

15 / 31
int() Convert to Integer

Use for whole numbers:


# Getting integer input
age = int ( input ( " Enter your age : " ) )
print ( " Next year you will be " , age + 1)

# Another example
num1 = int ( input ( " Enter first number : " ) )
num2 = int ( input ( " Enter second number : " ) )
sum = num1 + num2
print ( " Sum is : " , sum )

16 / 31
float() Convert to Decimal

Use for decimal numbers:


# Getting decimal input
height = float ( input ( " Enter height : " ) )
print ( " Your height is " , height , " meters " )

# Calculator example
price = float ( input ( " Enter price : " ) )
quantity = float ( input ( " Enter quantity : " ) )
total = price * quantity
print ( " Total cost : " , total )

17 / 31
Common Mistakes

Mistake 1: Forgetting conversion


# WRONG
age = input ( " Enter age : " )
print ( age + 5) # ERROR

# CORRECT
age = int ( input ( " Enter age : " ) )
print ( age + 5) # Works

18 / 31
Example 1: Simple Calculator

# Simple Addition Calculator

print ( " Addition Calculator " )

# Get input
num1 = float ( input ( " Enter first number : " ) )
num2 = float ( input ( " Enter second number : " ) )

# Calculate
sum = num1 + num2

# Display result
print ( " Sum is : " , sum )

19 / 31
Example 2: Area of Rectangle

# Rectangle Area Calculator

print ( " Calculate Rectangle Area " )

# Get dimensions
length = float ( input ( " Enter length : " ) )
width = float ( input ( " Enter width : " ) )

# Calculate area
area = length * width

# Display result
print ( " Area is : " , area )

20 / 31
Example 3: Temperature Converter

# Celsius to Fahrenheit

print ( " Temperature Converter " )

# Get input
celsius = float ( input ( " Enter temp in C : " ) )

# Convert
fahrenheit = ( celsius * 9/5) + 32

# Display
print ( celsius , " C = " , fahrenheit , " F " )

21 / 31
Example 4: Personal Information

# Personal Information Program

print ( " Enter Your Information " )

# Get inputs
name = input ( " Enter your name : " )
age = int ( input ( " Enter your age : " ) )
city = input ( " Enter your city : " )

# Display
print ( " Name : " , name )
print ( " Age : " , age , " years " )
print ( " City : " , city )

22 / 31
Example 5: Grade Calculator

# Grade Calculator

print ( " Calculate Average Grade " )

# Get marks
math = float ( input ( " Math marks : " ) )
science = float ( input ( " Science marks : " ) )
english = float ( input ( " English marks : " ) )

# Calculate
total = math + science + english
average = total / 3

# Display
print ( " Total : " , total )
print ( " Average : " , average )

23 / 31
Best Practices

1 Use clear messages


2 Convert input to correct type
3 Use meaningful variable names
4 Add helpful comments
5 Test with different inputs
6 Format output nicely

24 / 31
Good vs Bad Code

Bad Code:
a = input ()
b = input ()
print ( int ( a ) + int ( b ) )

Good Code:
# Add two numbers
num1 = int ( input ( " Enter number 1: " ) )
num2 = int ( input ( " Enter number 2: " ) )
sum = num1 + num2
print ( " Sum : " , sum )

25 / 31
Practice Exercise 1

Task: Create a program to calculate circle area

Steps:
1 Get radius from user
2 Calculate area using pi times r times r
3 Display the result

Formula: Area = 3.14 * radius * radius

26 / 31
Exercise 1 Solution

# Circle Area Calculator

print ( " Calculate Circle Area " )

# Get input
radius = float ( input ( " Enter radius : " ) )

# Calculate area
pi = 3.14
area = pi * radius * radius

# Display result
print ( " Area of circle : " , area )

27 / 31
Practice Exercise 2

Task: Create a simple interest calculator

Get from user:


Principal amount
Rate of interest
Time period in years

Formula: SI = (P * R * T) / 100

28 / 31
Exercise 2 Solution

# Simple Interest Calculator

print ( " Simple Interest Calculator " )

# Get inputs
principal = float ( input ( " Principal : " ) )
rate = float ( input ( " Rate : " ) )
time = float ( input ( " Time in years : " ) )

# Calculate
si = ( principal * rate * time ) / 100
total = principal + si

# Display
print ( " Simple Interest : " , si )
print ( " Total Amount : " , total )

29 / 31
Important

print() for output


input() for input
input() returns string
Use int() for whole numbers
Use float() for decimals
Always give clear messages
Format output properly

30 / 31
Function Example
print() print(”Hello”)
input() name = input(”Name: ”)
int() age = int(input(”Age: ”))
float() price = float(input(”Price: ”))

31 / 31

You might also like