Saturday 23 May 2020

How to take input in Python?

How to read input in Python?

In Python 3, input() function is used for taking input from the keyboard. The input() function in Python 3 is different from Python 2.

In Python 3, input treats everything as a string, while in Python 2 it is not.

For example, if you try to read integer value 10 using input() in Python 3, it will consider 10 as string.
But in Python 2, it remains as it is.

Lets have a look at the code given below,

Program

#Program: To take the input from keyboard
#Author: Hitendra E. Suryavanshi
a=input("Enter any value:")
print("Value is ",a)
print("Data type of a is:",type(a))

Output
Enter any value:10
Value is  10
Data type of a is: <class 'str'>

Here, you can see data type of 'a' is 'str' and not 'int'. Because, in Python 3, everything is returned as string.  If you try with other values, like float, it is also considered as string.

How can we solve this problem?

Typecasting:
We have to use typecasting (explicit) in order to solve the above problem.

#Program: To take the input from keyboard
#Author: Hitendra E. Suryavanshi
a=int(input("Enter any value:"))
print("Value is ",a)
print("Data type of a is:",type(a))

Output:

Enter any value:11
Value is  11
Data type of a is: <class 'int'>

Here, the value of a is 10 and so the data type is 'int'.

How can we read single character in Python?

In Python, unlike C, we have only one data type i.e. string (str), not Char.
Therefore you can read single character or multiple character at the same time. But all of them ar treated as a string.

Ex. a="c"
Ex. b="cat"

Here, the data type of both a and b are string.



Thank you
H. E. Suryavanshi










Python Program to read specific elements from a List

What is List in a Python?

List is a collection of elements like a array in C, C++. But array contains all elements of same type, while in a list its not compulsory to have same type of elements.

How to create a List?

list_name=list()
Ex. a=list()

or

list_name=[element-1, element-2,....]
Ex. a=[1, 3.14, "Python"]


How to access the elements of a List?

In Python, list elements can be accessed by indexing. Index value can be integer (Positive or Negative).

Let a be the list containing five elements:
a=[10,20,30,40,50]

If we print a[0], it will display first number i.e. 10.
For a[2], it will display 30.

If we pass negative value as -1 i.e a[-1], it will display last number (50).
For accessing second last element, put a[-2].

Note:
Negative indexing starts from Left to Right.


Program 

The following program will print the number as given position.

#Program: To read specific elements from a list
#Author: Hitendra E. Suryavanshi

a=[int(x) for x in input("Enter the numbers:").split()]
pos=int(input("Enter position to access number:"))
print("The No. is :",a[pos])

Output-1:

Enter the numbers:10 20 30 40 50
Enter position to access number:2
The No. is : 30

Output-2:


Enter the numbers:10 20 30 40 50
Enter position to access number:-1
The No. is : 50



Thank You
Hitendra E. Suryavanshi
M.Tech.(IT), BE (IT)






Python Program to check Divisibility of two numbers

Divisibility of Numbers

Let a and b be the two numbers.
Divisibility says that b can completely divides a if their remainder is Zero.
i.e. a%b=0

Program:

In this program, we will check the divisibility of numbers by applying simple formula given above.

#Program: To check divisibility of two numbers
#Author: Hitendra E. Suryavanshi

a=int(input("Enter a number"))
b=int(input("Enter a number"))
if a%b==0:
    print("a is fully divisible by b")
else:
    print("a is not fully divisible by b")

Output 1:

Enter a number10
Enter a number5
a is fully divisible by b

Output 2:

Enter a number7
Enter a number2
a is not fully divisible by b


Thank You
Hitendra E. Suryavanshi
M.Tech.(IT), BE (IT)







Python program to build Dictionary using sentence

What is a Dictionary in Python Programming?

Dictionary is a special data type in a python. It stored record in the form of (Key, Value). Each entry in the dictionary is accessed by 'Key' only. You cannot use index (like List, Tuple) for accessing the elements.

How to create Dictionary?

dictionary_name=dict()

Ex. d=dict()

or

dictionary_name={key1:value1, key2:value2,.....}

Ex. d={1:'Apple', 2:'Ball', 3:'Cat'}

Program 1:

In this program, one string (sentence) is given as input. 
Once, input is received, it is splitted on the basis of blank space, which results into a list.
Therefore, 'str' contains list of words and not string/sentence.
After that we will traverse through the list and simply add each word into a dictionary in (Key, Value) pair.

#Program: To create Dictionary using String Senetence
#Author: Hitendra E. Suryavanshi

str=input("Enter a sentence").split()
d=dict()
for word in str:
    d[word[0]]=word
print(d)

So, the output of the program is dictionary 'd', given as below.

Output 1:

Enter a sentence:Welcome To India
{'W': 'Welcome', 'T': 'To', 'I': 'India'}

The problem with this program is that, if you have two or more words starting with same character, it will keep only last entry into the dictionary. Because it updates the entry for that character.

Look at the example  below.
Here two words 'Python' and 'Programming' starts with same character 'P'.
Therefore, it will keep only last entry i.e. 'Programming' for character 'P'.

Output 2:

Enter a sentence:Python is Programming Language
{'P': 'Programming', 'i': 'is', 'L': 'Language'}


Program 2:

The problem in program 1 is solved into program 2, given below.
Here, in order to solve the problem, we first check for the 'Key', wether that 'Key' exists in a Dictionary or not?.
If not, then we will add new record (key, value) to the dictionary.
If yes, then we will read previous entries and append new 'value' to it, and update the dictionary.

Go through the below program for the answer.

#Program: To create Dictionary using String Senetence
#Author: Hitendra E. Suryavanshi

str1=input("Enter a sentence:").split()
d=dict()
for word in str1:
    key=word[0]
    value=word
    if key in d:
        d[key]=d[key]+[value]
    else:
        d[key]=[value]
print(d)


Enter a sentence:Python is Programming Language
{'P': ['Python', 'Programming'], 'i': ['is'], 'L': ['Language']}


Thank You
Hitendra E. Suryavanshi
M.Tech.(IT), BE (IT)

How to take input in Python?

How to read input in Python? In Python 3, input() function is used for taking input from the keyboard. The input() function in Python 3 i...