0% found this document useful (0 votes)
4 views7 pages

Class 3

Uploaded by

Somenath Kuiry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Class 3

Uploaded by

Somenath Kuiry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

In [8]: marks=40

course="DS"
print("I have got %d%% marks in the subject %s"%(marks,course))
print("I have got",marks,"% marks in the subject",course )
print("I have got "+str(marks),"% marks in the subject "+course )

I have got 40% marks in the subject DS


I have got 40 % marks in the subject DS
I have got 40 % marks in the subject DS

In [9]: """

Ternary Operator in Python


Syntax:
[on_true] if [expression] else [on_false]

"""

a, b = 10, 20
min = a if a < b else b

print(min)

10

In [10]: '''
Given an integer, N perform the following conditional actions:

If N is odd, print Weird


If N is even and in the inclusive range of 2 to 5, print Not Weird
If N is even and in the inclusive range of 6 to 20, print Weird
If N is even and greater than 20, print Not Weird

'''

n=int(input("N="))

if (n%2==0):
if(n>=2 and n<=5):
print("Not Weird")

elif(n>=6 and n<=20):


print("Weird")
else:
print("Not Weird")

else:
print ("Weird")

N=1
Weird

In [ ]: #Factorial of N #

N=9

mul=1
for i in range(1,N+1):
mul=mul*i

print("fact of %d id = %d"%(N,mul))

In [ ]: '''Patter: 1
12
123
1234 '''

num = 5
for n in range(1, num):
for i in range(1, n+1):
print(i, end=" ")
print("")

In [ ]: # Reverse of number #

num = 1234
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("Reversed Number: " + str(reversed_num))


In [ ]: ## prime NO ##

# Program to check if a number is prime or not

num = 29

# To take input from the user


#num = int(input("Enter a number: "))

# define a flag variable


flag = False

# prime numbers are greater than 1


if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True


if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

In [ ]: ## Define function with passing argument x


def my_function1(x):
x=x+5
return x

def my_function2(x):
x=x+5

## Calling function by argument passing

y=my_function1(5)
print(y)
z=my_function2(3)
print(z)
#u= my_function1()
#print(u)

In [ ]: #Multiple Argument Passing into function as Parameter#

def my_function(a, b):


#print(a + " &" + b)
return a,b

a,b=my_function("AI", "Data Science")


print(a,b)

In [ ]: ## Variable Number of argument passing ##


def my_function(*arg):
print("The youngest student is "+arg[2])

my_function("St1","St2","st3","s4")
#my_function(1,2,3,4,5)

The youngest student is st3

In [19]: ## Default arguments passing##

def myFun(y=50):

print("y: ", y)

myFun()
myFun(100)

y: 50
y: 100

In [20]: ## Keyword arguments ##

def student(course1, course2):


print(course1, course2)
student(course1='Python', course2='AI')
student(course2='AI', course1='Mathematics')

Python AI
Mathematics AI

In [11]: #Arbitrary Keyword Argument ##

# **kwargs for variable number of keyword arguments

def my_function(**kwargs):
print("The last module of the course is = "+kwargs["course2"])

my_function(course1 ="AI", course2= "Data Science", course3="Python", course4="ML Project")

#my_function(course1 =1, course2= 2)

The last module of the course is = Data Science

In [4]: ## Anonymous functions: ##

string ="Data Science"


# lambda returns a function object
print(lambda string : string)

x=lambda string : string


print(x(string))

## function ##
def cube(x):
return x*x*x

## Anonymous function ##
cube_v2 = lambda x : x*x*x

print(cube(7))
print(cube_v2(7))

<function <lambda> at 0x7fcfcfbf15f0>


Data Science
343
343
In [5]: ## filter() function ##
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
print(new_list)

[4, 6, 8, 12]

In [7]: ## map() function ##

my_list = [1, 5, 4, 6, 8, 11, 3, 12]


new_list = list(map(lambda x: x * 2 , my_list))
print(new_list)

[2, 10, 8, 12, 16, 22, 6, 24]

In [ ]: ## Recursive function ##
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))

num = 5
print("The factorial of", num, "is", factorial(num))

In [ ]: ## Fibonacci Series using Recursion ##

def fib(n):
if n<=1:
return n
else:
return fib(n-1) + fib(n-2)

count=10
for i in range(count):
print(fib(i),end=" ")

0 1 1 2 3 5 8 13 21 34

In [ ]: ## Finding the power of a number ##

def power(base,exp):
if exp==1:
return base
else:
return base*(power(base,exp-1))

base=10
exp=4
print("The result is:", power(base,exp))

The result is: 10000

In [ ]: ## Tower of Hanoi ##

def toh(n,start,end,aux):
if(n==1):
print("Move disk 1 from",start,"to",end)
return
toh(n-1,start,aux,end)
print("Move disk",n,"from",start,"to",end)
toh(n-1,aux,end,start)

n = int(input("Enter the number of disks: "))


toh(n,'A','B','C')

You might also like