Basic Program Fundamentals in Python

Share via:

Basic Program Fundamentals in Python

How To Take Input From User In Python

We use The input() Function to take the input from the user in python.for this we don’t need to import any libraries in python.

Syntax:

Some point on input() function in python

1. Whatever we take input from the user will be in the string format by default.

2. You can change into your desired data type from the string data type.

Taking input from the user is as follows

Output

The value or string that you give in the user prompt will be stored in the variable, in the above case the string “9” will be stored in the s.

At the time of taking input from the user only we can type cast in to the required data type as follows :

Syntax: variable=required datatype (input())

Ex1: if you want to take integer value from the user

Syntax : s=int(input())

After taking input from the user it will be type casted from string to int

Ex 2: if you want to take a float value from the user.

Syntax : s=float(input())

Example:

The above format is used when you want to take a single input from the user. If you want to provide any information while entering data then, follow this syntax Syntax: Input(“Enter Message to display ”)

Example For displaying message while taking input from the user

How to take Multiple Inputs in Python

If you want to take multiple values from the user of the same data type you need some functions like map,split

Ex: if you want to take two values of same data type from user then use map function Syntax : a,b=map(int,input().split())

Example

How to take inputs for the Sequence Data Types like List, Set.

In this sequence type data types we can take input in two ways

1.using a for loop or while loop and add the elements using append or add

2.using the list,set,map functions.

Example using loops and append function

Output:

Another way of taking sequential input is to use list and set functions along with the map,by using this you can get many inputs from the user.

Example using set and list functions with map

About print() in python

Syntax:

print(object(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush) About the syntax:

object(s): one or more objects that are to be printed.

Sep: used to insert in between the objects.Default value for the separator is single space. end: it will specify what to print at the end.by default it has “/n” which means new line.

file: An object with the write method.Stream where the output is to be printed. Default value = Standard output

flush: it is a boolean value that specifies if the output is flushed or buffered.By default value is False.

flush then the boolean value is True.then the processing of output is slow Buffered then the boolean value is False.

Example

When you use loops to print a sequence of numbers then every number gets into a new line.If we want to print them side by side then you can use end in the print function.

Example

Comments in Python

These lines are ignored by the python interpreter.this is used for readability of the code and help the other programmers to understand the code. There are of 2 kind of comments in python

1. Single line comments

2. Multi line comments

Single line comments

This type of comments start with hash symbol(#).

Only used for single lines.

Example

In line 3 also we used print to get output but it is not printed in the output because that instruction is ignored by the python interpreter.As we used (#) before the instruction it will be considered as the comment.

Multi line Comments

When we want to write comments in more than a single line then we use multi line comments.

Example

 

Author    : Prudhvi Teja

LinkedIn : http://linkedin.com/in/prudhvi-teja-nagabhyru-715052224

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTExperts is always active on social media platforms.

Facebook  : https://www.facebook.com/ktexperts

LinkedIn    : https://www.linkedin.com/company/ktexperts/

Twitter       : https://twitter.com/ktexpertsadmin

YouTube   :  https://www.youtube.com/c/ktexperts

Instagram  : https://www.instagram.com/knowledgesharingplatform

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment