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

Python - Input and Output

The document covers Python's input and output functionalities, explaining how to take user inputs at runtime using the input() function and convert data types such as string to int and float. It provides example programs demonstrating these concepts and lists several type conversion functions available in Python. The document serves as a guide for understanding basic input/output operations and type conversions in Python programming.

Uploaded by

forunaveenadds
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)
9 views12 pages

Python - Input and Output

The document covers Python's input and output functionalities, explaining how to take user inputs at runtime using the input() function and convert data types such as string to int and float. It provides example programs demonstrating these concepts and lists several type conversion functions available in Python. The document serves as a guide for understanding basic input/output operations and type conversions in Python programming.

Uploaded by

forunaveenadds
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
You are on page 1/ 12

Data Science – Python Input and Output

8. PYTHON – INPUT AND OUTPUT

Table of Contents

1. Input and output ................................................................................................................................ 2


2. Hard coding......................................................................................................................................... 2
3. input(p) function ................................................................................................................................ 3
4. Convert from string to int .................................................................................................................. 5
5. Convert from string type to other type ............................................................................................. 7
6. Convert float data type value into int data type............................................................................... 9
7. List out few type conversion functions in python ........................................................................... 12

1|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

8. Input and Output

1. Input and output

✓ Input represents data given to the program.


✓ Output represents the result of the program.

2. Hard coding

✓ Till now we have executed examples by hard coding values to variables


✓ In this chapter we will learn how to take values at run time.

Program Hard coding the values


Name demo1.py

age = 16
print(age)

output
16

✓ Based on requirement we can take values at runtime or dynamically as


well.

Please enter the age: 16


Entered value is: 16

2|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

3. input(p) function

✓ input(p) is a predefined function.


✓ This function accepts input from the keyboard.
✓ This function takes a value from keyboard and returns it as a string type.
✓ Based on requirement we can convert from string to other types.

Program Printing name by taking value at run time


Name demo2.py

name = input("Enter the name:")


print("You entered name as:", name)

Run py demo2.py

Output
Enter the name: Daniel
You entered name as: Daniel

Program Printing name and age by taking value at run time


Name demo3.py

name = input("Enter the name: ")


age = input("Enter the age: ")

print("You entered name as: ", name)


print("You entered age as: ", age)

Run py demo3.py

Output
Enter the name: Prasad
Enter the age: 16

3|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

You entered name as: Prasad


You entered age as: 16

Program Checking return type value for input() function


Name demo4.py

value = input("Enter the value ")


print("Entered value as: ", value)
print("type is: ", type(value))

Run py demo4.py

Output
Enter the value: Daniel
Entered value as: Daniel
type is: <class ‘str’>

Run py demo4.py

Output
Enter the value:123
Entered value as: 123
type is: <class 'str'>

Run py demo4.py

Output
Enter the value:123.456
Entered value as: 123.456
type is: <class 'str'>

4|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

4. Convert from string to int

✓ We can convert the string value into int value.


✓ int(p) is a predefined function
✓ This function converts to int data type.

Program Converting from string to int data type


Name demo5.py

a = "123"
print(a)
print(type(a))

b = int(a)
print(b)
print(type(b))

Output

123
<class 'str'>
123
<class 'int'>

5|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

Program Converting from string to int data type


Name demo7.py

a = input("Enter a value:")
print("Your value is:", a)
print("data type is:", type(a))

b = int(a)
print("After converting the value is:", b)
print("data type is:", type(b))

Run py demo5.py

Output

Enter a value:123
Your value is: 123
data type is: <class 'str'>
After converting the value is: 123
data type is: <class 'int'>

6|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

5. Convert from string type to other type

✓ We can convert the string value into float value.


✓ float(p) is a predefined function
✓ This function converts to float data type.

Program Converting from string to float data type


Name demo8.py

a = "123.99"
print(a)
print(type(a))

b = float(a)
print(b)
print(type(b))

Output

123.99
<class 'str'>
123.99
<class 'float'>

7|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

Program Converting from string to float data type


Name demo9.py

a = input("Enter a value:")
print("Your value is:", a)
print("data type is:", type(a))

b = float(a)
print("After converting the value is:", b)
print("data type is:", type(b))

Run py demo9.py

Output

Enter a value:123.99
Your value is: 123.99
data type is: <class 'str'>
After converting the value is: 123.99
data type is: <class 'float'>

8|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

6. Convert float data type value into int data type

✓ int(p) is predefined function in python.


✓ This function converts value into int data type

Program Converting from float to int data type


Name demo10.py

a = 10000.45
print(a)
print(type(a))

b = int(a)
print(b)
print(type(b))

Output

10000.45
<class 'float'>
10000
<class 'int'>

9|Page 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

Program + operator concatenates/joins two string values


Name demo11.py

p1 = input("Enter First product price:")


p2 = input("Enter Second product price:")

print("Total price is:", p1+p2)

Run py demo11.py

Output

Enter First product price:111


Enter Second product price:222
Total price is: 111222

10 | P a g e 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

Program Converting string to int and perform + operator


Name demo12.py

p1 = input("Enter First product price:")


p2 = input("Enter Second product price:")

a = int(p1)
b = int(p2)

print("Total price is:", a+b)

Run py demo12.py

Output

Enter First product price:111


Enter Second product price:222
Total price is: 333

11 | P a g e 8.PYTHON-INPUT & OUTPUT


Data Science – Python Input and Output

7. List out few type conversion functions in python

1. int(p) – converts other data type into integer type


2. float(p) – converts other data type into float type
3. str(p) – converts other data type into a string.
4. bool(p) – converts other data type into boolean type
5. list(p) – converts sequence to list
6. tuple(p) – converts sequence to a tuple.
7. set(p) – converts sequence to set.
8. dict(p) – converts a tuple of order (key, value) into a dictionary.

12 | P a g e 8.PYTHON-INPUT & OUTPUT

You might also like