Python Basics | Python Language

Python Basics

Python language is simple and versatile programming language.The structure and syntax of Python Code is simple as compared to many different languages like java, C++  etc . In the following examples you can easily understand the concept of python basics including python for loop, print statements,logical operators and predefined functions of the python libraries.

Note: Take care of the the indentation of the Code as seen in the given examples:

Python Basics

 

Example#1:

Write to initialize the given list and print its length/ count of elements.

l = [13, 99, 6, 76, 11, 83, 27, 84, 28, 67, 66, 22, 96, 46, 63, 21, 65, 48, 8, 14 , 84, 22, 28, 11, 83,
87, 11, 76, 6,83,27]

Code:

print len(l)

Output:

 

Example#2

 Write a python code to find the Maximum element of the list l.
l = [13, 99, 6, 76, 11, 83, 27, 84, 28, 67, 66, 22, 96, 46, 63, 21, 65, 48, 8, 14 , 84, 22, 28, 11, 83,
87, 11, 76, 6,83,27]

Code:
_max = float(“-inf”)
i = 0
while i<len(l):
if l[i]>_max:
_max = l[i]
i+=1
print _max

Output:

Example#3

Write a python code to find the Minimum element of the list l.

l = [13, 99, 6, 76, 11, 83, 27, 84, 28, 67, 66, 22, 96, 46, 63, 21, 65, 48, 8, 14 , 84, 22, 28, 11, 83,
87, 11, 76, 6,83,27]

Code:
_min = float(“+inf”)
i = 0
while i<len(l):
if l[i]<_min:
_min = l[i]
i+=1
print _min

 

Output:

Example#4

Write a python code to find the average of elements in the list l.

l = [13, 99, 6, 76, 11, 83, 27, 84, 28, 67, 66, 22, 96, 46, 63, 21, 65, 48, 8, 14 , 84, 22, 28, 11, 83,
87, 11, 76, 6,83,27]

Code:
_av = 0
i = 0
while i<len(l):
_av += l[i]
i+=1
print “Average:”,_av/len(l)

Output:

 

Example#5

Write a python code to search a given element (take as input) in the list.

l = [13, 99, 6, 76, 11, 83, 27, 84, 28, 67, 66, 22, 96, 46, 63, 21, 65, 48, 8, 14 , 84, 22, 28, 11, 83,
87, 11, 76, 6,83,27]

Code:

_n = input(“Enter number to search: “)
found = 0
i = 0
pos = “”
while i<len(l):
if _n==l[i]:
found+=1
pos +=str(i)+” ”
i+=1

if found > 0 :
print “Found”,found,”instance(s) at position(s) :”,pos
else:
print”Not found”

 

Output:

 

Comment below if you have any query or confusion.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *