Basic Program Fundamentals in Python

Share via:

Basic Program Fundamentals in Python

How To Take Input from User in Python?
By using 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
▪️ Whatever we take input from the user will be in the string format by default.
▪️ You can change into your desired data type from the string data type.
Taking input from the user is as follows.

👉 By observing the above input and output even though you give the input as an integer value it is stored in the given value as string only.
👉 The value or string that you give in the user prompt will be stored in the variable, in the above case the string “90” will be stored in the s.
👉 If you want to take the variable of the desired data type you have to perform typecasting from the string data type to your desired data type.
👉 At the time of taking input from the user only you can type cast into 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 without displaying any instructions or message to the user.
When you want to provide any instructions to the user then use
syntax: input (“Enter Message to display”)
Example for displaying any instructions while taking input from the user.

 

Taking multiple Inputs in a single line
When you want to take multiple values from the user of the same data type you need to include some functions like map and split
Ex: If you want to take two values of same data type from user then use map function
Syntax: a,b=map (desired data type,input().split())
Example:

Taking Input for the Sequence Data Types like List, Set.
In this sequence data types we can take input in two ways
1.Using a for loop or while loop and add the elements using append in list or add in set.
2.Using map functions.
1. Using loops and append method.
Before using the append or add method you have to create one empty sequence data type or use the present sequence data type.

2. 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 the map and split function.

More about print () in python
Syntax:  print(object(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
About the syntax:
object(s): one or more values or expressions that are to be printed.
Sep: used to insert in between the values. 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

Printing of numbers with the help of loop
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
👉 Comments are used to add some explanatory notes.
👉 These lines are ignored by the python interpreter while executing the program.
👉 Comments are used to increase the readability of the code and help the other programmers to understand the code.
There are of three types of comments in python.
Single line comments
Inline comments
Multi line comments
👉 Single line comments
This type of comments starts with hash symbol (#).
Example:

In the above-mentioned input line 2 and line 3 we used print to get output but line 3 is not printed in the output because that instruction in the line 3 will not be executed and it will be ignored by the python interpreter. As we used (#) before the instruction it will be considered as the comment.
👉 Inline Comments
Comments that appear at the end of a line of code.
The instruction before that comment will be executed by the interpreter.
Example

👉 Multi line Comments
Whenever you want to write comments in more than a single line then we use multi line comments.
We use triple Quotes for the multi-line comments.
Example
“”” multi line comments in python
we use triple quotes for multi-line comments. “””

 

Author    : Venkat Vinod Kumar Siram 

LinkedIn : https://www.linkedin.com/in/vinodsiram/

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