0% found this document useful (0 votes)
6 views6 pages

Python Input Types With Examples

The document provides examples of various input types in Python, including basic string, integer, float, list, tuple, dictionary, set, and boolean inputs. It also covers more advanced inputs like JSON-like dictionaries and multiple lines until EOF. Each example includes code snippets, expected input, and output formats.

Uploaded by

kiffaytullashaik
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)
6 views6 pages

Python Input Types With Examples

The document provides examples of various input types in Python, including basic string, integer, float, list, tuple, dictionary, set, and boolean inputs. It also covers more advanced inputs like JSON-like dictionaries and multiple lines until EOF. Each example includes code snippets, expected input, and output formats.

Uploaded by

kiffaytullashaik
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/ 6

Python Input Types with Examples

1. Basic String Input

Code:

s = input("Enter a string: ")

print(s)

Input:

Hello World

Output:

Hello World

2. Integer / Float Input

Code:

num = int(input("Enter an integer: "))

f = float(input("Enter a float: "))

print(num, f)

Input:

10

3.14

Output:

10 3.14

3. List of Integers (Array-style)

Code:

arr = list(map(int, input("Enter space-separated numbers: ").split()))

print(arr)

Input:

12345

Output:

[1, 2, 3, 4, 5]

4. List of Strings

Code:
Python Input Types with Examples

words = input("Enter words: ").split()

print(words)

Input:

apple banana mango

Output:

['apple', 'banana', 'mango']

5. 2D List (Matrix)

Code:

rows = int(input("Enter number of rows: "))

matrix = [list(map(int, input().split())) for _ in range(rows)]

print(matrix)

Input:

123

456

Output:

[[1, 2, 3], [4, 5, 6]]

6. Tuple Input

Code:

t = tuple(map(int, input("Enter tuple values: ").split()))

print(t)

Input:

10 20 30

Output:

(10, 20, 30)

7. Dictionary Input (Key-Value Pairs)

Code:

d = {}
Python Input Types with Examples

n = int(input("Enter number of items: "))

for _ in range(n):

key, value = input("Enter key and value: ").split()

d[key] = value

print(d)

Input:

name John

age 25

Output:

{'name': 'John', 'age': '25'}

8. Set Input

Code:

s = set(map(int, input("Enter unique elements: ").split()))

print(s)

Input:

12234

Output:

{1, 2, 3, 4}

9. Multiple Variables in One Line

Code:

a, b, c = map(int, input("Enter three numbers: ").split())

print(a, b, c)

Input:

5 10 15

Output:

5 10 15

10. List of Characters


Python Input Types with Examples

Code:

chars = list(input("Enter characters: "))

print(chars)

Input:

hello

Output:

['h', 'e', 'l', 'l', 'o']

11. Boolean Input

Code:

val = input("Enter true or false: ").lower() == "true"

print(val)

Input:

True

Output:

True

12. JSON-like Dictionary Input

Code:

import json

d = json.loads(input("Enter JSON: "))

print(d)

Input:

{"name": "Alice", "age": 30}

Output:

{'name': 'Alice', 'age': 30}

13. Input Using literal_eval

Code:

from ast import literal_eval

data = literal_eval(input("Enter a literal: "))


Python Input Types with Examples

print(type(data), data)

Input:

[10, 20, 30]

Output:

<class 'list'> [10, 20, 30]

14. Multiple Lines of Input

Code:

lines = []

for _ in range(3):

lines.append(input())

print(lines)

Input:

first line

second line

third line

Output:

['first line', 'second line', 'third line']

15. Read Until EOF (sys.stdin)

Code:

import sys

for line in sys.stdin:

print(line.strip())

Input:

Hello

World

Python

(then Ctrl+D)

Output:

Hello
Python Input Types with Examples

World

Python

You might also like