You are on page 1of 4

7/5/22, 12:46 AM Lesson 21 - Jupyter Notebook

In [ ]:  1 def factorial(n):


2 """Return n! for positive values of n.
3 >>> factorial(1)
4 1
5 >>> factorial(2)
6 2
7 >>> factorial(3)
8 6
9 >>> factorial(4)
10 24
11 """
12 fact = 1
13 for i in range(2,n+1):
14 fact = fact * n
15 return fact
16 factorial(3)
17 ​

In [23]:  1 # Exercise 3.12


2 n = 1221
3 ans = n % 10
4 ​
5 n = n // 10
6 ​
7 ans = (ans*10) + (n % 10)
8 n = n // 10
9 ans = (ans*10) + (n % 10)
10 print( ans)
11 print(ans == n)
12 if n == ans:
13 print("It's a palindrome")
14 else:
15 print("It's not a palindrome")

122

False

It's not a palindrome

Different types of arguments in python


positional arguments
default arguments
keyword arguments

Positional arguments

localhost:8888/notebooks/Python Teaching/Shuaib/Lesson 21.ipynb 1/4


7/5/22, 12:46 AM Lesson 21 - Jupyter Notebook

In [25]:  1 #Positional arguments


2 # name of function and creating a function
3 ​
4 def multiply(num1,num2):
5 print(num1*num2)
6
7 ​
8 print("Before Calling our Function multiply(20,5)")
9 ​
10 # Function Calling
11 multiply(20,5)
12 ​
13 print("After calling our function multiply(20,5)")

Before Calling our Function multiply(20,5)

100

After calling our function multiply(20,5)

Default arguments

In [27]:  1 # Default Arguments


2 def Numbers(a,b = 10,c = 14):
3 print(a,b,c)
4
5
6 ​
7 ​
8 ​
9 # Function Calling
10 Numbers(5)
11 ​

5 10 14

In [28]:  1 # Default Arguments


2 def Numbers(a,b = 10,c = 14):
3 print(a,b,c)
4
5
6 ​
7 ​
8 ​
9 # Function Calling
10 Numbers(5 , 15)

5 15 14

localhost:8888/notebooks/Python Teaching/Shuaib/Lesson 21.ipynb 2/4


7/5/22, 12:46 AM Lesson 21 - Jupyter Notebook

In [29]:  1 # Default Arguments


2 def Numbers(a,b = 10,c = 14):
3 print(a,b,c)
4
5
6 ​
7 ​
8 ​
9 # Function Calling
10 Numbers(5 , 15 , 25)

5 15 25

In [31]:  1 # While writing default arguments, default parameters should follow non d
2 ​
3 ​
4 def Numbers(c, a = 15 , b = 13):
5 print(a,b,c)
6
7
8 ​
9 ​
10 ​
11 # Function Calling
12 Numbers(5)

15 13 5

In [35]:  1 ​
2 def Numbers(a=14 , c=12, b = 20):
3 print(a,b,c)
4
5
6 ​
7 ​
8 ​
9 # Function Calling
10 Numbers()

7 78 45

Keyword arguments

localhost:8888/notebooks/Python Teaching/Shuaib/Lesson 21.ipynb 3/4


7/5/22, 12:46 AM Lesson 21 - Jupyter Notebook

In [41]:  1 #keyword arguments


2 # name of function and creating a function
3 ​
4 def multiply(num1,num2, num3):
5 print(num1,num2,num3)
6
7 ​
8 ​
9 ​
10 # Function Calling
11 multiply(20, num3 = 2, num2 = 5)
12 ​
13 ​

File "C:\Users\shahj\AppData\Local\Temp/ipykernel_2008/1146767251.py", li
ne 11

multiply(num3 = 2, num2 = 5, 20)

SyntaxError: positional argument follows keyword argument

localhost:8888/notebooks/Python Teaching/Shuaib/Lesson 21.ipynb 4/4

You might also like