Day-6 (Functions and Strings)
Day-6 (Functions and Strings)
In [ ]: #while loop:
while loop i used to iterate over a block of code as long as the test condition (expression) is True.
#Note : when we use while loops?
when we dont know the number of iterations for the looping of our code.
enter n value10
The Sum is 55
P
y
t
h
o
n
P
r
o
enter value10
current value : 10
current value : 9
current value : 8
current value : 7
current value : 6
End loop
In [ ]: #Functions in Python:
what are functions?
A function is a group of related statements that performs a specific task.
why we use functions?
(i) function can help to break our large or complex project into smaller parts.
(ii) Functions make the code as more organized and manageable.
(iii) Its avoids the repetition and makes the code reusable.
In [ ]: #Syntax of Function:
def function_name(parameters): #called parameters or formal parameters
"""doc string"""
statements
return expression
function_name(arguments)- #Calling parameters or actual parameters
enter a nameStudents
Hello Students Good Evening
In [ ]: #return statement:
The return statement is used to exit a function and go back to the place from where it was called.
#syntax:
return [expression_list]
5
9
In [ ]: #Type of functions:
we have 2 types of functions
1. Built-in Functions
2. User-defined functions
enter a value10
enter b value20
enter c value30
30 50 40
enter x value3
Out[41]: 283
In [47]: len("python")
len([1,2,4,4,5,6,7,8,888,"apssdc","abc"])
Out[47]: 11
In [51]: abs(-5)
abs(500)
abs(-40.55)
Out[51]: 40.55
In [54]: li = [1,2,3,4,5]
all(li)
Out[54]: True
In [60]: any([0,0,1])
Out[60]: True
In [64]: #bin()- method converts and returns the binary equilvalent string of given integer.
bin(2)
bin(10)
bin(7)
bin(100)
Out[64]: '0b1100100'
Out[67]: True
In [68]: bool(None)
Out[68]: False
In [79]: #chr : this function get the ascii chracter of given interger
print(chr(97))
print(chr(100))
print(chr(122))
print(chr(65))
print(chr(85))
print(chr(1200))
a
d
z
A
U
Ұ
In [85]: print(ord("a"))
print(ord("A"))
print(ord("z"))
print(ord("E"))
97
65
122
69
(0, 'karthik') (1, 'kalyan') (2, 'priya') (3, 'divya') (4, 'Raj')
In [93]: #eval() function: its parses the expression passed to this method and returns the value.
#syntax: eval(expression)
x = 100
eval("x+x**2+x*3+100")
Out[93]: 10500
In [1]: help(eval)
In [3]: #filter(function,iterable)
letters = ["p","y","t","h","o","n","i","d","l","e"]
def filtervowels(letter):
vowels = ["a","e","i","o","u"]
if letter in vowels:
return True
else:
return False
filtered= filter(filtervowels,letters)
for vowel in filtered:
print(vowel,end=" ")
o i e
In [ ]: #Type of arguments:
There are 3 types of arguments:
1. default arguments
2. keyword arguments
3. arbitary arguments
Parrot bird is like in green color and it can fly in the sky
In [12]: #arbitary arguments: this arguments are represented by astrick symbol "*".
def IPL_Players(*names):
return names
IPL_Players("Dhoni","Kohli","K L Rahul","Dhavan","Iyer")
In [13]: #5. Using Nested if conditions make a logic for Username and password validation and prints the display
#message for successfully logged users.
username = input("enter the username:")
password = input("enter the password: ")
if username == "[email protected]":
if password=="987654321":
print("Welcome to ",username)
else:
print("Invalid Password")
elif username=="[email protected]":
if password=="12345":
print("Welcome to",username)
else:
print("Invalid Password")
else:
print("invalid usename")
In [ ]: #6. Find the grade of students marks and percentage upto 5 members and prints the grade of highest percentag
e: