├── .gitignore ├── 100 Python Problems ├── 54 - reverse list.py ├── 47 - char index in string.py ├── 02 - c to f temp.py ├── 28 - digit combos.py ├── 36 - find factors.py ├── 51 - remove dupes list.py ├── 06 - even or odd.py ├── 53 - max value list without max().py ├── 07 - leap year.py ├── 75 - merge dicts.py ├── 45 - username from email.py ├── 44 - string len without len().py ├── 48 - count vowels.py ├── 24 - first 25 odd nums.py ├── 61 - combine lists without '+'.py ├── 63 - flatten 2d list.py ├── 35 - count digits.py ├── 56 - square list items.py ├── 49 - remove char.py ├── 66 - matrix shape.py ├── 55 - search num list.py ├── 21 - sum first n nums.py ├── 52 - title case without title().py ├── 13 - div by 3 & 6.py ├── 20 - swap two nums.py ├── 31 - first 25 primes.py ├── 32 - first 20 fibs.py ├── 58 - word count string.py ├── 69 - factorial without loop.py ├── 03 - swap vars w third.py ├── 26 - armstrong 100-1000.py ├── 65 - max item matrix row.py ├── 29 - find hcf.py ├── 15 - sum of squares.py ├── 73 - list items to dict.py ├── 04 - sum of digits.py ├── 59 - list sorted without sort().py ├── 38 - pyramid nested.py ├── 01 - largest of 3 nums.py ├── 50 - palindrome string.py ├── 33 - compound interest.py ├── 34 - sum n+nn+nnn.py ├── 57 - reverse words.py ├── 22 - multiply without op.py ├── 68 - sort list without sort().py ├── 23 - factorial.py ├── 12 - vol of cylinder.py ├── 08 - euclidean dist.py ├── 10 - profit loss calc.py ├── 46 - char occurrences.py ├── 16 - armstrong num.py ├── 40 - sum series.py ├── 62 - replace item.py ├── 09 - 3 angles triangle.py ├── 11 - simple interest.py ├── 30 - calc lcm.py ├── 25 - prime number.py ├── 60 - sep even-odd list.py ├── 05 - reverse digits.py ├── 17 - narcissistic num.py ├── 72 - dict upper-lower chars.py ├── 37 - reverse num.py ├── 64 - union & intersection lists.py ├── 42 - sum & avg.py ├── 78 - int to string without str().py ├── 27 - population calc.py ├── 43 - simplify fraction.py ├── 76 - swap max min dict.py ├── 70 - bag of words.py ├── 67 - matrix mult possib.py ├── 14 - weather type.py ├── 19 - menu driven.py ├── 71 - shortest dist coords.py ├── 41 - series sum.py ├── 39 - patterns nested.py ├── 77 - login and reg.py ├── 18 - inhand salary.py └── 74 - most used word song.py ├── README.md └── 100 Days Python ├── Day 03 - comments.ipynb ├── Day 10 - indentation.ipynb ├── Day 12 - guessing-game.ipynb ├── Day 14 - nested-loops.ipynb ├── Day 15 - break-continue-pass.ipynb ├── Day 05 - keywords-identifiers.ipynb ├── Day 09 - decision-control.ipynb ├── Day 01 - print-function.ipynb ├── Day 07 - literals.ipynb ├── Day 11 - while-loop.ipynb ├── Day 04 - variables.ipynb ├── Day 02 - data-types.ipynb ├── Day 28 - threading - multi-processing.ipynb ├── Day 25 - recursion - memoization.ipynb ├── Day 13 - for-loop.ipynb ├── Day 06 - user-input-type-conv.ipynb ├── Day 16 - built-in-funcs.ipynb ├── Day 26 - lambda - map - filter - reduce.ipynb ├── Day 08 - operators.ipynb └── Day 17 - built-in-modules.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints/ -------------------------------------------------------------------------------- /100 Python Problems/54 - reverse list.py: -------------------------------------------------------------------------------- 1 | # Write a program to reverse a list. 2 | 3 | L = [1, 2, 3, 4, 5] 4 | rev = [] 5 | 6 | for i in range(len(L) - 1, -1, -1): 7 | rev.append(L[i]) 8 | print(rev) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 100-days-of-python-programming 2 | 3 |

4 | 5 |

-------------------------------------------------------------------------------- /100 Python Problems/47 - char index in string.py: -------------------------------------------------------------------------------- 1 | # Find the index position of a particular character in another string. 2 | 3 | a = input('Enter your string: ') 4 | b = input('Enter your character: ') 5 | print(a.index(b)) -------------------------------------------------------------------------------- /100 Python Problems/02 - c to f temp.py: -------------------------------------------------------------------------------- 1 | # Write a program that converts Celsius values to Fahrenheit. 2 | 3 | temp = float(input("Enter the temperature in Celsius: ")) 4 | fahrenheit = (temp * 1.8) + 32 5 | print(fahrenheit) -------------------------------------------------------------------------------- /100 Python Problems/28 - digit combos.py: -------------------------------------------------------------------------------- 1 | # Write a program to print all the unique combinations of 1, 2, 3, and 4. 2 | 3 | for i in range(1, 5): 4 | for j in range(1, 5): 5 | if i != j: 6 | print(i, j) -------------------------------------------------------------------------------- /100 Python Problems/36 - find factors.py: -------------------------------------------------------------------------------- 1 | # Print all factors of a given number provided by the user. 2 | 3 | num = int(input('Enter your number: ')) 4 | 5 | for i in range(1, num + 1): 6 | if num % i == 0: 7 | print('Your factors are:', i) -------------------------------------------------------------------------------- /100 Python Problems/51 - remove dupes list.py: -------------------------------------------------------------------------------- 1 | # Write a program to remove all duplicates from a list. 2 | 3 | L1 = [1, 2, 2, 3, 4, 4, 5, 6, 7, 7] 4 | L2 = [] 5 | 6 | for i in L1: 7 | if i not in L2: 8 | L2.append(i) 9 | 10 | print(L2) -------------------------------------------------------------------------------- /100 Python Problems/06 - even or odd.py: -------------------------------------------------------------------------------- 1 | # Write a program that will determine whether the number entered by the user is even or odd. 2 | 3 | num = int(input("Enter your number: ")) 4 | 5 | if num % 2 == 0: 6 | print("Even") 7 | else: 8 | print("Odd") -------------------------------------------------------------------------------- /100 Python Problems/53 - max value list without max().py: -------------------------------------------------------------------------------- 1 | # Write a program to find the maximum item in a list without using the max function. 2 | 3 | L = [2, 3, 5, 6, 8, 9] 4 | max = L[0] 5 | 6 | for i in L: 7 | if i > max: 8 | max = i 9 | print(max) -------------------------------------------------------------------------------- /100 Python Problems/07 - leap year.py: -------------------------------------------------------------------------------- 1 | # Write a program that will determine whether the given year is a leap year or not. 2 | 3 | year = int(input("Enter your year: ")) 4 | 5 | if year % 4 == 0: 6 | print("Leap year") 7 | else: 8 | print("Not a leap year") -------------------------------------------------------------------------------- /100 Python Problems/75 - merge dicts.py: -------------------------------------------------------------------------------- 1 | # Write a program to merge two given dictionaries. 2 | 3 | D1 = {'a': 2, 'b': 3} 4 | D2 = {'c': 4, 'd': 5} 5 | 6 | D = {} 7 | 8 | for i in D1: 9 | D[i] = D1[i] 10 | for k in D2: 11 | D[k] = D2[k] 12 | 13 | print(D) -------------------------------------------------------------------------------- /100 Python Problems/45 - username from email.py: -------------------------------------------------------------------------------- 1 | # Extract the username from a given email. 2 | # For example, if the email is saurabh@gmail.com, then the username should be saurabh. 3 | 4 | email = input('Enter your email: ') 5 | username, domain = email.split('@') 6 | print(username) -------------------------------------------------------------------------------- /100 Python Problems/44 - string len without len().py: -------------------------------------------------------------------------------- 1 | # Find the length of a given string without using the len() function. 2 | 3 | a = input('Enter your string: ') 4 | 5 | count = 0 6 | 7 | for i in a: 8 | count = count + 1 9 | 10 | print('Length of the string is:', count) -------------------------------------------------------------------------------- /100 Python Problems/48 - count vowels.py: -------------------------------------------------------------------------------- 1 | # Count the number of vowels in a string provided by the user. 2 | 3 | a = input('Enter your string: ') 4 | 5 | vowels = 'aeiou' 6 | count = 0 7 | 8 | for i in a: 9 | if i in vowels: 10 | count = count + 1 11 | 12 | print(count) -------------------------------------------------------------------------------- /100 Python Problems/24 - first 25 odd nums.py: -------------------------------------------------------------------------------- 1 | # Write a program to print the first 25 odd numbers. 2 | 3 | flag = 0 4 | 5 | i = 1 6 | 7 | while True: 8 | if i % 2 != 0: 9 | print(i) 10 | flag = flag + 1 11 | if flag == 25: 12 | break 13 | i = i + 1 -------------------------------------------------------------------------------- /100 Python Problems/61 - combine lists without '+'.py: -------------------------------------------------------------------------------- 1 | # Write a program to merge two lists without using the + operator. 2 | 3 | L1 = [1, 2, 3, 4] 4 | L2 = [5, 6, 7, 8] 5 | 6 | L = [] 7 | 8 | for i in L1: 9 | L.append(i) 10 | for j in L2: 11 | L.append(j) 12 | 13 | print(L) -------------------------------------------------------------------------------- /100 Python Problems/63 - flatten 2d list.py: -------------------------------------------------------------------------------- 1 | # Write a program that can convert a 2D list to a 1D list. 2 | 3 | L = [1, 2, 3, 4, [5, 7, 8]] 4 | 5 | rev = [] 6 | 7 | for i in L: 8 | if type(i) == list: 9 | rev.extend(i) 10 | else: 11 | rev.append(i) 12 | print(rev) -------------------------------------------------------------------------------- /100 Python Problems/35 - count digits.py: -------------------------------------------------------------------------------- 1 | # Take a number from the user and determine the number of digits in it. 2 | 3 | a = int(input('Enter your number: ')) 4 | 5 | count = 0 6 | 7 | while(a > 0): 8 | a = a // 10 9 | count = count + 1 10 | 11 | print('Number of digits:', count) -------------------------------------------------------------------------------- /100 Python Problems/56 - square list items.py: -------------------------------------------------------------------------------- 1 | # Write a program that can create a new list from a given list, where each item in the new list is the square of the corresponding item in the old list. 2 | 3 | L1 = [2, 3, 4, 5, 6] 4 | L2 = [] 5 | 6 | for i in L1: 7 | L2.append(i**2) 8 | print(L2) -------------------------------------------------------------------------------- /100 Python Problems/49 - remove char.py: -------------------------------------------------------------------------------- 1 | # Write a program that can remove a particular character from a string. 2 | 3 | a = input('Enter your string: ') 4 | b = int(input('Enter the value of the character: ')) 5 | 6 | c = a[0:b - 1] 7 | d = a[b:] 8 | 9 | result = c + d 10 | 11 | print(result) -------------------------------------------------------------------------------- /100 Python Problems/66 - matrix shape.py: -------------------------------------------------------------------------------- 1 | # Write a program to print the shape of a matrix. 2 | 3 | matrix = [ 4 | [1, 2, 3], 5 | [4, 5, 6], 6 | [7, 8, 9] 7 | ] 8 | 9 | row = 0 10 | 11 | for i in matrix: 12 | row = row + 1 13 | print('The shape of this matrix is:', row, '*', len(i)) -------------------------------------------------------------------------------- /100 Python Problems/55 - search num list.py: -------------------------------------------------------------------------------- 1 | # Write a program to search for a given number in a list. 2 | 3 | L = [1, 2, 3, 4, 5, 6, 7, 8, 9] 4 | num = int(input('Enter your number: ')) 5 | 6 | for i in L: 7 | if i == num: 8 | print('True') 9 | break 10 | else: 11 | print('False') -------------------------------------------------------------------------------- /100 Python Problems/21 - sum first n nums.py: -------------------------------------------------------------------------------- 1 | # Write a program to find the sum of the first n numbers, where n will be provided by the user. 2 | # For example, if the user provides n = 10, the output should be 55. 3 | 4 | n = int(input("Enter your number: ")) 5 | 6 | s = n * (n + 1) / 2 7 | 8 | print("Your sum is: ", s) -------------------------------------------------------------------------------- /100 Python Problems/52 - title case without title().py: -------------------------------------------------------------------------------- 1 | # Write a program to convert a string to title case without using the title() function. 2 | 3 | a = 'hEllO sAuRABh SinGH dHaMI' 4 | y = a.split() 5 | print(y) 6 | 7 | r = '' 8 | for i in y: 9 | r = r + i[0].upper() + i[1:].lower() + ' ' 10 | print(r) -------------------------------------------------------------------------------- /100 Python Problems/13 - div by 3 & 6.py: -------------------------------------------------------------------------------- 1 | # Write a program that will determine whether the given number is divisible by 3 and 6. 2 | 3 | num = int(input("Enter your number: ")) 4 | 5 | if num % 3 == 0 and num % 6 == 0: 6 | print(num, "is divisible by 3 and 6") 7 | else: 8 | print(num, "is not divisible by 3 and 6") -------------------------------------------------------------------------------- /100 Python Problems/20 - swap two nums.py: -------------------------------------------------------------------------------- 1 | # Write a program that swaps numbers. 2 | 3 | a = int(input("Enter your first number: ")) 4 | b = int(input("Enter your second number: ")) 5 | 6 | a = a + b 7 | b = a - b 8 | a = a - b 9 | 10 | print("After swapping:") 11 | print("Your 'a' is: ", a) 12 | print("Your 'b' is: ", b) -------------------------------------------------------------------------------- /100 Python Problems/31 - first 25 primes.py: -------------------------------------------------------------------------------- 1 | # Print the first 25 prime numbers. 2 | 3 | counter = 0 4 | num = 2 5 | 6 | while counter <= 25: 7 | for i in range(2, num): 8 | if num % i == 0: 9 | break 10 | else: 11 | print(num) 12 | counter = counter + 1 13 | num = num + 1 -------------------------------------------------------------------------------- /100 Python Problems/32 - first 20 fibs.py: -------------------------------------------------------------------------------- 1 | # Print the first 20 numbers of a Fibonacci series. 2 | 3 | count = 0 4 | a = 0 5 | b = 1 6 | 7 | print(a) 8 | print(b) 9 | 10 | while True: 11 | c = a + b 12 | a = b 13 | b = c 14 | print(c) 15 | count = count + 1 16 | if count == 18: 17 | break -------------------------------------------------------------------------------- /100 Python Problems/58 - word count string.py: -------------------------------------------------------------------------------- 1 | # Write a program that can count the number of words in a given string. 2 | 3 | a = input('Enter your string: ') 4 | count = 0 5 | 6 | for i in a: 7 | if i == ' ': 8 | count = count + 1 9 | 10 | words = count + 1 11 | print('Number of words in the string:', words) -------------------------------------------------------------------------------- /100 Python Problems/69 - factorial without loop.py: -------------------------------------------------------------------------------- 1 | # Write a function that accepts a number and returns its factorial. 2 | # You cannot use any loops. 3 | 4 | def fact(number): 5 | if number == 1: 6 | return 1 7 | else: 8 | return number * fact(number-1) 9 | 10 | result = fact(3) 11 | print(result) -------------------------------------------------------------------------------- /100 Python Problems/03 - swap vars w third.py: -------------------------------------------------------------------------------- 1 | # The user will input two numbers. 2 | # Write a program to swap the numbers. 3 | 4 | a = int(input("Enter the value of a : ")) 5 | b = int(input("Enter the value of b: ")) 6 | 7 | temp = a 8 | a = b 9 | b = temp 10 | 11 | print("value of a: ", a) 12 | print("value of b: ", b) -------------------------------------------------------------------------------- /100 Python Problems/26 - armstrong 100-1000.py: -------------------------------------------------------------------------------- 1 | # Print all the Armstrong numbers in the range of 100 to 1000. 2 | 3 | for num in range(100, 1000): 4 | i = num 5 | a = num % 10 6 | num = num // 10 7 | b = num % 10 8 | c = num // 10 9 | if (a**3) + (b**3) + (c**3) == i: 10 | print(i) 11 | i = i + 1 -------------------------------------------------------------------------------- /100 Python Problems/65 - max item matrix row.py: -------------------------------------------------------------------------------- 1 | # The maximum item in each row of a matrix. 2 | 3 | matrix = [ 4 | [1, 2, 3], 5 | [4, 5, 6], 6 | [7, 8, 9] 7 | ] 8 | 9 | for i in matrix: 10 | max_value = i[0] 11 | for j in i: 12 | if j > max_value: 13 | max_value = j 14 | print(max_value) -------------------------------------------------------------------------------- /100 Python Problems/29 - find hcf.py: -------------------------------------------------------------------------------- 1 | # The user will provide two numbers, and you have to find the HCF of those two numbers. 2 | 3 | a = int(input('Enter your first number: ')) 4 | b = int(input('Enter your second number: ')) 5 | 6 | while a % b != 0: 7 | rem = a % b 8 | a = b 9 | b = rem 10 | 11 | print('Your HCF is:', b) -------------------------------------------------------------------------------- /100 Python Problems/15 - sum of squares.py: -------------------------------------------------------------------------------- 1 | # Write a program that takes three digits from the user and adds the square of each digit. 2 | 3 | num = int(input("Enter your number: ")) 4 | 5 | a = num % 10 6 | num = num // 10 7 | b = num % 10 8 | c = num // 10 9 | 10 | add = (a**2) + (b**2) + (c**2) 11 | 12 | print("Required number is: ", add) -------------------------------------------------------------------------------- /100 Python Problems/73 - list items to dict.py: -------------------------------------------------------------------------------- 1 | # Assume a list with numbers from 1 to 10 and then convert it into a dictionary where the keys would be the numbers of the list, and the values would be the squares of those numbers. 2 | 3 | L = [1, 2, 3, 4, 5, 6, 7, 8, 9] 4 | 5 | D = {} 6 | 7 | for i in L: 8 | D[i] = i**2 9 | 10 | print(D) -------------------------------------------------------------------------------- /100 Python Problems/04 - sum of digits.py: -------------------------------------------------------------------------------- 1 | # Write a program that will give you the sum of three digits. 2 | 3 | num = int(input("Enter the three-digit number: ")) 4 | 5 | a = num % 10 # (123 % 10 = 3) 6 | num = num // 10 # (123 // 10 = 12) 7 | b = num % 10 # (12 % 10 = 2) 8 | c = num // 10 # (12 // 10 = 1) 9 | rev = (a + b + c) 10 | 11 | print(rev) -------------------------------------------------------------------------------- /100 Python Problems/59 - list sorted without sort().py: -------------------------------------------------------------------------------- 1 | # Write a program to check whether a list is in ascending order or not. 2 | 3 | L = [2, 3, 1, 6, 0] 4 | flag = 0 5 | 6 | for i in range(0, len(L)-1): 7 | if L[i] >= L[i+1]: 8 | flag = 1 9 | break 10 | 11 | if flag == 0: 12 | print('Sorted') 13 | else: 14 | print('Not sorted') -------------------------------------------------------------------------------- /100 Python Problems/38 - pyramid nested.py: -------------------------------------------------------------------------------- 1 | # Write a program to print the following pattern. 2 | 3 | # * 4 | # * * 5 | # * * * 6 | 7 | row = int(input('Enter number of rows: ')) 8 | 9 | for i in range(0, row): 10 | for j in range(0, row - i - 1): 11 | print(end=' ') 12 | for k in range(0, i + 1): 13 | print('*', end=' ') 14 | print() -------------------------------------------------------------------------------- /100 Python Problems/01 - largest of 3 nums.py: -------------------------------------------------------------------------------- 1 | # User will input (3 ages). 2 | # Find the oldest one. 3 | 4 | a = int(input("Enter the first age: ")) 5 | b = int(input("Enter the second age: ")) 6 | c = int(input("Enter the third age: ")) 7 | 8 | max_age = a 9 | 10 | if max_age < b: 11 | max_age = b 12 | if max_age < c: 13 | max_age = c 14 | 15 | print(max_age) -------------------------------------------------------------------------------- /100 Python Problems/50 - palindrome string.py: -------------------------------------------------------------------------------- 1 | # Write a program that can check whether a given string is a palindrome or not. 2 | 3 | a = input('Enter your string: ') 4 | 5 | rev = '' 6 | 7 | for i in range(len(a) - 1, -1, -1): 8 | rev = rev + a[i] 9 | 10 | print(rev) 11 | 12 | if rev == a: 13 | print('Palindrome') 14 | else: 15 | print('Not a palindrome') -------------------------------------------------------------------------------- /100 Python Problems/33 - compound interest.py: -------------------------------------------------------------------------------- 1 | # Write a program to calculate compound interest. 2 | 3 | p = int(input('Enter your principal: ')) 4 | r = int(input('Enter rate of interest: ')) 5 | t = int(input('Enter the time period elapsed: ')) 6 | 7 | a = p * (1 + r / 100)**t 8 | print('Your amount is:', a) 9 | 10 | ci = a - p 11 | print('Your compound interest is:', ci) -------------------------------------------------------------------------------- /100 Python Problems/34 - sum n+nn+nnn.py: -------------------------------------------------------------------------------- 1 | # Write a program that accepts an integer (n) and computes the value of n + nn + nnn. 2 | 3 | # n + nn + nnn ---> (2 + 22 + 222) 4 | 5 | n = input('Enter your number: ') 6 | print('n is:', n) 7 | 8 | nn = n + n 9 | print('nn is:', nn) 10 | 11 | nnn = n + n + n 12 | print('nnn is:', nnn) 13 | 14 | c = int(n) + int(nn) + int(nnn) 15 | print(c) -------------------------------------------------------------------------------- /100 Python Problems/57 - reverse words.py: -------------------------------------------------------------------------------- 1 | # Write a program that can reverse the words of a given string. 2 | # For example, if the input is 'Hello how are you', 3 | # the output should be 'you are how Hello'. 4 | 5 | a = input('Enter your string: ') 6 | x = a.split() 7 | rev = [] 8 | 9 | for i in range(len(x)-1, -1, -1): 10 | rev.append(x[i]) 11 | y = ' '.join(rev) 12 | print(y) -------------------------------------------------------------------------------- /100 Python Problems/22 - multiply without op.py: -------------------------------------------------------------------------------- 1 | # Write a program that can multiply two numbers provided by the user without using the * operator. 2 | 3 | first_num = int(input("Enter your first number: ")) 4 | second_num = int(input("Enter your second number: ")) 5 | 6 | sum = 0 7 | 8 | for i in range(0, second_num): 9 | sum = sum + first_num 10 | 11 | print("Your result is: ", sum) -------------------------------------------------------------------------------- /100 Python Problems/68 - sort list without sort().py: -------------------------------------------------------------------------------- 1 | # Write a program that can sort a given unsorted list. 2 | # Don't use any built-in function for sorting. 3 | 4 | L = [5, 2, 3, 6, 4, 7] 5 | 6 | for i in range(len(L)): 7 | for k in range(0, len(L)-1): 8 | if L[k] > L[k+1]: 9 | temp = L[k] 10 | L[k] = L[k+1] 11 | L[k+1] = temp 12 | print(L) -------------------------------------------------------------------------------- /100 Python Problems/23 - factorial.py: -------------------------------------------------------------------------------- 1 | # Write a program that can calculate the factorial of a given number provided by the user. 2 | 3 | num = int(input("Enter your number: ")) 4 | 5 | i = 1 6 | 7 | if num > 0: 8 | while num >= 1: 9 | i = i * num 10 | num = num - 1 11 | print("Factorial of the given number is: ", i) 12 | else: 13 | print("Factorial not possible") -------------------------------------------------------------------------------- /100 Python Problems/12 - vol of cylinder.py: -------------------------------------------------------------------------------- 1 | # Write a program to find the volume of the cylinder. 2 | # Also, determine the cost when the price of 1 liter of milk is 40 Rs. 3 | 4 | r = float(input("Enter your radius: ")) 5 | h = float(input("Enter your height: ")) 6 | 7 | v = 3.14 * (r ** 2) * h 8 | 9 | print("Your Volume is:", v) 10 | 11 | cost = v / 1000 * 40 12 | 13 | print("Your cost is: Rs", cost) -------------------------------------------------------------------------------- /100 Python Problems/08 - euclidean dist.py: -------------------------------------------------------------------------------- 1 | # Write a program to calculate the Euclidean distance between two coordinates. 2 | 3 | x_1 = float(input("Enter x1 of x coordinate: ")) 4 | y_1 = float(input("Enter y1 of y coordinate: ")) 5 | 6 | x_2 = float(input("Enter x2 of x coordinate: ")) 7 | y_2 = float(input("Enter y2 of y coordinate: ")) 8 | 9 | d = ((x_2 - x_1)**2 + (y_2 - y_1)**2)**0.5 10 | 11 | print(d) -------------------------------------------------------------------------------- /100 Python Problems/10 - profit loss calc.py: -------------------------------------------------------------------------------- 1 | # Write a program that takes user input for cost price and selling price, and determines whether it's a loss or a profit. 2 | 3 | cp = float(input("Enter your cost price: ")) 4 | sp = float(input("Enter your selling price: ")) 5 | 6 | if cp > sp: 7 | amount = cp - sp 8 | print("Loss:", amount) 9 | else: 10 | amount = sp - cp 11 | print("Profit:", amount) -------------------------------------------------------------------------------- /100 Python Problems/46 - char occurrences.py: -------------------------------------------------------------------------------- 1 | # Count the frequency of a particular character in a provided string. 2 | # For example, in the string 'hello, how are you,' the frequency of 'h' is 2. 3 | 4 | a = input('Enter your string: ') 5 | b = input('Enter the character: ') 6 | 7 | count = 0 8 | 9 | for i in a: 10 | if i in b: 11 | count = count + 1 12 | 13 | print('Character occurred:', count, 'times') -------------------------------------------------------------------------------- /100 Python Problems/16 - armstrong num.py: -------------------------------------------------------------------------------- 1 | # Write a program that checks whether a given number is an Armstrong number or not. 2 | 3 | user_input = int(input("Enter your number: ")) 4 | 5 | num = user_input 6 | 7 | a = num % 10 8 | num = num // 10 9 | b = num % 10 10 | c = num // 10 11 | 12 | if (a**3) + (b**3) + (c**3) == user_input: 13 | print("Armstrong number") 14 | else: 15 | print("Not an Armstrong number") -------------------------------------------------------------------------------- /100 Python Problems/40 - sum series.py: -------------------------------------------------------------------------------- 1 | # Write a program to calculate the sum of the following series up to the nth term: 2 | 3 | # 1/1! + 2/2! + 3/3! + 4/4! + ... + n/n! 4 | 5 | # The value of n will be provided by the user. 6 | 7 | num = int(input('Enter your number: ')) 8 | 9 | result = 0 10 | fact = 1 11 | 12 | for i in range(1, num + 1): 13 | fact = fact * i 14 | result = result + (i / fact) 15 | 16 | print(result) -------------------------------------------------------------------------------- /100 Python Problems/62 - replace item.py: -------------------------------------------------------------------------------- 1 | # Write a program to replace an item with a different item if it is found in the list. 2 | 3 | L = [1, 2, 3, 4] 4 | 5 | find = int(input('Enter the number you want to replace: ')) 6 | replace = int(input('Enter your number: ')) 7 | 8 | for i in range(0, len(L)): 9 | if find == L[i]: 10 | L[i] = replace 11 | print(L) 12 | break 13 | else: 14 | print('Not found') -------------------------------------------------------------------------------- /100 Python Problems/09 - 3 angles triangle.py: -------------------------------------------------------------------------------- 1 | # Write a program that takes user input for three angles and determines whether they can form a triangle or not. 2 | 3 | a = int(input("Enter your first angle: ")) 4 | b = int(input("Enter your second angle: ")) 5 | c = int(input("Enter your third angle: ")) 6 | 7 | if a + b + c == 180 and a != 0 and b != 0 and c != 0: 8 | print("Possible") 9 | else: 10 | print("Not Possible") -------------------------------------------------------------------------------- /100 Python Problems/11 - simple interest.py: -------------------------------------------------------------------------------- 1 | # Write a program to find the simple interest when the values of principal, rate of interest, and time period are given. 2 | 3 | p = int(input("Enter principal: ")) 4 | r = int(input("Enter rate of interest: ")) 5 | t = int(input("Enter time period in years: ")) 6 | 7 | si = (p * r * t) / 100 8 | 9 | print("Your Simple Interest is: ", si) 10 | 11 | a = p + si 12 | print("Your Amount is: ", a) -------------------------------------------------------------------------------- /100 Python Problems/30 - calc lcm.py: -------------------------------------------------------------------------------- 1 | # The user will provide two numbers, and you have to find the LCM of those two numbers. 2 | 3 | a = int(input('Enter your first number: ')) 4 | num_1 = a 5 | b = int(input('Enter your second number: ')) 6 | num_2 = b 7 | 8 | while num_1 % num_2 != 0: 9 | rem = num_1 % num_2 10 | num_1 = num_2 11 | num_2 = rem 12 | 13 | hcf = num_2 14 | lcm = (a * b) / hcf 15 | 16 | print('Your LCM is:', lcm) -------------------------------------------------------------------------------- /100 Python Problems/25 - prime number.py: -------------------------------------------------------------------------------- 1 | # Write a program to print whether a given number is a prime number or not. 2 | 3 | num = int(input("Enter your number: ")) 4 | 5 | if num == 2: 6 | print("Prime number") 7 | elif num > 1: 8 | for i in range(2, num): 9 | if (num % i) == 0: 10 | print(num, "is not a prime number") 11 | break 12 | else: 13 | print(num, "is a prime number") 14 | else: 15 | print(num, "is not a prime number") -------------------------------------------------------------------------------- /100 Python Problems/60 - sep even-odd list.py: -------------------------------------------------------------------------------- 1 | # Create two lists from a given list where the first list will contain all the odd numbers from the original list, and the second one will contain all the even numbers. 2 | 3 | L = [1, 2, 3, 4, 5, 6, 7, 8, 9] 4 | 5 | L_even = [] 6 | L_odd = [] 7 | 8 | for i in L: 9 | if i % 2 == 0: 10 | L_even.append(i) 11 | else: 12 | L_odd.append(i) 13 | 14 | print('List of even values:', L_even) 15 | print('List of odd values:', L_odd) -------------------------------------------------------------------------------- /100 Python Problems/05 - reverse digits.py: -------------------------------------------------------------------------------- 1 | # Write a program that reverses a four-digit number. 2 | # Additionally, ensure that it checks whether the reversal is true. 3 | 4 | user_input = int(input("Enter the four digit number: ")) 5 | 6 | num = user_input 7 | 8 | a = num % 5 9 | num = num // 10 10 | 11 | b = num % 5 12 | num = num // 10 13 | 14 | c = num % 5 15 | 16 | d = num // 10 17 | 18 | reverse = 1000*a + 100*b + 10*c + d 19 | 20 | print("original number: ", user_input) 21 | print("reverse number: ", reverse) -------------------------------------------------------------------------------- /100 Python Problems/17 - narcissistic num.py: -------------------------------------------------------------------------------- 1 | # Write a program that will take user input of a (4-digit number) and check whether the number is a narcissist number or not. 2 | 3 | user_input = int(input("Enter a four digit number: ")) 4 | 5 | num = user_input 6 | 7 | a = num % 10 8 | num = num // 10 9 | b = num % 10 10 | num = num // 10 11 | c = num % 10 12 | d = num // 10 13 | 14 | if (a**4) + (b**4) + (c**4) + (d**4) == user_input: 15 | print("Narcissist number") 16 | else: 17 | print("Not a Narcissist number") -------------------------------------------------------------------------------- /100 Python Problems/72 - dict upper-lower chars.py: -------------------------------------------------------------------------------- 1 | # Write a function that accepts a string and returns the number of uppercase characters and lowercase characters as a dictionary. 2 | 3 | a = input('Enter your string: ') 4 | 5 | D = {} 6 | 7 | upper_count = 0 8 | lower_count = 0 9 | 10 | for i in a: 11 | if i.isupper(): 12 | upper_count = upper_count + 1 13 | elif i.islower(): 14 | lower_count = lower_count + 1 15 | 16 | D = {'Upper case:': upper_count, 'Lower_case': lower_count} 17 | 18 | print(D) -------------------------------------------------------------------------------- /100 Python Problems/37 - reverse num.py: -------------------------------------------------------------------------------- 1 | # Find the reverse of a number provided by the user (any number of digits). 2 | 3 | a = int(input('Enter your number: ')) 4 | 5 | rev = 0 6 | 7 | while a > 0: 8 | rem = a % 10 9 | a = a // 10 10 | rev = (rev * 10) + rem 11 | 12 | print('Reversed value:', rev) 13 | 14 | # 657 15 | 16 | # rem = 657 % 10 = 7 17 | # a = 657 // 10 = 65 18 | # (0 * 10) + 7 = 7 19 | 20 | # 65 % 10 = 5 21 | # a = 65 // 10 = 6 22 | # rev = 7 * 10 + 5 = 75 23 | 24 | # rev = 75 * 10 + 6 25 | # rev = 756 -------------------------------------------------------------------------------- /100 Python Problems/64 - union & intersection lists.py: -------------------------------------------------------------------------------- 1 | # Write a program that can perform union and intersection on two given lists. 2 | 3 | L1 = [1, 2, 3, 4, 5] 4 | L2 = [3, 4, 5, 6, 7] 5 | 6 | union = [] 7 | intersection = [] 8 | 9 | # Union 10 | for i in L1: 11 | if i not in union: 12 | union.append(i) 13 | for j in L2: 14 | if j not in union: 15 | union.append(j) 16 | 17 | # Intersection 18 | for i in L1: 19 | if i in L2: 20 | intersection.append(i) 21 | 22 | print('Your union list:', union) 23 | print('Your intersection list:', intersection) -------------------------------------------------------------------------------- /100 Python Problems/42 - sum & avg.py: -------------------------------------------------------------------------------- 1 | # Write a program that continues to accept a number from the user until the user enters zero. 2 | # Display the sum and average of all the numbers. 3 | 4 | num = int(input('Enter your number: ')) 5 | 6 | add = 0 7 | avg = 0 8 | count = 0 9 | 10 | while True: 11 | if num != 0: 12 | add = add + num 13 | count = count + 1 14 | avg = add / count 15 | num = int(input('Another number please: ')) 16 | else: 17 | print('Thank you') 18 | break 19 | 20 | print('Your sum is:', add) 21 | print('Your average is:', avg) -------------------------------------------------------------------------------- /100 Python Problems/78 - int to string without str().py: -------------------------------------------------------------------------------- 1 | # Write a program that can convert an integer to a string without using str(). 2 | 3 | def convert(number): 4 | digits = '0123456789' 5 | result = '' 6 | 7 | while number != 0: 8 | current_num = number % 10 9 | result = digits[current_num] + result 10 | number = number // 10 11 | 12 | return result 13 | 14 | print(type(convert(23))) 15 | 16 | # number = 234 17 | # currect_num = 4 18 | # result = 4 19 | 20 | # number = 23 21 | # current_num = 3 22 | 23 | # result = 3 + 4 = 34 24 | 25 | # number = 2 -------------------------------------------------------------------------------- /100 Python Problems/27 - population calc.py: -------------------------------------------------------------------------------- 1 | # The current population of a town is 10,000. 2 | # The population of the town is increasing at a rate of 10% per year. 3 | # You have to write a program to find out the population at the end of each of the last 10 years. 4 | 5 | # For example, if the current population is 10,000, the output should be like this: 6 | # 10th year - 10,000 7 | # 9th year - 9,000 8 | # 8th year - 8,100, and so on. 9 | 10 | flag = 0 11 | a = 10000 12 | 13 | print(a) 14 | 15 | while True: 16 | b = (a) - ((10*a)/100) 17 | a = b 18 | print(int(b)) 19 | flag = flag + 1 20 | if flag == 9: 21 | break -------------------------------------------------------------------------------- /100 Python Problems/43 - simplify fraction.py: -------------------------------------------------------------------------------- 1 | # Write a program that accepts two numbers from the user, a numerator and a denominator, and then simplifies them. 2 | # For example, if the numerator is 5 and the denominator is 15, the answer should be 1/3. 3 | # For example, if the numerator is 6 and the denominator is 9, the answer should be 2/3. 4 | 5 | a = int(input('Enter your first number: ')) 6 | num_1 = a 7 | b = int(input('Enter your second number: ')) 8 | num_2 = b 9 | 10 | while num_1 % num_2 != 0: 11 | rem = num_1 % num_2 12 | num_1 = num_2 13 | num_2 = rem 14 | 15 | hcf = num_2 16 | 17 | a = a/hcf 18 | b = b/hcf 19 | 20 | print(a/b) -------------------------------------------------------------------------------- /100 Python Problems/76 - swap max min dict.py: -------------------------------------------------------------------------------- 1 | # Write a program to swap the key-value pairs for the maximum and minimum values. 2 | # For example, if the dictionary is like this {‘a’: 1, ‘b’: 2, ‘c’: 3}: 3 | # The output should be {a: 3, b: 2, c: 1}. 4 | 5 | D = {'a': 100, 'b': 200, 'c': 300, 'd': 400} 6 | 7 | max_val = max(D.values()) 8 | min_val = min(D.values()) 9 | 10 | for i in D: 11 | if D[i] == max_val: 12 | print("Maximum value:", D[i]) 13 | break 14 | 15 | for j in D: 16 | if D[j] == min_val: 17 | print("Minimum value:", D[j]) 18 | break 19 | 20 | D[i] = min_val 21 | D[j] = max_val 22 | 23 | print("Updated Dictionary:", D) -------------------------------------------------------------------------------- /100 Python Problems/70 - bag of words.py: -------------------------------------------------------------------------------- 1 | # Write a function that accepts a list of strings, performs Bag of Words, and converts it to numerical vectors. 2 | 3 | # https://en.wikipedia.org/wiki/Bag-of-words_model 4 | 5 | def bow(sentences): 6 | vocab = [] 7 | for i in sentences: 8 | vocab.extend(i.split()) 9 | vocab = list(set(vocab)) 10 | 11 | vector2d = [] 12 | for sentence in sentences: 13 | vector = [] 14 | for word in vocab: 15 | vector.append(sentence.count(word)) 16 | vector2d.append(vector) 17 | 18 | return vector2d 19 | 20 | sentences = [ 21 | 'Hello how are you', 22 | 'I was waiting for your call', 23 | 'where do you work', 24 | 'Lets meet someday', 25 | 'Hope you are fine' 26 | ] 27 | 28 | bow(sentences) -------------------------------------------------------------------------------- /100 Python Problems/67 - matrix mult possib.py: -------------------------------------------------------------------------------- 1 | # Write a program that can check whether you can perform matrix multiplication on two matrices. 2 | 3 | matrix_1 = [ 4 | [1, 2, 3], 5 | [4, 5, 6], 6 | [7, 8, 9] 7 | ] 8 | 9 | row_1 = 0 10 | 11 | for i in matrix_1: 12 | row_1 = row_1 + 1 13 | column_1 = len(i) 14 | print('Matrix_1', i) 15 | 16 | print('Row_1', row_1) 17 | print('Column_1', column_1) 18 | 19 | matrix_2 = [ 20 | [2, 3], 21 | [4, 5], 22 | [7, 8] 23 | ] 24 | 25 | row_2 = 0 26 | 27 | for j in matrix_2: 28 | row_2 = row_2 + 1 29 | column_2 = len(j) 30 | print('Matrix_2', j) 31 | 32 | print('Row_2', row_2) 33 | print('Column_2', column_2) 34 | 35 | if column_1 == row_2: 36 | print('Multiplication is possible') 37 | else: 38 | print('Not possible') -------------------------------------------------------------------------------- /100 Python Problems/14 - weather type.py: -------------------------------------------------------------------------------- 1 | # Write a program that will determine whether the value of temperature and humidity is provided by the user. 2 | 3 | # | TEMPERATURE(C) | HUMIDITY(%) | WEATHER | 4 | # |----------------|-------------|----------------| 5 | # | >= 30 | >= 90 | Hot and Humid | 6 | # | >= 30 | < 90 | Hot | 7 | # | < 30 | >= 90 | Cold and Humid | 8 | # | < 30 | < 90 | Cold | 9 | 10 | temp = int(input("Enter temperature in Celsius: ")) 11 | humid = int(input("Enter humidity percentage: ")) 12 | 13 | if temp >= 30 and humid >= 90: 14 | print("Hot and Humid") 15 | elif temp >= 30 and humid < 90: 16 | print("Hot") 17 | elif temp < 30 and humid >= 90: 18 | print("Cold and Humid") 19 | else: 20 | print("Cold") -------------------------------------------------------------------------------- /100 Python Problems/19 - menu driven.py: -------------------------------------------------------------------------------- 1 | # Write a menu-driven program with the following options: 2 | 3 | # 1. cm to inch, 4 | # 2. km to miles, 5 | # 3. USD to INR, 6 | # 4. exit. 7 | 8 | user_input = input(''' 9 | Hi! Welcome to my page 10 | What would you like to do? 11 | 12 | 1. Convert cm to inches 13 | 2. Convert km to miles 14 | 3. Convert usd to inr 15 | 4. Exit''') 16 | 17 | if user_input == '1': 18 | cm = float(input('Enter value in cm: ')) 19 | inch = cm * 0.394 20 | print('Your value in inches is:', inch) 21 | elif user_input == '2': 22 | km = float(input('Enter value in km: ')) 23 | miles = km * 0.621 24 | print('Your value in miles is:', miles) 25 | elif user_input == '3': 26 | usd = float(input('Enter value in usd: ')) 27 | inr = usd * 76.63 28 | print('Your value in inr is:', inr) 29 | else: 30 | print('Exit') -------------------------------------------------------------------------------- /100 Python Problems/71 - shortest dist coords.py: -------------------------------------------------------------------------------- 1 | # Write a program that accepts neighbors (a set of 2D coordinates) and a point (a single 2D coordinate) and tells the nearest neighbor (in terms of Euclidean distance). 2 | 3 | def nearest_neighbour(neighbours, you): 4 | def calculate_distance(c1, c2): 5 | return ((c1[0] - c2[0]) ** 2 + (c1[1] - c2[1]) ** 2) ** 0.5 6 | 7 | distances = [] 8 | 9 | for i in neighbours: 10 | distances.append(calculate_distance(you, i)) 11 | 12 | neighbour_index, distance = sorted(list(enumerate(distances)), key=lambda x: x[1])[0] 13 | 14 | print('Nearest neighbour is:', neighbour_index, 'at the distance of:', distance) 15 | 16 | neighbours = [ 17 | [89, 34], 18 | [12, 35], 19 | [10, 89], 20 | [90, 90] 21 | ] 22 | 23 | you = [-9, 7] 24 | 25 | nearest_neighbour(neighbours, you) -------------------------------------------------------------------------------- /100 Python Problems/41 - series sum.py: -------------------------------------------------------------------------------- 1 | # Write a program to find the sum of the series up to the nth term: 2 | 3 | # 1 + x^2/2 + x^3/3 + ... + x^n/n 4 | 5 | # The value of n will be provided by the user. 6 | 7 | x = int(input('Enter your number: ')) 8 | n = int(input('Enter nth value: ')) 9 | 10 | sum = 1 11 | 12 | for i in range(2, n + 1): 13 | sum = sum + ((x**i)/i) 14 | 15 | print(sum) 16 | 17 | # The natural logarithm can be approximated by the following series: 18 | 19 | # (x-1)/x + 1/2 * ((x-1)/x)^2 + 1/2 * ((x-1)/x)^3 + ... + 1/2 * ((x-1)/x)^7 20 | 21 | # If x is input through the keyboard, write a program to calculate the sum of the first seven terms of this series. 22 | 23 | x = int(input('Enter your number: ')) 24 | 25 | n = 7 26 | 27 | sum = ((x - 1) / x) 28 | 29 | for i in range(2, n + 1): 30 | sum = sum + (1 / 2 * ((x - 1) / x)**i) 31 | 32 | print(sum) -------------------------------------------------------------------------------- /100 Python Problems/39 - patterns nested.py: -------------------------------------------------------------------------------- 1 | # To print different patterns using nested loops. 2 | 3 | # * 4 | # ** 5 | # *** 6 | # **** 7 | 8 | row = int(input('Enter number of rows: ')) 9 | 10 | for i in range(1, row + 1): 11 | for j in range(0, i): 12 | print('*', end=' ') 13 | print('') 14 | 15 | # * 16 | # ** 17 | # *** 18 | # ** 19 | # * 20 | 21 | row = int(input('Enter number of rows: ')) 22 | 23 | for i in range(1, row + 1): 24 | for j in range(0, i): 25 | print('*', end=' ') 26 | print('') 27 | 28 | for k in range(row, 0, -1): 29 | for l in range(0, k - 1): 30 | print('*', end=' ') 31 | print('') 32 | 33 | # 1 34 | # 121 35 | # 12321 36 | # 1234321 37 | 38 | row = int(input('Enter number of rows: ')) 39 | 40 | for i in range(1, row + 1): 41 | for j in range(1, i + 1): 42 | print(j, end=' ') 43 | for k in range(i - 1, 0, -1): 44 | print(k, end=' ') 45 | print(' ') 46 | 47 | # 1 48 | # 23 49 | # 456 50 | 51 | row = int(input('Enter row value: ')) 52 | 53 | num = 1 54 | 55 | for i in range(1, row + 1): 56 | for j in range(1, i + 1): 57 | print(num, end=' ') 58 | num = num + 1 59 | print(' ') -------------------------------------------------------------------------------- /100 Days Python/Day 03 - comments.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "ba6ccc71", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# comments" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "id": "74a11b94", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "# this is a comment" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "id": "d03e2e8c", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "# my name is saurabh\n", 31 | "# I love python" 32 | ] 33 | } 34 | ], 35 | "metadata": { 36 | "kernelspec": { 37 | "display_name": "Python 3 (ipykernel)", 38 | "language": "python", 39 | "name": "python3" 40 | }, 41 | "language_info": { 42 | "codemirror_mode": { 43 | "name": "ipython", 44 | "version": 3 45 | }, 46 | "file_extension": ".py", 47 | "mimetype": "text/x-python", 48 | "name": "python", 49 | "nbconvert_exporter": "python", 50 | "pygments_lexer": "ipython3", 51 | "version": "3.10.9" 52 | } 53 | }, 54 | "nbformat": 4, 55 | "nbformat_minor": 5 56 | } 57 | -------------------------------------------------------------------------------- /100 Python Problems/77 - login and reg.py: -------------------------------------------------------------------------------- 1 | # Write a dummy program that can perform login and registration using a menu-driven approach. 2 | 3 | database = {} 4 | 5 | def user_menu(): 6 | user_input = input(''' 7 | 1. Enter 1 to Register 8 | 2. Enter 2 to Login 9 | 3. Enter 3 to Exit 10 | ''') 11 | 12 | if user_input == '1': 13 | register() 14 | elif user_input == '2': 15 | login() 16 | else: 17 | print('Bye') 18 | 19 | def register(): 20 | name = input('Enter your name: ') 21 | email = input('Enter your email id: ') 22 | password = input('Enter your password: ') 23 | 24 | database[email] = [name, password] 25 | 26 | print('Registration successful') 27 | print() 28 | 29 | user_menu() 30 | 31 | def login(): 32 | email = input('Enter your email id: ') 33 | password = input('Enter your password: ') 34 | 35 | flag = 0 36 | 37 | for i in database: 38 | if email == i: 39 | flag = 1 40 | if password == database[i][1]: 41 | print('Welcome') 42 | else: 43 | print('Incorrect credential') 44 | 45 | if flag == 0: 46 | print('Email not found') 47 | 48 | user_menu() -------------------------------------------------------------------------------- /100 Python Problems/18 - inhand salary.py: -------------------------------------------------------------------------------- 1 | # Write a program that calculates the in-hand salary after deducting HRA (10%), DA (5%), PF (3%), and tax. 2 | # If the salary is between 5-10 lakh, apply a 10% tax; 3 | # for salaries between 11-20 lakh, apply a 20% tax; 4 | # for salaries above 20 lakh, apply a 30% tax. 5 | # If the salary is between 0-1 lakh, print 'k'. 6 | 7 | user_input = float(input('Enter your annual salary: ')) 8 | 9 | if user_input > 500000 and user_input < 1000000: 10 | tax = (10/100) * user_input 11 | temp_salary = user_input - tax 12 | elif user_input > 1000000 and user_input < 2000000: 13 | tax = (20/100) * user_input 14 | temp_salary = user_input - tax 15 | else: 16 | tax = (30/100) * user_input 17 | temp_salary = user_input - tax 18 | 19 | print('After salary reduction:', temp_salary) 20 | 21 | hra = (10/100) * temp_salary 22 | da = (5/100) * temp_salary 23 | pf = (3/100) * temp_salary 24 | 25 | inhand_salary = (temp_salary - hra - da - pf) / 12 26 | print('Inhand salary is:', inhand_salary) 27 | 28 | if inhand_salary <= 999: 29 | print(inhand_salary) 30 | elif inhand_salary >= 1000 and inhand_salary <= 9999: 31 | print(inhand_salary / 1000, 'k') 32 | elif inhand_salary >= 100000 and inhand_salary <= 9999999: 33 | print(inhand_salary / 1000000, 'l') 34 | else: 35 | print(inhand_salary / 10000000, 'Cr') -------------------------------------------------------------------------------- /100 Days Python/Day 10 - indentation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "67d9c38e", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# In Other Languages\n", 11 | "if(name == \"xyz\"){\n", 12 | " something;\n", 13 | " something;\n", 14 | "}else{\n", 15 | " something else;\n", 16 | " something else;\n", 17 | "}" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "id": "9c643e75", 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "line1\n", 31 | "line2\n", 32 | "line5\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "# In Python\n", 38 | "name = \"xyz\"\n", 39 | "if name == \"xyz\":\n", 40 | " print(\"line1\")\n", 41 | " print(\"line2\")\n", 42 | " if 5 == 5:\n", 43 | " print(\"line5\")\n", 44 | "else:\n", 45 | " print(\"line3\")" 46 | ] 47 | } 48 | ], 49 | "metadata": { 50 | "kernelspec": { 51 | "display_name": "Python 3 (ipykernel)", 52 | "language": "python", 53 | "name": "python3" 54 | }, 55 | "language_info": { 56 | "codemirror_mode": { 57 | "name": "ipython", 58 | "version": 3 59 | }, 60 | "file_extension": ".py", 61 | "mimetype": "text/x-python", 62 | "name": "python", 63 | "nbconvert_exporter": "python", 64 | "pygments_lexer": "ipython3", 65 | "version": "3.10.9" 66 | } 67 | }, 68 | "nbformat": 4, 69 | "nbformat_minor": 5 70 | } 71 | -------------------------------------------------------------------------------- /100 Python Problems/74 - most used word song.py: -------------------------------------------------------------------------------- 1 | # Write a program that can find the most used word in a song. 2 | 3 | lyrics = '''I got room 4 | In my fumes (yeah) 5 | She fill my mind up with ideas 6 | I'm the highest in the room (it's lit) 7 | Hope I make it outta here (let's go) 8 | She saw my eyes, she know I'm gone (ah) 9 | I see some things that you might fear 10 | I'm doin' a show, I'll be back soon (soon) 11 | That ain't what she wanna hear (nah) 12 | Now I got her in my room (ah) 13 | Legs wrapped around my beard 14 | Got the fastest car, it zoom (skrrt) 15 | Hope we make it outta here (ah) 16 | When I'm with you, I feel alive 17 | You say you love me, don't you lie (yeah) 18 | Won't cross my heart, don't wanna die 19 | Keep the pistol on my side (yeah) 20 | Case it's fumes (smoke) 21 | She fill my mind up with ideas (straight up) 22 | I'm the highest in the room (it's lit) 23 | Hope I make it outta here (let's go, yeah) 24 | We ain't stressin' 'bout the loot (yeah) 25 | My block made of queseria 26 | This not the Molly, this the boot 27 | Ain't no comin' back from here 28 | Live the life of La Familia 29 | It's so much gang that I can't see ya (yeah) 30 | Turn it up 'til they can't hear (we can't) 31 | Runnin', runnin' 'round for the thrill 32 | Yeah, dawg, dawg, 'round my real (gang) 33 | Raw, raw, I been pourin' to the real (drank) 34 | Nah, nah, nah, they not back of the VIP (in the VIP) 35 | Gorgeous, baby, keep me hard as steel 36 | Ah, this my life, I did not choose 37 | Uh, been on this since we was kids 38 | We gon' stay on top and break the rules 39 | Uh, I fill my mind up with ideas 40 | Case it's fumes 41 | She fill my mind up with ideas (straight up) 42 | I'm the highest in the room (I'm the highest, it's lit) 43 | Hope I make it outta here 44 | I'm the highest, you might got the Midas touch 45 | What the vibe is? And my bitch the vibiest, yeah 46 | Everyone excited, everyone too excited, yeah now 47 | Play with the giants, little bit too extravagant, yeah now 48 | Night, everyone feel my vibe, yeah 49 | In the broad day, everyone hypnotizing, yeah 50 | I'm okay and I take the cake, yeah''' 51 | 52 | D = {} 53 | 54 | for word in lyrics.split(): 55 | if word in D: 56 | D[word] = D[word] + 1 57 | else: 58 | D[word] = 1 59 | 60 | max_val = max(D.values()) 61 | 62 | for i in D: 63 | if D[i] == max_val: 64 | print(i, max_val) 65 | break -------------------------------------------------------------------------------- /100 Days Python/Day 12 - guessing-game.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "eb2b2057", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import random" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "id": "524fa522", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/plain": [ 22 | "31" 23 | ] 24 | }, 25 | "execution_count": 2, 26 | "metadata": {}, 27 | "output_type": "execute_result" 28 | } 29 | ], 30 | "source": [ 31 | "random.randint(1, 100)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 3, 37 | "id": "11b1273b", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "Chal guess kar: 50\n", 45 | "Guess higher\n", 46 | "Chal guess kar: 60\n", 47 | "Guess higher\n", 48 | "Chal guess kar: 70\n", 49 | "Guess higher\n", 50 | "Chal guess kar: 80\n", 51 | "Guess lower\n", 52 | "Chal guess kar: 75\n", 53 | "Guess lower\n", 54 | "Chal guess kar: 73\n", 55 | "Guess higher\n", 56 | "Chal guess kar: 74\n", 57 | "Sahi Jawab\n", 58 | "You took 7 attempts\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "jackpot = random.randint(1, 100)\n", 64 | "guess = int(input(\"Chal guess kar: \"))\n", 65 | "counter = 1\n", 66 | "while guess != jackpot:\n", 67 | " if guess < jackpot:\n", 68 | " print(\"Guess higher\")\n", 69 | " else:\n", 70 | " print(\"Guess lower\") \n", 71 | " guess = int(input(\"Chal guess kar: \"))\n", 72 | " counter += 1\n", 73 | "print(\"Sahi Jawab\")\n", 74 | "print(\"You took\", counter, \"attempts\")" 75 | ] 76 | } 77 | ], 78 | "metadata": { 79 | "kernelspec": { 80 | "display_name": "Python 3 (ipykernel)", 81 | "language": "python", 82 | "name": "python3" 83 | }, 84 | "language_info": { 85 | "codemirror_mode": { 86 | "name": "ipython", 87 | "version": 3 88 | }, 89 | "file_extension": ".py", 90 | "mimetype": "text/x-python", 91 | "name": "python", 92 | "nbconvert_exporter": "python", 93 | "pygments_lexer": "ipython3", 94 | "version": "3.10.9" 95 | } 96 | }, 97 | "nbformat": 4, 98 | "nbformat_minor": 5 99 | } 100 | -------------------------------------------------------------------------------- /100 Days Python/Day 14 - nested-loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "19d3e00e", 6 | "metadata": {}, 7 | "source": [ 8 | "# Nested Loops" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "bed7a082", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "1 1\n", 22 | "1 2\n", 23 | "1 3\n", 24 | "1 4\n", 25 | "2 1\n", 26 | "2 2\n", 27 | "2 3\n", 28 | "2 4\n", 29 | "3 1\n", 30 | "3 2\n", 31 | "3 3\n", 32 | "3 4\n", 33 | "4 1\n", 34 | "4 2\n", 35 | "4 3\n", 36 | "4 4\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "# Unique Pairs Example\n", 42 | "\n", 43 | "for i in range(1, 5):\n", 44 | " for j in range(1, 5):\n", 45 | " print(i, j)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "id": "239e2940", 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "Enter rows: 5\n", 59 | "* \n", 60 | "* * \n", 61 | "* * * \n", 62 | "* * * * \n", 63 | "* * * * * \n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "# *\n", 69 | "# * *\n", 70 | "# * * *\n", 71 | "# * * * *\n", 72 | "# * * * * *\n", 73 | "\n", 74 | "rows = int(input(\"Enter rows: \"))\n", 75 | "for i in range(1, rows + 1):\n", 76 | " for j in range(0, i):\n", 77 | " print(\"*\", end=\" \")\n", 78 | " print(\"\")" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 3, 84 | "id": "5b6d1a4f", 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "Enter rows: 4\n", 92 | "1\n", 93 | "121\n", 94 | "12321\n", 95 | "1234321\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "# 1\n", 101 | "# 121\n", 102 | "# 12321\n", 103 | "# 1234321\n", 104 | "\n", 105 | "rows = int(input('Enter rows: '))\n", 106 | "for i in range(1, rows+1):\n", 107 | " for j in range(1, i+1):\n", 108 | " print(j, end='')\n", 109 | " for k in range(i-1, 0, -1):\n", 110 | " print(k, end='')\n", 111 | " print()" 112 | ] 113 | } 114 | ], 115 | "metadata": { 116 | "kernelspec": { 117 | "display_name": "Python 3 (ipykernel)", 118 | "language": "python", 119 | "name": "python3" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 3 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython3", 131 | "version": "3.10.9" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 5 136 | } 137 | -------------------------------------------------------------------------------- /100 Days Python/Day 15 - break-continue-pass.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7e66d998", 6 | "metadata": {}, 7 | "source": [ 8 | "# Loop Control Statements\n", 9 | "\n", 10 | "**Break**: Ends loop immediately.\n", 11 | "\n", 12 | "**Continue**: Skips to next iteration.\n", 13 | "\n", 14 | "**Pass**: Placeholder; does nothing." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "id": "c0a2c0aa", 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "name": "stdout", 25 | "output_type": "stream", 26 | "text": [ 27 | "1\n", 28 | "2\n", 29 | "3\n", 30 | "4\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "for i in range(1, 11):\n", 36 | " if i == 5:\n", 37 | " break\n", 38 | " print(i)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 2, 44 | "id": "d3d80fe1", 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "lower: 10\n", 52 | "upper: 100\n", 53 | "11\n", 54 | "13\n", 55 | "17\n", 56 | "19\n", 57 | "23\n", 58 | "29\n", 59 | "31\n", 60 | "37\n", 61 | "41\n", 62 | "43\n", 63 | "47\n", 64 | "53\n", 65 | "59\n", 66 | "61\n", 67 | "67\n", 68 | "71\n", 69 | "73\n", 70 | "79\n", 71 | "83\n", 72 | "89\n", 73 | "97\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "lower = int(input('lower: '))\n", 79 | "upper = int(input('upper: '))\n", 80 | "for i in range(lower, upper+1):\n", 81 | " for j in range(2, i):\n", 82 | " if i % j == 0:\n", 83 | " break\n", 84 | " else:\n", 85 | " print(i)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 3, 91 | "id": "87a729be", 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "1\n", 99 | "2\n", 100 | "3\n", 101 | "4\n", 102 | "6\n", 103 | "7\n", 104 | "8\n", 105 | "9\n", 106 | "10\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "for i in range(1, 11):\n", 112 | " if i == 5:\n", 113 | " continue\n", 114 | " print(i)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 4, 120 | "id": "f0096bb1", 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "1\n", 128 | "Hello\n", 129 | "2\n", 130 | "Hello\n", 131 | "3\n", 132 | "Hello\n", 133 | "4\n", 134 | "Hello\n", 135 | "6\n", 136 | "Hello\n", 137 | "7\n", 138 | "Hello\n", 139 | "8\n", 140 | "Hello\n", 141 | "9\n", 142 | "Hello\n", 143 | "10\n", 144 | "Hello\n" 145 | ] 146 | } 147 | ], 148 | "source": [ 149 | "for i in range(1, 11):\n", 150 | " if i == 5:\n", 151 | " continue\n", 152 | " print(i)\n", 153 | " print(\"Hello\")" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 5, 159 | "id": "2adc4523", 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "for i in range(1, 11):\n", 164 | " pass" 165 | ] 166 | } 167 | ], 168 | "metadata": { 169 | "kernelspec": { 170 | "display_name": "Python 3 (ipykernel)", 171 | "language": "python", 172 | "name": "python3" 173 | }, 174 | "language_info": { 175 | "codemirror_mode": { 176 | "name": "ipython", 177 | "version": 3 178 | }, 179 | "file_extension": ".py", 180 | "mimetype": "text/x-python", 181 | "name": "python", 182 | "nbconvert_exporter": "python", 183 | "pygments_lexer": "ipython3", 184 | "version": "3.10.9" 185 | } 186 | }, 187 | "nbformat": 4, 188 | "nbformat_minor": 5 189 | } 190 | -------------------------------------------------------------------------------- /100 Days Python/Day 05 - keywords-identifiers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b830be5b", 6 | "metadata": {}, 7 | "source": [ 8 | "# Keywords" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "fe185089", 14 | "metadata": {}, 15 | "source": [ 16 | "**Python is case sensitive**" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "40f7d967", 22 | "metadata": {}, 23 | "source": [ 24 | "- *Keywords*: reserved words, special meanings\n", 25 | "- Cannot use as **variable names**" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "id": "e9239680", 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "# Python has 33 keywords.\n", 44 | "import keyword\n", 45 | "print(keyword.kwlist)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "id": "fd99ac49", 51 | "metadata": {}, 52 | "source": [ 53 | "# Identifiers\n", 54 | "\n", 55 | "**Identifier** are name for var, func, class, module, etc.\n", 56 | "\n", 57 | "**Rules**:\n", 58 | "- Start with letter or `_`\n", 59 | "- Followed by letters, digits, `_`\n", 60 | "- Not a Python keyword" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 2, 66 | "id": "484e5c3f", 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "Saurabh\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "name = 'Saurabh'\n", 79 | "print(name)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "id": "01681557", 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "Saurabh\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "_ = 'Saurabh'\n", 98 | "print(_)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 4, 104 | "id": "2e8cbed9", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "Saurabh\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "first_name = 'Saurabh'\n", 117 | "print(first_name)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 5, 123 | "id": "fa836405", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "ename": "SyntaxError", 128 | "evalue": "cannot assign to False (Temp/ipykernel_7012/1676416165.py, line 1)", 129 | "output_type": "error", 130 | "traceback": [ 131 | "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\pc\\AppData\\Local\\Temp/ipykernel_7012/1676416165.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m False = 'Saurabh'\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot assign to False\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "False = 'Saurabh'\n", 137 | "print(False)" 138 | ] 139 | } 140 | ], 141 | "metadata": { 142 | "kernelspec": { 143 | "display_name": "Python 3 (ipykernel)", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.10.9" 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 5 162 | } 163 | -------------------------------------------------------------------------------- /100 Days Python/Day 09 - decision-control.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "db0bdab5", 6 | "metadata": {}, 7 | "source": [ 8 | "# if-else" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "bf6ea54a", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Apna email bata: saurabh@gmail.com\n", 22 | "Apna password bhi bata: bolo1234\n", 23 | "Password incorrect\n", 24 | "Password fir se bol: 1234\n", 25 | "Finally correct\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "# correct email - saurabh@gmail.com\n", 31 | "# password = 1234\n", 32 | "\n", 33 | "email = input(\"Apna email bata: \")\n", 34 | "if \"@\" in email:\n", 35 | " password = input(\"Apna password bhi bata: \")\n", 36 | " \n", 37 | " if email == \"saurabh@gmail.com\" and password == \"1234\":\n", 38 | " print(\"Welcome\")\n", 39 | " elif email == \"saurabh@gmail.com\" and password != \"1234\":\n", 40 | " print(\"Password incorrect\")\n", 41 | " password = input(\"Password fir se bol: \")\n", 42 | " if password ==\"1234\":\n", 43 | " print(\"Finally correct\")\n", 44 | " else:\n", 45 | " print(\"Still incorrect\")\n", 46 | " else:\n", 47 | " print(\"Incorrect credentials\")\n", 48 | "else:\n", 49 | " print(\"Email galat hai sahi likho: \")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 1, 55 | "id": "5c5e3423", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "# if-else Examples\n", 60 | "# 1. Min of 3 Numbers\n", 61 | "# 2. Menu Driven Program" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 2, 67 | "id": "c935319a", 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "first num: 4\n", 75 | "second num: 1\n", 76 | "third num: 10\n", 77 | "smallest is 1\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "# 1. Min of 3 Numbers\n", 83 | "a = int(input('first num: '))\n", 84 | "b = int(input('second num: '))\n", 85 | "c = int(input('third num: '))\n", 86 | "\n", 87 | "if a < b and a < c:\n", 88 | " print('smallest is', a)\n", 89 | "elif b < c:\n", 90 | " print('smallest is', b)\n", 91 | "else:\n", 92 | " print('smallest is', c)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 3, 98 | "id": "6350150c", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "\n", 106 | "Hi! how can I help you.\n", 107 | "1. Enter 1 for pin change\n", 108 | "2. Enter 2 for balance check\n", 109 | "3. Enter 3 for withdrawl\n", 110 | "4. Enter 4 for exit\n", 111 | "2\n", 112 | "balance\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "# 2. Menu-Driven Calculator\n", 118 | "menu = input(\"\"\"\n", 119 | "Hi! how can I help you.\n", 120 | "1. Enter 1 for pin change\n", 121 | "2. Enter 2 for balance check\n", 122 | "3. Enter 3 for withdrawl\n", 123 | "4. Enter 4 for exit\n", 124 | "\"\"\")\n", 125 | "\n", 126 | "if menu == '1':\n", 127 | " print('pin change')\n", 128 | "elif menu == '2':\n", 129 | " print('balance')\n", 130 | "else:\n", 131 | " print('exit')" 132 | ] 133 | } 134 | ], 135 | "metadata": { 136 | "kernelspec": { 137 | "display_name": "Python 3 (ipykernel)", 138 | "language": "python", 139 | "name": "python3" 140 | }, 141 | "language_info": { 142 | "codemirror_mode": { 143 | "name": "ipython", 144 | "version": 3 145 | }, 146 | "file_extension": ".py", 147 | "mimetype": "text/x-python", 148 | "name": "python", 149 | "nbconvert_exporter": "python", 150 | "pygments_lexer": "ipython3", 151 | "version": "3.10.9" 152 | } 153 | }, 154 | "nbformat": 4, 155 | "nbformat_minor": 5 156 | } 157 | -------------------------------------------------------------------------------- /100 Days Python/Day 01 - print-function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "be4809e9", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Hello World\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# Python is Case-Sensitive\n", 19 | "\n", 20 | "print(\"Hello World\")" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "id": "63093bf1", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "6\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "print(6)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "id": "4cda87fd", 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "5.6\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "print(5.6)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "id": "5880a38a", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "False\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "print(False)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 5, 80 | "id": "9608ad01", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "India Pakistan Nepal Srilanka\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "print(\"India\", \"Pakistan\", \"Nepal\", \"Srilanka\")" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 6, 98 | "id": "e37cdfa8", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "India 5 True\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "print(\"India\", 5, True)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 7, 116 | "id": "f75bd676", 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "India/Pakistan/Nepal/Srilanka\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "print(\"India\", \"Pakistan\", \"Nepal\", \"Srilanka\", sep='/')" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 8, 134 | "id": "32809dee", 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "India-Pakistan-Nepal-Srilanka\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "print(\"India\", \"Pakistan\", \"Nepal\", \"Srilanka\", sep='-')" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 9, 152 | "id": "029f9994", 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "Hello\n", 160 | "World\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "print(\"Hello\")\n", 166 | "print(\"World\")" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 10, 172 | "id": "5d0c94b6", 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "Hello World\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "print(\"Hello\", end=' ')\n", 185 | "print(\"World\")" 186 | ] 187 | } 188 | ], 189 | "metadata": { 190 | "kernelspec": { 191 | "display_name": "Python 3 (ipykernel)", 192 | "language": "python", 193 | "name": "python3" 194 | }, 195 | "language_info": { 196 | "codemirror_mode": { 197 | "name": "ipython", 198 | "version": 3 199 | }, 200 | "file_extension": ".py", 201 | "mimetype": "text/x-python", 202 | "name": "python", 203 | "nbconvert_exporter": "python", 204 | "pygments_lexer": "ipython3", 205 | "version": "3.10.9" 206 | } 207 | }, 208 | "nbformat": 4, 209 | "nbformat_minor": 5 210 | } 211 | -------------------------------------------------------------------------------- /100 Days Python/Day 07 - literals.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ad76d5e3", 6 | "metadata": {}, 7 | "source": [ 8 | "# Literals\n", 9 | "\n", 10 | "Literals = basic **data values** for variables.\n", 11 | "\n", 12 | "Types in Python:\n", 13 | "- *Numeric literals*\n", 14 | "- *String literals*\n", 15 | "- *Boolean literals*\n", 16 | "- *Special literals*" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "d3e7577f", 22 | "metadata": {}, 23 | "source": [ 24 | "## 1. Numeric literals" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "id": "768c9a57", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "10 100 200 300\n", 38 | "10.5 150.0 0.0015\n", 39 | "3.14j 3.14 0.0\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "# Integer Literals\n", 45 | "a = 0b1010 # Binary\n", 46 | "b = 100 # Decimal\n", 47 | "c = 0o310 # Octal\n", 48 | "d = 0x12c # Hex\n", 49 | "\n", 50 | "# Float Literals\n", 51 | "f1 = 10.5\n", 52 | "f2 = 1.5e2\n", 53 | "f3 = 1.5e-3\n", 54 | "\n", 55 | "# Complex Literal\n", 56 | "x = 3.14j\n", 57 | "\n", 58 | "print(a, b, c, d)\n", 59 | "print(f1, f2, f3)\n", 60 | "print(x, x.imag, x.real)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "id": "5dc50986", 66 | "metadata": {}, 67 | "source": [ 68 | "## 2. String literals" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 1, 74 | "id": "ee4c0d69", 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "This is Python\n", 82 | "This is Python\n", 83 | "C\n", 84 | "This is a multiline string with more than one line code.\n", 85 | "😀😆🤣\n", 86 | "raw \\n string\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "string = 'This is Python'\n", 92 | "strings = \"This is Python\"\n", 93 | "char = \"C\"\n", 94 | "multiline_str = \"\"\"This is a multiline string with more than one line code.\"\"\"\n", 95 | "unicode = u\"\\U0001f600\\U0001F606\\U0001F923\"\n", 96 | "raw_str = r\"raw \\n string\"\n", 97 | "\n", 98 | "print(string)\n", 99 | "print(strings)\n", 100 | "print(char)\n", 101 | "print(multiline_str)\n", 102 | "print(unicode)\n", 103 | "print(raw_str)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "id": "8155238b", 109 | "metadata": {}, 110 | "source": [ 111 | "## 3. Boolean literals" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 3, 117 | "id": "a6abac0d", 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "a: 5\n", 125 | "b: 10\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "a = True + 4 # True == 1\n", 131 | "b = False + 10 # False == 0\n", 132 | "\n", 133 | "print(\"a:\", a)\n", 134 | "print(\"b:\", b)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "id": "101fd351", 140 | "metadata": {}, 141 | "source": [ 142 | "## 4. Special literals" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 4, 148 | "id": "7fff7507", 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "None\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "a = None\n", 161 | "print(a)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 5, 167 | "id": "f63271b0", 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [ 171 | "# Variable Declaration\n", 172 | "k = None" 173 | ] 174 | } 175 | ], 176 | "metadata": { 177 | "kernelspec": { 178 | "display_name": "Python 3 (ipykernel)", 179 | "language": "python", 180 | "name": "python3" 181 | }, 182 | "language_info": { 183 | "codemirror_mode": { 184 | "name": "ipython", 185 | "version": 3 186 | }, 187 | "file_extension": ".py", 188 | "mimetype": "text/x-python", 189 | "name": "python", 190 | "nbconvert_exporter": "python", 191 | "pygments_lexer": "ipython3", 192 | "version": "3.10.9" 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 5 197 | } 198 | -------------------------------------------------------------------------------- /100 Days Python/Day 11 - while-loop.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7c0dab1a", 6 | "metadata": {}, 7 | "source": [ 8 | "# Loops in Python\n", 9 | "\n", 10 | "**Need**\n", 11 | "- Repeat code block.\n", 12 | "- **Iteration**: Execute same code repeatedly.\n", 13 | "\n", 14 | "**While Loop**\n", 15 | "- Executes while *condition* true.\n", 16 | "\n", 17 | "**For Loop**\n", 18 | "- Executes for each *item* in *iterable*." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "id": "b6220870", 24 | "metadata": {}, 25 | "source": [ 26 | "# While Loop" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "id": "33b5564d", 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "# While Loop Example" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "id": "812c44ea", 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# While Loop\n", 47 | "# For Loop" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "id": "ae8e9296", 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "# C\n", 58 | "while(condition){\n", 59 | " code\n", 60 | "}" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "id": "4af21edf", 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "# Python\n", 71 | "while condition:\n", 72 | " code" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 1, 78 | "id": "171a0774", 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "Enter the number: 11\n", 86 | "11\n", 87 | "22\n", 88 | "33\n", 89 | "44\n", 90 | "55\n", 91 | "66\n", 92 | "77\n", 93 | "88\n", 94 | "99\n", 95 | "110\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "# 1. Print Multiplication Table\n", 101 | "number = int(input(\"Enter the number: \"))\n", 102 | "i = 1\n", 103 | "while i < 11:\n", 104 | " print(number * i)\n", 105 | " i += 1" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 2, 111 | "id": "ba5007a4", 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "Enter the number: 11\n", 119 | "11 * 1 = 11\n", 120 | "11 * 2 = 22\n", 121 | "11 * 3 = 33\n", 122 | "11 * 4 = 44\n", 123 | "11 * 5 = 55\n", 124 | "11 * 6 = 66\n", 125 | "11 * 7 = 77\n", 126 | "11 * 8 = 88\n", 127 | "11 * 9 = 99\n", 128 | "11 * 10 = 110\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "number = int(input(\"Enter the number: \"))\n", 134 | "i = 1\n", 135 | "while i < 11:\n", 136 | " print(number, \"*\", i, \"=\", number * i)\n", 137 | " i += 1" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 2, 143 | "id": "6048ec1e", 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "1\n", 151 | "2\n", 152 | "limit crossed\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "# while loop with else\n", 158 | "x = 1\n", 159 | "while x < 3:\n", 160 | " print(x)\n", 161 | " x += 1\n", 162 | "else:\n", 163 | " print('limit crossed')" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 3, 169 | "id": "3a71a65e", 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "Enter the number: 1234\n", 177 | "Sum of digits: 10\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "# 2. Sum of Digits\n", 183 | "number = int(input(\"Enter the number: \"))\n", 184 | "sum_digits = 0\n", 185 | "while number > 0:\n", 186 | " digit = number % 10\n", 187 | " sum_digits += digit\n", 188 | " number //= 10\n", 189 | "print(f\"Sum of digits: {sum_digits}\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 4, 195 | "id": "b44db62e", 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "Enter a number (0 to stop): 5\n", 203 | "Enter a number (0 to stop): 4\n", 204 | "Enter a number (0 to stop): 3\n", 205 | "Enter a number (0 to stop): 2\n", 206 | "Enter a number (0 to stop): 1\n", 207 | "Enter a number (0 to stop): 0\n", 208 | "Average of entered numbers: 3.0\n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "# 3. Average of Numbers Until 0\n", 214 | "count = 0\n", 215 | "total = 0\n", 216 | "while True:\n", 217 | " number = int(input(\"Enter a number (0 to stop): \"))\n", 218 | " if number == 0:\n", 219 | " break\n", 220 | " total += number\n", 221 | " count += 1\n", 222 | "if count > 0:\n", 223 | " average = total / count\n", 224 | " print(f\"Average of entered numbers: {average}\")\n", 225 | "else:\n", 226 | " print(\"No numbers entered.\")" 227 | ] 228 | } 229 | ], 230 | "metadata": { 231 | "kernelspec": { 232 | "display_name": "Python 3 (ipykernel)", 233 | "language": "python", 234 | "name": "python3" 235 | }, 236 | "language_info": { 237 | "codemirror_mode": { 238 | "name": "ipython", 239 | "version": 3 240 | }, 241 | "file_extension": ".py", 242 | "mimetype": "text/x-python", 243 | "name": "python", 244 | "nbconvert_exporter": "python", 245 | "pygments_lexer": "ipython3", 246 | "version": "3.10.9" 247 | } 248 | }, 249 | "nbformat": 4, 250 | "nbformat_minor": 5 251 | } 252 | -------------------------------------------------------------------------------- /100 Days Python/Day 04 - variables.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f39ae936", 6 | "metadata": {}, 7 | "source": [ 8 | "**Static vs Dynamic Typing:**\n", 9 | "- **Dynamic Typing (Python)**: Types change during execution.\n", 10 | "- **Static Typing**: Fixed types before runtime.\n", 11 | "\n", 12 | "**Static vs Dynamic Binding:**\n", 13 | "- **Static Binding**: Types fixed at compile-time.\n", 14 | "- **Dynamic Binding**: Types associated at runtime, changeable during execution.\n", 15 | "\n", 16 | "**Stylish Declaration Techniques:**\n", 17 | "- Clear, efficient variable/structure declarations in Python.\n", 18 | "- Use Python's flexibility for concise, readable code." 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "id": "6ff186bf", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "# c\n", 29 | "int a = 5;" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "id": "a2f7579b", 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "name = 'Saurabh'" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "id": "240c9a22", 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "Saurabh\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "print(name)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 4, 63 | "id": "440b727b", 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "Hello World\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "name = 'Hello World'\n", 76 | "print(name)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "id": "fdd4c2e2", 82 | "metadata": {}, 83 | "source": [ 84 | "**No Var Decl in Python**\n", 85 | "- Use vars **w/o prior decl**.\n", 86 | "- Assign val directly; Python auto-creates." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "id": "4946e220", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "# Dynamic Typing (PHP)\n", 97 | "$a = 5;\n", 98 | "\n", 99 | "# Static Typing (Java)\n", 100 | "int a = 5;" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 7, 106 | "id": "2b86608d", 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "name": "stdout", 111 | "output_type": "stream", 112 | "text": [ 113 | "Hello World\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "print(name)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 8, 124 | "id": "2c6e4e24", 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "4\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "name = 4\n", 137 | "print(name)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 9, 143 | "id": "e2b4589a", 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "True\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "name = True\n", 156 | "print(name)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 1, 162 | "id": "a7d662a9", 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "5\n", 170 | "nitish\n" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "# Dynamic Binding Example\n", 176 | "a = 5 # Assign integer\n", 177 | "print(a)\n", 178 | "\n", 179 | "a = 'nitish' # Reassign string\n", 180 | "print(a)" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 11, 186 | "id": "1d237b00", 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "# Static Binding Example\n", 191 | "int a = 5" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 12, 197 | "id": "5e0542f7", 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "5\n", 205 | "6\n", 206 | "7\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "# Special Syntax\n", 212 | "a = 5; b = 6; c = 7\n", 213 | "\n", 214 | "print(a)\n", 215 | "print(b)\n", 216 | "print(c)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 13, 222 | "id": "a2161680", 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "5\n", 230 | "6\n", 231 | "7\n" 232 | ] 233 | } 234 | ], 235 | "source": [ 236 | "a, b, c = 5, 6, 7\n", 237 | "\n", 238 | "print(a)\n", 239 | "print(b)\n", 240 | "print(c)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 2, 246 | "id": "7100ea88", 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "6 6 6\n" 254 | ] 255 | } 256 | ], 257 | "source": [ 258 | "a = b = c = 6\n", 259 | "\n", 260 | "print(a, b, c)" 261 | ] 262 | } 263 | ], 264 | "metadata": { 265 | "kernelspec": { 266 | "display_name": "Python 3 (ipykernel)", 267 | "language": "python", 268 | "name": "python3" 269 | }, 270 | "language_info": { 271 | "codemirror_mode": { 272 | "name": "ipython", 273 | "version": 3 274 | }, 275 | "file_extension": ".py", 276 | "mimetype": "text/x-python", 277 | "name": "python", 278 | "nbconvert_exporter": "python", 279 | "pygments_lexer": "ipython3", 280 | "version": "3.10.9" 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 5 285 | } 286 | -------------------------------------------------------------------------------- /100 Days Python/Day 02 - data-types.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b5c05161", 6 | "metadata": {}, 7 | "source": [ 8 | "# Python Data Types Categories\n", 9 | "\n", 10 | "## Basic Types:\n", 11 | "- **`Int`**: Whole nums (1, 2, 3).\n", 12 | "- **`Float`**: Decimals (3.14, 2.5).\n", 13 | "- **`Complex`**: Real+Imag (1 + 2j).\n", 14 | "- **`Bool`**: True/False.\n", 15 | "- **`Str`**: Text (\"hello\").\n", 16 | "\n", 17 | "## Container Types:\n", 18 | "- **`List`**: Ordered items.\n", 19 | " ```python\n", 20 | " [1, 2, 3]\n", 21 | " ```\n", 22 | "- **`Tuple`**: Ordered, immutable.\n", 23 | " ```python\n", 24 | " (1, 2, 3)\n", 25 | " ```\n", 26 | "- **`Set`**: Unordered, unique.\n", 27 | " ```python\n", 28 | " {1, 2, 3}\n", 29 | " ```\n", 30 | "- **`Dict`**: Key-value pairs.\n", 31 | " ```python\n", 32 | " {'k1': 'v1', 'k2': 'v2'}\n", 33 | " ```\n", 34 | "\n", 35 | "## User-Defined Types:\n", 36 | "- **`Class`**: Obj blueprint." 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "id": "e70bd04d", 42 | "metadata": {}, 43 | "source": [ 44 | "### int" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 1, 50 | "id": "376d9e5c", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "4\n", 58 | "1e+308\n", 59 | "inf\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "# Numbers\n", 65 | "print(4)\n", 66 | "\n", 67 | "# Large Numbers\n", 68 | "print(1e308) # 1 * 10^308\n", 69 | "print(1e309) # 'inf' (exceeds float limit)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "3ec98d1c", 75 | "metadata": {}, 76 | "source": [ 77 | "### float" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 1, 83 | "id": "ad065a20", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "4.5\n", 91 | "1.8e+307\n", 92 | "inf\n", 93 | "inf\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "# Floating-Point Numbers\n", 99 | "print(4.5) # Standard\n", 100 | "\n", 101 | "# Large Floating-Point Numbers\n", 102 | "print(1.8e307) # Large\n", 103 | "print(1.8e308) # Larger\n", 104 | "print(1.9e308) # Too large (inf)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "id": "a945d9a3", 110 | "metadata": {}, 111 | "source": [ 112 | "### bool" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 3, 118 | "id": "fdcbf9a6", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "True\n", 126 | "False\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "# Boolean Values\n", 132 | "print(True)\n", 133 | "print(False)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "id": "43e07174", 139 | "metadata": {}, 140 | "source": [ 141 | "### complex" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 4, 147 | "id": "77161653", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "(4+5j)\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "# Complex Number\n", 160 | "print(4 + 5j)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "id": "97d32c57", 166 | "metadata": {}, 167 | "source": [ 168 | "### str" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 5, 174 | "id": "dbace775", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "Kolkata\n", 182 | "Kolkata\n", 183 | "Kolkata\n", 184 | "Kolkata\n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "# Strings with Different Quotation Styles\n", 190 | "print('Kolkata') # single quotes\n", 191 | "print(\"Kolkata\") # double quotes\n", 192 | "print('''Kolkata''') # triple single quotes\n", 193 | "print(\"\"\"Kolkata\"\"\") # triple double quotes" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "id": "945a1af7", 199 | "metadata": {}, 200 | "source": [ 201 | "### list" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 6, 207 | "id": "1da611b9", 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "[1, 2, 3, 4, 5]\n" 215 | ] 216 | } 217 | ], 218 | "source": [ 219 | "# List\n", 220 | "print([1, 2, 3, 4, 5])" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "id": "d00854f2", 226 | "metadata": {}, 227 | "source": [ 228 | "### tuple" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 7, 234 | "id": "baac95c5", 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "(1, 2, 3, 4, 5)\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "# Tuple\n", 247 | "print((1, 2, 3, 4, 5))" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "id": "75812024", 253 | "metadata": {}, 254 | "source": [ 255 | "### set" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 8, 261 | "id": "4f81e9b0", 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "{1, 2, 3, 4, 5}\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "# Set\n", 274 | "print({1, 2, 3, 4, 5})" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "id": "5d612e07", 280 | "metadata": {}, 281 | "source": [ 282 | "### dict" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 9, 288 | "id": "03206dfb", 289 | "metadata": {}, 290 | "outputs": [ 291 | { 292 | "name": "stdout", 293 | "output_type": "stream", 294 | "text": [ 295 | "{'Name': 'Saurabh', 'Age': 22, 'Gender': 'Male'}\n" 296 | ] 297 | } 298 | ], 299 | "source": [ 300 | "# Dictionary\n", 301 | "print({\"Name\": \"Saurabh\", \"Age\": 22, \"Gender\": \"Male\"})" 302 | ] 303 | } 304 | ], 305 | "metadata": { 306 | "kernelspec": { 307 | "display_name": "Python 3 (ipykernel)", 308 | "language": "python", 309 | "name": "python3" 310 | }, 311 | "language_info": { 312 | "codemirror_mode": { 313 | "name": "ipython", 314 | "version": 3 315 | }, 316 | "file_extension": ".py", 317 | "mimetype": "text/x-python", 318 | "name": "python", 319 | "nbconvert_exporter": "python", 320 | "pygments_lexer": "ipython3", 321 | "version": "3.10.9" 322 | } 323 | }, 324 | "nbformat": 4, 325 | "nbformat_minor": 5 326 | } 327 | -------------------------------------------------------------------------------- /100 Days Python/Day 28 - threading - multi-processing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "86cfe4c2", 6 | "metadata": {}, 7 | "source": [ 8 | "## What is Threading?" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "61b5670a", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "+-------------------------------------+ +----------------------------------+\n", 19 | "| Single-Threaded Process | | Multi-Threaded Process |\n", 20 | "|-------------------------------------| |----------------------------------|\n", 21 | "| +------------+ | | +------------+ +------------+ |\n", 22 | "| | Thread 1 | | | | Thread 1 | | Thread 2 | |\n", 23 | "| +------------+ | | +------------+ +------------+ |\n", 24 | "| | Task 1 | | | | Task A | | Task C | |\n", 25 | "| | Task 2 | | | | Task B | | Task D | |\n", 26 | "| | Task 3 | | | +------------+ +------------+ |\n", 27 | "| +------------+ | | +------------+ +------------+ |\n", 28 | "+-------------------------------------+ | | Thread 3 | | Thread 4 | |\n", 29 | " | +------------+ +------------+ |\n", 30 | " | | Task E | | Task G | |\n", 31 | " | | Task F | | Task H | |\n", 32 | " | +------------+ +------------+ |\n", 33 | " +----------------------------------+" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "32ba0e35", 39 | "metadata": {}, 40 | "source": [ 41 | "**Single Thread:** Default; main thread executes code sequentially.\n", 42 | "\n", 43 | "**Multithreading:** Needed for concurrent/simultaneous code execution." 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "id": "c91b3f78", 49 | "metadata": {}, 50 | "source": [ 51 | "## What is a thread?" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "id": "c862c8fe", 57 | "metadata": {}, 58 | "source": [ 59 | "- Threads are basic CPU units.\n", 60 | "- Single process contains multiple threads.\n", 61 | "- Threads share code, data, files.\n", 62 | "- Each thread has its own registers, separate stack." 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "id": "44441268", 68 | "metadata": {}, 69 | "source": [ 70 | "## Why or When Threading?" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "id": "6022b4f7", 76 | "metadata": {}, 77 | "source": [ 78 | "**App Screen Persistence:**\n", 79 | "\n", 80 | "***Static Image Display:***\n", 81 | "```python\n", 82 | "# Main Thread\n", 83 | "while(True):\n", 84 | " displayScreen()\n", 85 | "```\n", 86 | "Shows fleeting images; appears like the app is running but isn't.\n", 87 | "\n", 88 | "***Heavy Operation + Display:***\n", 89 | "```python\n", 90 | "# Main Thread\n", 91 | "while(True):\n", 92 | " # Heavy Operation\n", 93 | " displayScreen()\n", 94 | "```\n", 95 | "Results in flicker; heavy op delays screen display.\n", 96 | "\n", 97 | "***Network Call + Display:***\n", 98 | "```python\n", 99 | "# Main Thread\n", 100 | "while(True):\n", 101 | " Image = request(ImageUrl)\n", 102 | " displayScreen()\n", 103 | "```\n", 104 | "Causes screen to pop up based on network delay.\n", 105 | "\n", 106 | "***Solution: Multithreading***\n", 107 | "```python\n", 108 | "# Main Thread\n", 109 | "Image = None\n", 110 | "\n", 111 | "def startAnotherThread():\n", 112 | " while(True):\n", 113 | " Image = request(ImageUrl)\n", 114 | "\n", 115 | "while(True):\n", 116 | " displayScreen(Image)\n", 117 | "```\n", 118 | "`startAnotherThread()` fetches images concurrently; `displayScreen()` shows available images." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 3, 124 | "id": "4e88695f", 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "# another example ---> multithreading in servers is used to handle multiple requests. \n", 129 | "# mechanism involves diff threads for each request" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "id": "38690eb6", 135 | "metadata": {}, 136 | "source": [ 137 | "## How threads are handled by OS?" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "6a0edfe3", 143 | "metadata": {}, 144 | "source": [ 145 | "**Threads Running Concurrently** a **\"False Statement\"**. Threads are not truly running concurrently.\n", 146 | "\n", 147 | "**Reality:** Limited by single-core CPUs; **Concurrent** ≠ **Parallel**" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "id": "3dea0f2c", 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | " Web Server Thread\n", 158 | "+-----------------------------------------+ ^ +--------------------------------+\n", 159 | "| +-------------------------+ | | t3 ----> | Running | Sleeping | Running |\n", 160 | "| | Request Handler | | | +--------------------------------+\n", 161 | "| +-------------------------+ | | +--------------------------------+\n", 162 | "| +---------+ +---------+ +---------+ | | t2 ----> | Sleeping | Running | Sleeping |\n", 163 | "| | Client1 | | Client2 | | Client3 | | | +--------------------------------+\n", 164 | "| +---------+ +---------+ +---------+ | | +--------------------------------+\n", 165 | "+-----------------------------------------+ | t1 ----> | Running | Running | Sleeping |\n", 166 | " | +--------------------------------+ \n", 167 | " +-----------------------------------------------> Time" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "id": "b7223ab7", 173 | "metadata": {}, 174 | "source": [ 175 | "3 threads.\n", 176 | "\n", 177 | "CPU switches between them.\n", 178 | "\n", 179 | "Only 1 thread executes at a time.\n", 180 | "\n", 181 | "Effect: Concurrency." 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "id": "f1c5344c", 187 | "metadata": {}, 188 | "source": [ 189 | "## Implementation" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 6, 195 | "id": "0a0327fa", 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "Sleeping...0\n", 203 | "Sleeping...1\n", 204 | "Sleeping...2\n", 205 | "Sleeping...3\n", 206 | "Sleeping...4\n", 207 | "Sleeping...5\n", 208 | "Sleeping...6\n", 209 | "Sleeping...7\n", 210 | "Sleeping...8\n", 211 | "Sleeping...9\n", 212 | "Woke up...8Woke up...7\n", 213 | "Woke up...5\n", 214 | "Woke up...4\n", 215 | "Woke up...2\n", 216 | "Woke up...1\n", 217 | "Woke up...9\n", 218 | "Woke up...3\n", 219 | "Woke up...6\n", 220 | "Woke up...0\n", 221 | "\n", 222 | "Main Thread Duration: 1.0146267414093018 sec\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "from time import sleep, time\n", 228 | "import threading\n", 229 | "\n", 230 | "start = time()\n", 231 | "\n", 232 | "def task(id):\n", 233 | " print(f\"Sleeping...{id}\")\n", 234 | " sleep(1)\n", 235 | " print(f\"Woke up...{id}\")\n", 236 | "\n", 237 | "threads = [threading.Thread(target=task, args=[i]) for i in range(10)]\n", 238 | "for t in threads:\n", 239 | " t.start()\n", 240 | "\n", 241 | "for t in threads:\n", 242 | " t.join()\n", 243 | "\n", 244 | "end = time()\n", 245 | "print(f\"Main Thread Duration: {end - start} sec\")" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "id": "9a512454", 251 | "metadata": {}, 252 | "source": [ 253 | "## Thread Synchronization" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 10, 259 | "id": "6f0b7c97", 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "name": "stdout", 264 | "output_type": "stream", 265 | "text": [ 266 | "200\n" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "import threading\n", 272 | "\n", 273 | "balance = 200\n", 274 | "lock = threading.Lock()\n", 275 | "\n", 276 | "def deposit(amount, times, lock):\n", 277 | " global balance\n", 278 | " for _ in range(times):\n", 279 | " lock.acquire()\n", 280 | " balance += amount\n", 281 | " lock.release()\n", 282 | "\n", 283 | "def withdraw(amount, times, lock):\n", 284 | " global balance\n", 285 | " for _ in range(times):\n", 286 | " lock.acquire()\n", 287 | " balance -= amount\n", 288 | " lock.release()\n", 289 | "\n", 290 | "deposit_thread = threading.Thread(target=deposit, args=[1, 100000, lock])\n", 291 | "withdraw_thread = threading.Thread(target=withdraw, args=[1, 100000, lock])\n", 292 | "\n", 293 | "deposit_thread.start()\n", 294 | "withdraw_thread.start()\n", 295 | "deposit_thread.join()\n", 296 | "withdraw_thread.join()\n", 297 | "\n", 298 | "print(balance)" 299 | ] 300 | } 301 | ], 302 | "metadata": { 303 | "kernelspec": { 304 | "display_name": "Python 3 (ipykernel)", 305 | "language": "python", 306 | "name": "python3" 307 | }, 308 | "language_info": { 309 | "codemirror_mode": { 310 | "name": "ipython", 311 | "version": 3 312 | }, 313 | "file_extension": ".py", 314 | "mimetype": "text/x-python", 315 | "name": "python", 316 | "nbconvert_exporter": "python", 317 | "pygments_lexer": "ipython3", 318 | "version": "3.10.9" 319 | } 320 | }, 321 | "nbformat": 4, 322 | "nbformat_minor": 5 323 | } 324 | -------------------------------------------------------------------------------- /100 Days Python/Day 25 - recursion - memoization.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "710ebbca", 6 | "metadata": {}, 7 | "source": [ 8 | "# Recursion\n", 9 | "\n", 10 | "*Function calls itself*\n", 11 | "\n", 12 | "### Advantages:\n", 13 | "- No loops needed.\n", 14 | "- Solves problems without iteration.\n", 15 | "\n", 16 | "*\"To understand recursion you must understand recursion\"*" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "f29e9fef", 22 | "metadata": {}, 23 | "source": [ 24 | "## Iterative Vs Recursive" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "8b443009", 30 | "metadata": {}, 31 | "source": [ 32 | "### a*b" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "id": "c0bdc028", 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "12\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "# iterative method\n", 51 | "def multiply(a, b):\n", 52 | " result = 0\n", 53 | " for i in range(b):\n", 54 | " result += a\n", 55 | " print(result)\n", 56 | "multiply(3, 4)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "id": "4a409aad", 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "# Recursive Method\n", 67 | "\n", 68 | "# 1. Base Case: Define stopping condition.\n", 69 | "\n", 70 | "# 2. Decompose: Break main problem into smaller subproblems until base case is reached.\n", 71 | "\n", 72 | "def mul(a, b):\n", 73 | " if b == 1:\n", 74 | " return a\n", 75 | " else:\n", 76 | " return a + mul(a, b-1)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "id": "ab71aa76", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "12\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "print(mul(3, 4))" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 5, 100 | "id": "33dd7306", 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "120\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "# Factorial via Recursion\n", 113 | "def fact(number):\n", 114 | " if number == 1:\n", 115 | " return 1\n", 116 | " else:\n", 117 | " return number * fact(number-1)\n", 118 | "print(fact(5))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 6, 124 | "id": "522fa700", 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "# Palindrome\n", 129 | "def palin(text):\n", 130 | " if len(text) <= 1:\n", 131 | " print(\"palindrome\")\n", 132 | " else:\n", 133 | " if text[0] == text[-1]:\n", 134 | " palin(text[1:-1])\n", 135 | " else:\n", 136 | " print(\"not a palindrome\")" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 7, 142 | "id": "bc999b41", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "palindrome\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "palin(\"madam\")" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "id": "187dc9d2", 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "palindrome\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "palin(\"malayalam\")" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 9, 178 | "id": "5851a44e", 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "not a palindrome\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "palin(\"python\")" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 10, 196 | "id": "44ed4f2b", 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "palindrome\n" 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "palin(\"abba\")" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 11, 214 | "id": "c579001f", 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "name": "stdout", 219 | "output_type": "stream", 220 | "text": [ 221 | "233\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "# The Rabbit Problem: Fibonacci Number\n", 227 | "\n", 228 | "# Scenario ---> 2 newborn rabbits: 1 male + 1 female monthly.\n", 229 | "# Reproduce after 1 month, immortality.\n", 230 | "\n", 231 | "def fib(m):\n", 232 | " if m == 0 or m == 1:\n", 233 | " return 1\n", 234 | " else:\n", 235 | " return fib(m-1) + fib(m-2)\n", 236 | "print(fib(12)) # T = O(2^n)\n", 237 | "\n", 238 | "# Key Concepts:\n", 239 | "# Fibonacci Sequence\n", 240 | "# Reproduction Rate\n", 241 | "# Population Growth" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 12, 247 | "id": "046e5eb5", 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "233\n", 255 | "0.0\n" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "import time\n", 261 | "start = time.time()\n", 262 | "print(fib(12))\n", 263 | "print(time.time() - start)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 13, 269 | "id": "8df0062c", 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "75025\n", 277 | "0.22108960151672363\n" 278 | ] 279 | } 280 | ], 281 | "source": [ 282 | "print(fib(24))\n", 283 | "print(time.time() - start)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 14, 289 | "id": "d629a33d", 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "24157817\n", 297 | "6.103978157043457\n" 298 | ] 299 | } 300 | ], 301 | "source": [ 302 | "print(fib(36))\n", 303 | "print(time.time() - start)" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "id": "0dbc213c", 309 | "metadata": {}, 310 | "source": [ 311 | "# Memoization\n", 312 | "\n", 313 | "Memoization refers to remembering method call results based on inputs.\n", 314 | "\n", 315 | "- Returns cached results, avoiding recomputation.\n", 316 | "- Speeds up computations; stores previous results.\n", 317 | "- Used in dynamic programming for recursive solutions.\n", 318 | "- Reduces time complexity; avoids redundant calculations.\n", 319 | "- Optimizes recursive algorithms by reusing results." 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 16, 325 | "id": "9c673f9e", 326 | "metadata": {}, 327 | "outputs": [ 328 | { 329 | "name": "stdout", 330 | "output_type": "stream", 331 | "text": [ 332 | "7778742049\n" 333 | ] 334 | } 335 | ], 336 | "source": [ 337 | "def memo(m, d):\n", 338 | " if m in d:\n", 339 | " return d[m]\n", 340 | " else:\n", 341 | " d[m] = memo(m-1, d) + memo(m-2, d)\n", 342 | " return d[m]\n", 343 | "d = {0:1, 1:1}\n", 344 | "print(memo(48, d))" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 17, 350 | "id": "0c4a17bd", 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "name": "stdout", 355 | "output_type": "stream", 356 | "text": [ 357 | "7778742049\n", 358 | "6.157997369766235\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "print(memo(48, d))\n", 364 | "print(time.time() - start)" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 18, 370 | "id": "5b304aee", 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "225591516161936330872512695036072072046011324913758190588638866418474627738686883405015987052796968498626\n", 378 | "6.17300009727478\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "print(memo(500, d))\n", 384 | "print(time.time() - start)" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 19, 390 | "id": "d2da5e7b", 391 | "metadata": {}, 392 | "outputs": [ 393 | { 394 | "name": "stdout", 395 | "output_type": "stream", 396 | "text": [ 397 | "70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501\n", 398 | "6.189241170883179\n" 399 | ] 400 | } 401 | ], 402 | "source": [ 403 | "print(memo(1000, d))\n", 404 | "print(time.time() - start)" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 20, 410 | "id": "c48ec6d5", 411 | "metadata": {}, 412 | "outputs": [], 413 | "source": [ 414 | "print(d) # Dict in memory, execution time reduced" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": 21, 420 | "id": "d56b927c", 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "name": "stdout", 425 | "output_type": "stream", 426 | "text": [ 427 | "[[], ['1'], ['2'], ['1', '2'], ['3'], ['1', '3'], ['2', '3'], ['1', '2', '3']]\n", 428 | "8\n" 429 | ] 430 | } 431 | ], 432 | "source": [ 433 | "# Recursive PowerSet Function in Python\n", 434 | "\n", 435 | "# PowerSet: Given set S, return power set P(S) (all subsets of S).\n", 436 | "\n", 437 | "# Input: String\n", 438 | "# Output: Array of Strings (power set)\n", 439 | "\n", 440 | "# Example: S = \"123\", P(S) = ['', '1', '2', '3', '12', '13', '23', '123']\n", 441 | "\n", 442 | "def powerset1(xs):\n", 443 | " res = [[]]\n", 444 | " if len(xs) <= 0:\n", 445 | " return \"Please Enter a parameter\"\n", 446 | " if len(xs) == 1:\n", 447 | " res.append([xs[0]])\n", 448 | " return res\n", 449 | " else:\n", 450 | " z = []\n", 451 | " for i in powerset1(xs[1:]):\n", 452 | " z.append(i)\n", 453 | " z.append([xs[0]] + i) \n", 454 | " return z\n", 455 | "\n", 456 | "final = powerset1('123')\n", 457 | "print(final)\n", 458 | "print(len(final))" 459 | ] 460 | } 461 | ], 462 | "metadata": { 463 | "kernelspec": { 464 | "display_name": "Python 3 (ipykernel)", 465 | "language": "python", 466 | "name": "python3" 467 | }, 468 | "language_info": { 469 | "codemirror_mode": { 470 | "name": "ipython", 471 | "version": 3 472 | }, 473 | "file_extension": ".py", 474 | "mimetype": "text/x-python", 475 | "name": "python", 476 | "nbconvert_exporter": "python", 477 | "pygments_lexer": "ipython3", 478 | "version": "3.10.9" 479 | } 480 | }, 481 | "nbformat": 4, 482 | "nbformat_minor": 5 483 | } 484 | -------------------------------------------------------------------------------- /100 Days Python/Day 13 - for-loop.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "bea9fe53", 6 | "metadata": {}, 7 | "source": [ 8 | "# For Loop" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "06304a66", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# C, C++, Java Loop Syntax\n", 19 | "for(i=0; i<10; i++) {\n", 20 | " // code\n", 21 | "}" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "id": "c3272c71", 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "range(1, 11)" 34 | ] 35 | }, 36 | "execution_count": 1, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "# Range Function\n", 43 | "range(1, 11)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "id": "a1382dd4", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" 56 | ] 57 | }, 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "list(range(1, 11))" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 3, 70 | "id": "ea9f81c5", 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "[0, 1, 2, 3, 4]" 77 | ] 78 | }, 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "list(range(5))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 4, 91 | "id": "33ab56a3", 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]" 98 | ] 99 | }, 100 | "execution_count": 4, 101 | "metadata": {}, 102 | "output_type": "execute_result" 103 | } 104 | ], 105 | "source": [ 106 | "list(range(15))" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 5, 112 | "id": "c37dfb9c", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "[1, 3, 5, 7, 9]" 119 | ] 120 | }, 121 | "execution_count": 5, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "list(range(1, 11, 2))" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 6, 133 | "id": "5d0f75af", 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "[1, 4, 7, 10]" 140 | ] 141 | }, 142 | "execution_count": 6, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "list(range(1, 11, 3))" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 7, 154 | "id": "c1c4e204", 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]" 161 | ] 162 | }, 163 | "execution_count": 7, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "list(range(10, 0, -1))" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 8, 175 | "id": "2e10ac3b", 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "'Kolkata'" 182 | ] 183 | }, 184 | "execution_count": 8, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "# Sequence\n", 191 | "# String\n", 192 | "\"Kolkata\"" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 9, 198 | "id": "f3e3382c", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "['Kolkata', 'Delhi', 'Mumbai']" 205 | ] 206 | }, 207 | "execution_count": 9, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "[\"Kolkata\", \"Delhi\", \"Mumbai\"]" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 10, 219 | "id": "58c76f3f", 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "data": { 224 | "text/plain": [ 225 | "('Kolkata', 'Delhi', 'Mumbai')" 226 | ] 227 | }, 228 | "execution_count": 10, 229 | "metadata": {}, 230 | "output_type": "execute_result" 231 | } 232 | ], 233 | "source": [ 234 | "(\"Kolkata\", \"Delhi\", \"Mumbai\")" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 11, 240 | "id": "cdefbc86", 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "{'Delhi', 'Kolkata', 'Mumbai'}" 247 | ] 248 | }, 249 | "execution_count": 11, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "{\"Kolkata\", \"Delhi\", \"Mumbai\"}" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 12, 261 | "id": "a07fae55", 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "1\n", 269 | "2\n", 270 | "3\n", 271 | "4\n", 272 | "5\n", 273 | "6\n", 274 | "7\n", 275 | "8\n", 276 | "9\n", 277 | "10\n" 278 | ] 279 | } 280 | ], 281 | "source": [ 282 | "for i in range(1, 11):\n", 283 | " print(i)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 13, 289 | "id": "f35ccedd", 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "1\n", 297 | "3\n", 298 | "5\n", 299 | "7\n", 300 | "9\n" 301 | ] 302 | } 303 | ], 304 | "source": [ 305 | "for i in range(1, 11, 2):\n", 306 | " print(i)" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 14, 312 | "id": "c9ac27bb", 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "10\n", 320 | "9\n", 321 | "8\n", 322 | "7\n", 323 | "6\n", 324 | "5\n", 325 | "4\n", 326 | "3\n", 327 | "2\n", 328 | "1\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "for i in range(10, 0, -1):\n", 334 | " print(i)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 15, 340 | "id": "d5bfef06", 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "name": "stdout", 345 | "output_type": "stream", 346 | "text": [ 347 | "K\n", 348 | "o\n", 349 | "l\n", 350 | "k\n", 351 | "a\n", 352 | "t\n", 353 | "a\n" 354 | ] 355 | } 356 | ], 357 | "source": [ 358 | "for i in \"Kolkata\":\n", 359 | " print(i)" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 16, 365 | "id": "4747dd56", 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "name": "stdout", 370 | "output_type": "stream", 371 | "text": [ 372 | "1\n", 373 | "2\n", 374 | "3\n", 375 | "5\n" 376 | ] 377 | } 378 | ], 379 | "source": [ 380 | "for i in [1, 2, 3, 5]:\n", 381 | " print(i)" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 17, 387 | "id": "be9f95fd", 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "name": "stdout", 392 | "output_type": "stream", 393 | "text": [ 394 | "1\n", 395 | "2\n", 396 | "3\n", 397 | "5\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "for i in (1, 2, 3, 5):\n", 403 | " print(i)" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 18, 409 | "id": "4d346e19", 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "name": "stdout", 414 | "output_type": "stream", 415 | "text": [ 416 | "1\n", 417 | "2\n", 418 | "3\n", 419 | "5\n" 420 | ] 421 | } 422 | ], 423 | "source": [ 424 | "for i in {1, 2, 3, 5}:\n", 425 | " print(i)" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 1, 431 | "id": "8b9cf0f5", 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "name": "stdout", 436 | "output_type": "stream", 437 | "text": [ 438 | "10 10000\n", 439 | "9 9090.90909090909\n", 440 | "8 8264.462809917353\n", 441 | "7 7513.148009015775\n", 442 | "6 6830.134553650703\n", 443 | "5 6209.213230591548\n", 444 | "4 5644.739300537771\n", 445 | "3 5131.5811823070635\n", 446 | "2 4665.07380209733\n", 447 | "1 4240.976183724845\n" 448 | ] 449 | } 450 | ], 451 | "source": [ 452 | "# 1. Population Growth Calculation\n", 453 | "\n", 454 | "# Initial Population: 10000\n", 455 | "# Growth Rate: 10% annually\n", 456 | "# Duration: 10 years\n", 457 | "\n", 458 | "curr_pop = 10000\n", 459 | "for i in range(10, 0, -1):\n", 460 | " print(i, curr_pop)\n", 461 | " curr_pop = curr_pop / 1.1" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 4, 467 | "id": "694f370b", 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "name": "stdout", 472 | "output_type": "stream", 473 | "text": [ 474 | "enter n: 2\n", 475 | "2.0\n" 476 | ] 477 | } 478 | ], 479 | "source": [ 480 | "# 2. Sequence Sum\n", 481 | "\n", 482 | "# Formula: 1/1! + 2/2! + 3/3! + ...\n", 483 | "\n", 484 | "n = int(input('enter n: '))\n", 485 | "result = 0\n", 486 | "fact = 1\n", 487 | "for i in range(1, n+1):\n", 488 | " fact *= i\n", 489 | " result += i / fact\n", 490 | "print(result)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "markdown", 495 | "id": "6bc1c340", 496 | "metadata": {}, 497 | "source": [ 498 | "**For Loop** : **Known** iterations. \n", 499 | " ```python\n", 500 | " for item in iterable:\n", 501 | " # Code block\n", 502 | " ```\n", 503 | "*iterable*: list, tuple, string, etc.\n", 504 | "\n", 505 | "**While Loop**: **Unknown** iterations. \n", 506 | " ```python\n", 507 | " while condition:\n", 508 | " # Code block\n", 509 | " ```\n", 510 | "*condition*: boolean expression.\n", 511 | "\n", 512 | "**Key Differences** is **For Loop** iterates a fixed number of times; **While Loop** iterates until condition is False.\n", 513 | "\n", 514 | "**For Loop Usage** - Known iterations/sequence traversal; **While Loop Usage** - Uncertain iterations/condition-based." 515 | ] 516 | } 517 | ], 518 | "metadata": { 519 | "kernelspec": { 520 | "display_name": "Python 3 (ipykernel)", 521 | "language": "python", 522 | "name": "python3" 523 | }, 524 | "language_info": { 525 | "codemirror_mode": { 526 | "name": "ipython", 527 | "version": 3 528 | }, 529 | "file_extension": ".py", 530 | "mimetype": "text/x-python", 531 | "name": "python", 532 | "nbconvert_exporter": "python", 533 | "pygments_lexer": "ipython3", 534 | "version": "3.10.9" 535 | } 536 | }, 537 | "nbformat": 4, 538 | "nbformat_minor": 5 539 | } 540 | -------------------------------------------------------------------------------- /100 Days Python/Day 06 - user-input-type-conv.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "89e17e82", 6 | "metadata": {}, 7 | "source": [ 8 | "# User Input" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "c12cd1ee", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Enter Emailsaurabhsinghdhami@gmail.com\n" 22 | ] 23 | }, 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "'saurabhsinghdhami@gmail.com'" 28 | ] 29 | }, 30 | "execution_count": 1, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "# Static vs Dynamic\n", 37 | "input('Enter Email')" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "id": "5113d43a", 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Enter your age: 22\n" 51 | ] 52 | }, 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "'22'" 57 | ] 58 | }, 59 | "execution_count": 1, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "# Input from user\n", 66 | "age = input(\"Enter your age: \")" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 2, 72 | "id": "47e94919", 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "Enter first number: 56\n", 80 | "Enter second number: 76\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "first_num = int(input(\"Enter first number: \"))\n", 86 | "second_num = int(input(\"Enter second number: \"))" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 3, 92 | "id": "28621cac", 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "56\n", 100 | "76\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "print(first_num)\n", 106 | "print(second_num)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 4, 112 | "id": "c71841f8", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "5676\n" 120 | ] 121 | } 122 | ], 123 | "source": [ 124 | "result = first_num + second_num\n", 125 | "print(result)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 5, 131 | "id": "8c2442ed", 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "int" 138 | ] 139 | }, 140 | "execution_count": 5, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "# type() function\n", 147 | "type(4)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 6, 153 | "id": "037359c9", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "str" 160 | ] 161 | }, 162 | "execution_count": 6, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "type(first_num)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 7, 174 | "id": "dd35f771", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "'56'" 181 | ] 182 | }, 183 | "execution_count": 7, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "first_num" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 9, 195 | "id": "e665c10c", 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "data": { 200 | "text/plain": [ 201 | "9.5" 202 | ] 203 | }, 204 | "execution_count": 9, 205 | "metadata": {}, 206 | "output_type": "execute_result" 207 | } 208 | ], 209 | "source": [ 210 | "4 + 5.5" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 10, 216 | "id": "b6b5526a", 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "data": { 221 | "text/plain": [ 222 | "(11+7j)" 223 | ] 224 | }, 225 | "execution_count": 10, 226 | "metadata": {}, 227 | "output_type": "execute_result" 228 | } 229 | ], 230 | "source": [ 231 | "5 + 6+7j" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 11, 237 | "id": "7d79d094", 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "data": { 242 | "text/plain": [ 243 | "(9.5+5j)" 244 | ] 245 | }, 246 | "execution_count": 11, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "4.5 + 5+5j" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 12, 258 | "id": "e8ccd6eb", 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "'5676'" 265 | ] 266 | }, 267 | "execution_count": 12, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "first_num + second_num" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "id": "700154c9", 279 | "metadata": {}, 280 | "source": [ 281 | "# Type Conversion\n", 282 | "\n", 283 | "**Implicit**: Auto type conversion by language. \n", 284 | "**Explicit**: Manual type conversion using functions/methods." 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 2, 290 | "id": "65eaa4ad", 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "10.6\n", 298 | " \n" 299 | ] 300 | }, 301 | { 302 | "ename": "TypeError", 303 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 304 | "output_type": "error", 305 | "traceback": [ 306 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 307 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 308 | "Cell \u001b[1;32mIn[2], line 5\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;241m5\u001b[39m\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m5.6\u001b[39m)\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mtype\u001b[39m(\u001b[38;5;241m5\u001b[39m),\u001b[38;5;28mtype\u001b[39m(\u001b[38;5;241m5.6\u001b[39m))\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m4\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m)\n", 309 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "# Implicit Vs Explicit\n", 315 | "print(5 + 5.6) # Implicit: int + float → float\n", 316 | "print(type(5), type(5.6)) # Show types: int, float\n", 317 | "print(4 + '4') # Error: no implicit int + str" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 3, 323 | "id": "7cbdb795", 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "data": { 328 | "text/plain": [ 329 | "4.0" 330 | ] 331 | }, 332 | "execution_count": 3, 333 | "metadata": {}, 334 | "output_type": "execute_result" 335 | } 336 | ], 337 | "source": [ 338 | "# Explicit\n", 339 | "\n", 340 | "# str to int\n", 341 | "int('4')\n", 342 | "\n", 343 | "# int to str\n", 344 | "str(5)\n", 345 | "\n", 346 | "# float\n", 347 | "float(4)" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 13, 353 | "id": "2328e35c", 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "data": { 358 | "text/plain": [ 359 | "45" 360 | ] 361 | }, 362 | "execution_count": 13, 363 | "metadata": {}, 364 | "output_type": "execute_result" 365 | } 366 | ], 367 | "source": [ 368 | "# int\n", 369 | "int(45)" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 14, 375 | "id": "644518ba", 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "data": { 380 | "text/plain": [ 381 | "4.0" 382 | ] 383 | }, 384 | "execution_count": 14, 385 | "metadata": {}, 386 | "output_type": "execute_result" 387 | } 388 | ], 389 | "source": [ 390 | "float(4)" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 15, 396 | "id": "5cfee174", 397 | "metadata": {}, 398 | "outputs": [ 399 | { 400 | "data": { 401 | "text/plain": [ 402 | "'5'" 403 | ] 404 | }, 405 | "execution_count": 15, 406 | "metadata": {}, 407 | "output_type": "execute_result" 408 | } 409 | ], 410 | "source": [ 411 | "str(5)" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 16, 417 | "id": "526fb578", 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "data": { 422 | "text/plain": [ 423 | "True" 424 | ] 425 | }, 426 | "execution_count": 16, 427 | "metadata": {}, 428 | "output_type": "execute_result" 429 | } 430 | ], 431 | "source": [ 432 | "bool(1)" 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": 17, 438 | "id": "158097c8", 439 | "metadata": {}, 440 | "outputs": [ 441 | { 442 | "data": { 443 | "text/plain": [ 444 | "(4+0j)" 445 | ] 446 | }, 447 | "execution_count": 17, 448 | "metadata": {}, 449 | "output_type": "execute_result" 450 | } 451 | ], 452 | "source": [ 453 | "complex(4)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 18, 459 | "id": "22df6315", 460 | "metadata": {}, 461 | "outputs": [ 462 | { 463 | "data": { 464 | "text/plain": [ 465 | "['H', 'e', 'l', 'l', 'o']" 466 | ] 467 | }, 468 | "execution_count": 18, 469 | "metadata": {}, 470 | "output_type": "execute_result" 471 | } 472 | ], 473 | "source": [ 474 | "list(\"Hello\")" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 19, 480 | "id": "47a3624a", 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "data": { 485 | "text/plain": [ 486 | "('H', 'e', 'l', 'l', 'o')" 487 | ] 488 | }, 489 | "execution_count": 19, 490 | "metadata": {}, 491 | "output_type": "execute_result" 492 | } 493 | ], 494 | "source": [ 495 | "tuple(\"Hello\")" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 20, 501 | "id": "2082a866", 502 | "metadata": {}, 503 | "outputs": [ 504 | { 505 | "data": { 506 | "text/plain": [ 507 | "{'H', 'e', 'l', 'o'}" 508 | ] 509 | }, 510 | "execution_count": 20, 511 | "metadata": {}, 512 | "output_type": "execute_result" 513 | } 514 | ], 515 | "source": [ 516 | "set(\"Hello\")" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 21, 522 | "id": "22a5d8d3", 523 | "metadata": {}, 524 | "outputs": [], 525 | "source": [ 526 | "a = 4.5" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 22, 532 | "id": "d2d14c5b", 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "data": { 537 | "text/plain": [ 538 | "4" 539 | ] 540 | }, 541 | "execution_count": 22, 542 | "metadata": {}, 543 | "output_type": "execute_result" 544 | } 545 | ], 546 | "source": [ 547 | "int(a)" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": 23, 553 | "id": "62bb6e69", 554 | "metadata": {}, 555 | "outputs": [ 556 | { 557 | "data": { 558 | "text/plain": [ 559 | "4.5" 560 | ] 561 | }, 562 | "execution_count": 23, 563 | "metadata": {}, 564 | "output_type": "execute_result" 565 | } 566 | ], 567 | "source": [ 568 | "a" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 24, 574 | "id": "81522d32", 575 | "metadata": {}, 576 | "outputs": [ 577 | { 578 | "name": "stdout", 579 | "output_type": "stream", 580 | "text": [ 581 | "Enter first number: 56\n", 582 | "Enter second number: 76\n", 583 | "132\n" 584 | ] 585 | } 586 | ], 587 | "source": [ 588 | "first_num = input(\"Enter first number: \")\n", 589 | "second_num = input(\"Enter second number: \")\n", 590 | "result = int(first_num) + int(second_num)\n", 591 | "print(result)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": 25, 597 | "id": "dafcc103", 598 | "metadata": {}, 599 | "outputs": [ 600 | { 601 | "name": "stdout", 602 | "output_type": "stream", 603 | "text": [ 604 | "Enter first number: 56\n", 605 | "Enter second number: 76\n", 606 | "132\n" 607 | ] 608 | } 609 | ], 610 | "source": [ 611 | "first_num = int(input(\"Enter first number: \"))\n", 612 | "second_num = int(input(\"Enter second number: \"))\n", 613 | "result = first_num + second_num\n", 614 | "print(result)" 615 | ] 616 | } 617 | ], 618 | "metadata": { 619 | "kernelspec": { 620 | "display_name": "Python 3 (ipykernel)", 621 | "language": "python", 622 | "name": "python3" 623 | }, 624 | "language_info": { 625 | "codemirror_mode": { 626 | "name": "ipython", 627 | "version": 3 628 | }, 629 | "file_extension": ".py", 630 | "mimetype": "text/x-python", 631 | "name": "python", 632 | "nbconvert_exporter": "python", 633 | "pygments_lexer": "ipython3", 634 | "version": "3.10.9" 635 | } 636 | }, 637 | "nbformat": 4, 638 | "nbformat_minor": 5 639 | } 640 | -------------------------------------------------------------------------------- /100 Days Python/Day 16 - built-in-funcs.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6d9bc427", 6 | "metadata": {}, 7 | "source": [ 8 | "### 1. print" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "58ba37e7", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Hello World\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "print(\"Hello World\")" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "0d3c6613", 32 | "metadata": {}, 33 | "source": [ 34 | "### 2. input" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "id": "ae6cba18", 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "Enter your name: Saurabh Singh Dhami\n" 48 | ] 49 | }, 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "'Saurabh Singh Dhami'" 54 | ] 55 | }, 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "input(\"Enter your name: \")" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "id": "255ef20b", 68 | "metadata": {}, 69 | "source": [ 70 | "### 3. type" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 3, 76 | "id": "295d0783", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "int" 83 | ] 84 | }, 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "a = 3\n", 92 | "type(a)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 4, 98 | "id": "c0c50f50", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "float" 105 | ] 106 | }, 107 | "execution_count": 4, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "a = 3.5\n", 114 | "type(a)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "id": "52ef6f08", 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "bool" 127 | ] 128 | }, 129 | "execution_count": 5, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "a = True\n", 136 | "type(a)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "id": "29da395e", 142 | "metadata": {}, 143 | "source": [ 144 | "### 4. int etc." 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 6, 150 | "id": "c1635d27", 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "5" 157 | ] 158 | }, 159 | "execution_count": 6, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "int('5')\n", 166 | "# float\n", 167 | "# str\n", 168 | "# list\n", 169 | "# tuple" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "id": "92f83b27", 175 | "metadata": {}, 176 | "source": [ 177 | "### 5. abs" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 7, 183 | "id": "9702d8c8", 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "4" 190 | ] 191 | }, 192 | "execution_count": 7, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "abs(4)" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 8, 204 | "id": "6d1d7eca", 205 | "metadata": {}, 206 | "outputs": [ 207 | { 208 | "data": { 209 | "text/plain": [ 210 | "4" 211 | ] 212 | }, 213 | "execution_count": 8, 214 | "metadata": {}, 215 | "output_type": "execute_result" 216 | } 217 | ], 218 | "source": [ 219 | "abs(-4)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "id": "57cf8095", 225 | "metadata": {}, 226 | "source": [ 227 | "### 6. pow" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 9, 233 | "id": "e594d332", 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "8" 240 | ] 241 | }, 242 | "execution_count": 9, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "pow(2, 3)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 10, 254 | "id": "57ef3b6d", 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "data": { 259 | "text/plain": [ 260 | "0.125" 261 | ] 262 | }, 263 | "execution_count": 10, 264 | "metadata": {}, 265 | "output_type": "execute_result" 266 | } 267 | ], 268 | "source": [ 269 | "pow(2, -3)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "id": "6fc71a0e", 275 | "metadata": {}, 276 | "source": [ 277 | "### 7. min/max" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 11, 283 | "id": "f68962e4", 284 | "metadata": {}, 285 | "outputs": [ 286 | { 287 | "data": { 288 | "text/plain": [ 289 | "0" 290 | ] 291 | }, 292 | "execution_count": 11, 293 | "metadata": {}, 294 | "output_type": "execute_result" 295 | } 296 | ], 297 | "source": [ 298 | "min([2, 1, 3, 0])" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 12, 304 | "id": "9919e411", 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "data": { 309 | "text/plain": [ 310 | "3" 311 | ] 312 | }, 313 | "execution_count": 12, 314 | "metadata": {}, 315 | "output_type": "execute_result" 316 | } 317 | ], 318 | "source": [ 319 | "max([2, 1, 3, 0])" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 13, 325 | "id": "4632b6d8", 326 | "metadata": {}, 327 | "outputs": [ 328 | { 329 | "data": { 330 | "text/plain": [ 331 | "'a'" 332 | ] 333 | }, 334 | "execution_count": 13, 335 | "metadata": {}, 336 | "output_type": "execute_result" 337 | } 338 | ], 339 | "source": [ 340 | "min(\"kolkata\")" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 14, 346 | "id": "fc626280", 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "data": { 351 | "text/plain": [ 352 | "'t'" 353 | ] 354 | }, 355 | "execution_count": 14, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "max(\"kolkata\")" 362 | ] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "id": "dd97d328", 367 | "metadata": {}, 368 | "source": [ 369 | "### 8. round" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 15, 375 | "id": "7957e84b", 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "3.142857142857143\n" 383 | ] 384 | }, 385 | { 386 | "data": { 387 | "text/plain": [ 388 | "3" 389 | ] 390 | }, 391 | "execution_count": 15, 392 | "metadata": {}, 393 | "output_type": "execute_result" 394 | } 395 | ], 396 | "source": [ 397 | "c = 22/7\n", 398 | "print(c)\n", 399 | "round(c)" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 16, 405 | "id": "3e2b0265", 406 | "metadata": {}, 407 | "outputs": [ 408 | { 409 | "data": { 410 | "text/plain": [ 411 | "3.14" 412 | ] 413 | }, 414 | "execution_count": 16, 415 | "metadata": {}, 416 | "output_type": "execute_result" 417 | } 418 | ], 419 | "source": [ 420 | "round(c, 2)" 421 | ] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "id": "b216484b", 426 | "metadata": {}, 427 | "source": [ 428 | "### 9. divmod" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 17, 434 | "id": "57db959f", 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "data": { 439 | "text/plain": [ 440 | "(2, 1)" 441 | ] 442 | }, 443 | "execution_count": 17, 444 | "metadata": {}, 445 | "output_type": "execute_result" 446 | } 447 | ], 448 | "source": [ 449 | "divmod(5, 2)" 450 | ] 451 | }, 452 | { 453 | "cell_type": "markdown", 454 | "id": "6f17e0ba", 455 | "metadata": {}, 456 | "source": [ 457 | "### 10. bin/oct/hex" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 18, 463 | "id": "426927fa", 464 | "metadata": {}, 465 | "outputs": [ 466 | { 467 | "data": { 468 | "text/plain": [ 469 | "'0b100'" 470 | ] 471 | }, 472 | "execution_count": 18, 473 | "metadata": {}, 474 | "output_type": "execute_result" 475 | } 476 | ], 477 | "source": [ 478 | "bin(4)" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 19, 484 | "id": "7849482f", 485 | "metadata": {}, 486 | "outputs": [ 487 | { 488 | "data": { 489 | "text/plain": [ 490 | "'0o4'" 491 | ] 492 | }, 493 | "execution_count": 19, 494 | "metadata": {}, 495 | "output_type": "execute_result" 496 | } 497 | ], 498 | "source": [ 499 | "oct(4)" 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": 20, 505 | "id": "bd6a51a0", 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "data": { 510 | "text/plain": [ 511 | "'0x4'" 512 | ] 513 | }, 514 | "execution_count": 20, 515 | "metadata": {}, 516 | "output_type": "execute_result" 517 | } 518 | ], 519 | "source": [ 520 | "hex(4)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "markdown", 525 | "id": "03cab636", 526 | "metadata": {}, 527 | "source": [ 528 | "### 11. id" 529 | ] 530 | }, 531 | { 532 | "cell_type": "code", 533 | "execution_count": 21, 534 | "id": "f21dfbb5", 535 | "metadata": {}, 536 | "outputs": [ 537 | { 538 | "data": { 539 | "text/plain": [ 540 | "2390799640944" 541 | ] 542 | }, 543 | "execution_count": 21, 544 | "metadata": {}, 545 | "output_type": "execute_result" 546 | } 547 | ], 548 | "source": [ 549 | "a = 3\n", 550 | "id(a)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "id": "8286fe9e", 556 | "metadata": {}, 557 | "source": [ 558 | "### 12. ord" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": 22, 564 | "id": "7fbed814", 565 | "metadata": {}, 566 | "outputs": [ 567 | { 568 | "data": { 569 | "text/plain": [ 570 | "65" 571 | ] 572 | }, 573 | "execution_count": 22, 574 | "metadata": {}, 575 | "output_type": "execute_result" 576 | } 577 | ], 578 | "source": [ 579 | "ord('A')" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 23, 585 | "id": "b09cf58d", 586 | "metadata": {}, 587 | "outputs": [ 588 | { 589 | "data": { 590 | "text/plain": [ 591 | "97" 592 | ] 593 | }, 594 | "execution_count": 23, 595 | "metadata": {}, 596 | "output_type": "execute_result" 597 | } 598 | ], 599 | "source": [ 600 | "ord('a')" 601 | ] 602 | }, 603 | { 604 | "cell_type": "markdown", 605 | "id": "4eab12de", 606 | "metadata": {}, 607 | "source": [ 608 | "### 13. len" 609 | ] 610 | }, 611 | { 612 | "cell_type": "code", 613 | "execution_count": 24, 614 | "id": "2360ffbc", 615 | "metadata": {}, 616 | "outputs": [ 617 | { 618 | "data": { 619 | "text/plain": [ 620 | "7" 621 | ] 622 | }, 623 | "execution_count": 24, 624 | "metadata": {}, 625 | "output_type": "execute_result" 626 | } 627 | ], 628 | "source": [ 629 | "len('Kolkata')" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": 25, 635 | "id": "40cc235a", 636 | "metadata": {}, 637 | "outputs": [ 638 | { 639 | "data": { 640 | "text/plain": [ 641 | "3" 642 | ] 643 | }, 644 | "execution_count": 25, 645 | "metadata": {}, 646 | "output_type": "execute_result" 647 | } 648 | ], 649 | "source": [ 650 | "len([1, 2, 3])" 651 | ] 652 | }, 653 | { 654 | "cell_type": "markdown", 655 | "id": "529a7915", 656 | "metadata": {}, 657 | "source": [ 658 | "### 14. sum" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 26, 664 | "id": "d5d3f720", 665 | "metadata": {}, 666 | "outputs": [ 667 | { 668 | "data": { 669 | "text/plain": [ 670 | "15" 671 | ] 672 | }, 673 | "execution_count": 26, 674 | "metadata": {}, 675 | "output_type": "execute_result" 676 | } 677 | ], 678 | "source": [ 679 | "sum([1, 2, 3, 4, 5])" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": 27, 685 | "id": "f0337453", 686 | "metadata": {}, 687 | "outputs": [ 688 | { 689 | "data": { 690 | "text/plain": [ 691 | "15" 692 | ] 693 | }, 694 | "execution_count": 27, 695 | "metadata": {}, 696 | "output_type": "execute_result" 697 | } 698 | ], 699 | "source": [ 700 | "sum({1, 2, 3, 4, 5})" 701 | ] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "id": "58c6714a", 706 | "metadata": {}, 707 | "source": [ 708 | "### 15. help" 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": 28, 714 | "id": "9a2667e6", 715 | "metadata": {}, 716 | "outputs": [ 717 | { 718 | "name": "stdout", 719 | "output_type": "stream", 720 | "text": [ 721 | "Help on built-in function print in module builtins:\n", 722 | "\n", 723 | "print(...)\n", 724 | " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 725 | " \n", 726 | " Prints the values to a stream, or to sys.stdout by default.\n", 727 | " Optional keyword arguments:\n", 728 | " file: a file-like object (stream); defaults to the current sys.stdout.\n", 729 | " sep: string inserted between values, default a space.\n", 730 | " end: string appended after the last value, default a newline.\n", 731 | " flush: whether to forcibly flush the stream.\n", 732 | "\n" 733 | ] 734 | } 735 | ], 736 | "source": [ 737 | "help('print')" 738 | ] 739 | }, 740 | { 741 | "cell_type": "code", 742 | "execution_count": 29, 743 | "id": "39f038f0", 744 | "metadata": {}, 745 | "outputs": [ 746 | { 747 | "name": "stdout", 748 | "output_type": "stream", 749 | "text": [ 750 | "Help on built-in function sum in module builtins:\n", 751 | "\n", 752 | "sum(iterable, /, start=0)\n", 753 | " Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n", 754 | " \n", 755 | " When the iterable is empty, return the start value.\n", 756 | " This function is intended specifically for use with numeric values and may\n", 757 | " reject non-numeric types.\n", 758 | "\n" 759 | ] 760 | } 761 | ], 762 | "source": [ 763 | "help('sum')" 764 | ] 765 | } 766 | ], 767 | "metadata": { 768 | "kernelspec": { 769 | "display_name": "Python 3 (ipykernel)", 770 | "language": "python", 771 | "name": "python3" 772 | }, 773 | "language_info": { 774 | "codemirror_mode": { 775 | "name": "ipython", 776 | "version": 3 777 | }, 778 | "file_extension": ".py", 779 | "mimetype": "text/x-python", 780 | "name": "python", 781 | "nbconvert_exporter": "python", 782 | "pygments_lexer": "ipython3", 783 | "version": "3.10.9" 784 | } 785 | }, 786 | "nbformat": 4, 787 | "nbformat_minor": 5 788 | } 789 | -------------------------------------------------------------------------------- /100 Days Python/Day 26 - lambda - map - filter - reduce.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "cdeb53f3", 6 | "metadata": {}, 7 | "source": [ 8 | "# Lambda Functions\n", 9 | "\n", 10 | "Anonymous functions.\n", 11 | "\n", 12 | "Single Expression Limit.\n", 13 | "\n", 14 | "**Syntax**: `lambda args: expr`\n", 15 | "\n", 16 | "**Example**: `lambda a, b: a + b`" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "id": "05546f7f", 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "81" 29 | ] 30 | }, 31 | "execution_count": 3, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "x = lambda x : x**2\n", 38 | "x(9)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 4, 44 | "id": "bc1680f4", 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "data": { 49 | "text/plain": [ 50 | "9" 51 | ] 52 | }, 53 | "execution_count": 4, 54 | "metadata": {}, 55 | "output_type": "execute_result" 56 | } 57 | ], 58 | "source": [ 59 | "a = lambda x, y : x + y\n", 60 | "a(4, 5)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 5, 66 | "id": "63d243d4", 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "function" 73 | ] 74 | }, 75 | "execution_count": 5, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "type(a)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "id": "51dc57a0", 87 | "metadata": {}, 88 | "source": [ 89 | "## Lambda Function vs. Normal Function\n", 90 | "\n", 91 | "### Lambda function:\n", 92 | "- No return value.\n", 93 | "- *Written in Single-line.*\n", 94 | "- Not used for code reusability.\n", 95 | "- *Anonymous/No name.*\n", 96 | "\n", 97 | "### Normal function:\n", 98 | "- Has a return value.\n", 99 | "- Multi-line.\n", 100 | "- Encourages code reusability via named functions." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 7, 106 | "id": "832349d4", 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "# Why?\n", 111 | "\n", 112 | "# Lambda functions are Anonymous functions ---> `lambda args: expr`,\n", 113 | "# ideal for Higher-order functions, offers Concise function writing without naming." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "id": "604a52d3", 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "# Higher Order Functions:\n", 124 | "\n", 125 | "# Functions that take/return other functions.\n", 126 | "# Useful for Abstraction, Code reuse.\n", 127 | "\n", 128 | "# Enhances code modularity, readability.\n", 129 | "# Encourages functional programming.\n", 130 | "\n", 131 | "# Can lead to Complexity and Performance overhead (if overused)." 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 9, 137 | "id": "ac262d4e", 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "True" 144 | ] 145 | }, 146 | "execution_count": 9, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "b = lambda x : x[0] == 'a'\n", 153 | "b('apple')" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 10, 159 | "id": "3b2aa862", 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "False" 166 | ] 167 | }, 168 | "execution_count": 10, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "b('banana')" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 11, 180 | "id": "e13877cd", 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "'Odd'" 187 | ] 188 | }, 189 | "execution_count": 11, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "b = lambda x :'Even' if x%2 == 0 else 'Odd'\n", 196 | "b(3)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 12, 202 | "id": "7b6233c9", 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "'Even'" 209 | ] 210 | }, 211 | "execution_count": 12, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "b(2)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 13, 223 | "id": "9c48f446", 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "# HOF" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 14, 233 | "id": "d5c8f1f5", 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "(206, 195, 240)\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "L = [11, 14, 27, 21, 23, 56, 78, 39, 45, 29, 28, 30]\n", 246 | "\n", 247 | "# Even Sum\n", 248 | "# Odd Sum\n", 249 | "# Div3 Sum\n", 250 | "\n", 251 | "def return_sum(L):\n", 252 | " even_sum = 0\n", 253 | " odd_sum = 0\n", 254 | " div3_sum = 0\n", 255 | " for i in L:\n", 256 | " if i%2 == 0:\n", 257 | " even_sum = even_sum + i\n", 258 | " for i in L:\n", 259 | " if i%2 != 0:\n", 260 | " odd_sum = odd_sum + i\n", 261 | " for i in L:\n", 262 | " if i%3 == 0:\n", 263 | " div3_sum = div3_sum + i\n", 264 | " return(even_sum, odd_sum, div3_sum)\n", 265 | "print(return_sum(L))" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 15, 271 | "id": "26baf28b", 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "206\n", 279 | "195\n", 280 | "240\n" 281 | ] 282 | } 283 | ], 284 | "source": [ 285 | "L = [11, 14, 27, 21, 23, 56, 78, 39, 45, 29, 28, 30]\n", 286 | "def return_sum(func, L):\n", 287 | " result = 0\n", 288 | " for i in L:\n", 289 | " if func(i):\n", 290 | " result = result + i\n", 291 | " return result\n", 292 | "x = lambda x : x%2 == 0 # Even Sum\n", 293 | "y = lambda x : x%2 != 0 # Odd Sum\n", 294 | "z = lambda x : x%3 == 0 # Div3 Sum\n", 295 | "print(return_sum(x, L))\n", 296 | "print(return_sum(y, L))\n", 297 | "print(return_sum(z, L))" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "id": "3bfaef8b", 303 | "metadata": {}, 304 | "source": [ 305 | "Higher-order functions (HOF) accepts *input* + define *operation of function*." 306 | ] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "id": "49dbae31", 311 | "metadata": {}, 312 | "source": [ 313 | "# Higher-Order Functions\n", 314 | "\n", 315 | "1. Map\n", 316 | "2. Filter\n", 317 | "3. Reduce" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "id": "d1580393", 323 | "metadata": {}, 324 | "source": [ 325 | "## 1. Map\n", 326 | "\n", 327 | "Applies a `function` to each item in `iterable`.\n", 328 | "\n", 329 | "**Syntax**:\n", 330 | "```python\n", 331 | "map(function, iterable)\n", 332 | "```\n", 333 | "\n", 334 | "Returns an Iterator of results." 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 18, 340 | "id": "35519f23", 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "data": { 345 | "text/plain": [ 346 | "[1, 2, 3, 4, 5, 6, 7]" 347 | ] 348 | }, 349 | "execution_count": 18, 350 | "metadata": {}, 351 | "output_type": "execute_result" 352 | } 353 | ], 354 | "source": [ 355 | "L = [1, 2, 3, 4, 5, 6, 7]\n", 356 | "L" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 19, 362 | "id": "7aa73f4d", 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "data": { 367 | "text/plain": [ 368 | "" 369 | ] 370 | }, 371 | "execution_count": 19, 372 | "metadata": {}, 373 | "output_type": "execute_result" 374 | } 375 | ], 376 | "source": [ 377 | "map(lambda x : x * 2, L)" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 20, 383 | "id": "74c01dea", 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "data": { 388 | "text/plain": [ 389 | "[2, 4, 6, 8, 10, 12, 14]" 390 | ] 391 | }, 392 | "execution_count": 20, 393 | "metadata": {}, 394 | "output_type": "execute_result" 395 | } 396 | ], 397 | "source": [ 398 | "list(map(lambda x : x * 2, L))" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 21, 404 | "id": "dd04cbc4", 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "data": { 409 | "text/plain": [ 410 | "[False, True, False, True, False, True, False]" 411 | ] 412 | }, 413 | "execution_count": 21, 414 | "metadata": {}, 415 | "output_type": "execute_result" 416 | } 417 | ], 418 | "source": [ 419 | "list(map(lambda x : x % 2 == 0, L))" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 22, 425 | "id": "c5c3170b", 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "['Jacob Martin', 'Angela Stevens', 'Ricky Smart']" 432 | ] 433 | }, 434 | "execution_count": 22, 435 | "metadata": {}, 436 | "output_type": "execute_result" 437 | } 438 | ], 439 | "source": [ 440 | "students = [\n", 441 | " {\n", 442 | " \"name\" : \"Jacob Martin\",\n", 443 | " \"Father name\" : \"Ros Martin\",\n", 444 | " \"Address\" : \"123 Hills Street\",\n", 445 | " },{\n", 446 | " \"name\" : \"Angela Stevens\",\n", 447 | " \"Father name\" : \"Robert Stevens\",\n", 448 | " \"Address\" : \"3 Upper Street London\",\n", 449 | " },{\n", 450 | " \"name\" : \"Ricky Smart\",\n", 451 | " \"Father name\" : \"William Smart\",\n", 452 | " \"Address\" : \"Unknown\",\n", 453 | " }\n", 454 | " \n", 455 | "]\n", 456 | "list(map(lambda student : student[\"name\"], students))" 457 | ] 458 | }, 459 | { 460 | "cell_type": "markdown", 461 | "id": "e3d3ba87", 462 | "metadata": {}, 463 | "source": [ 464 | "## 2. Filter\n", 465 | "\n", 466 | "Applies a `function` to `sequence`.\n", 467 | "\n", 468 | "**Syntax**:\n", 469 | "```python\n", 470 | "filter(function, sequence)\n", 471 | "```\n", 472 | "\n", 473 | "Returns elements where `function` is `True`." 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 24, 479 | "id": "5942d759", 480 | "metadata": {}, 481 | "outputs": [ 482 | { 483 | "data": { 484 | "text/plain": [ 485 | "[1, 2, 3, 4, 5, 6, 7]" 486 | ] 487 | }, 488 | "execution_count": 24, 489 | "metadata": {}, 490 | "output_type": "execute_result" 491 | } 492 | ], 493 | "source": [ 494 | "L" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 25, 500 | "id": "43be852a", 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "data": { 505 | "text/plain": [ 506 | "[5, 6, 7]" 507 | ] 508 | }, 509 | "execution_count": 25, 510 | "metadata": {}, 511 | "output_type": "execute_result" 512 | } 513 | ], 514 | "source": [ 515 | "list(filter(lambda x : x > 4, L))" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 26, 521 | "id": "94552e82", 522 | "metadata": {}, 523 | "outputs": [], 524 | "source": [ 525 | "fruits = [\"Apple\", \"Orange\", \"Mango\", \"Guava\"]" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 27, 531 | "id": "6fe1fe43", 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "data": { 536 | "text/plain": [ 537 | "['Apple', 'Orange']" 538 | ] 539 | }, 540 | "execution_count": 27, 541 | "metadata": {}, 542 | "output_type": "execute_result" 543 | } 544 | ], 545 | "source": [ 546 | "list(filter(lambda fruit : \"e\" in fruit, fruits))" 547 | ] 548 | }, 549 | { 550 | "cell_type": "markdown", 551 | "id": "6f5d2015", 552 | "metadata": {}, 553 | "source": [ 554 | "## 3. Reduce\n", 555 | "\n", 556 | "Reduces an iterable to a single value.\n", 557 | "\n", 558 | "**Syntax**:\n", 559 | "```python\n", 560 | "from functools import reduce\n", 561 | "reduce(function, iterable)\n", 562 | "```\n", 563 | "\n", 564 | "Operates on pairs of elements until a single value remains.\n", 565 | "\n", 566 | "Useful for Cumulative Operations (e.g., sum, product)." 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 29, 572 | "id": "10d7eae6", 573 | "metadata": {}, 574 | "outputs": [], 575 | "source": [ 576 | "import functools" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 30, 582 | "id": "0ef5f2a3", 583 | "metadata": {}, 584 | "outputs": [ 585 | { 586 | "data": { 587 | "text/plain": [ 588 | "[1, 2, 3, 4, 5, 6, 7]" 589 | ] 590 | }, 591 | "execution_count": 30, 592 | "metadata": {}, 593 | "output_type": "execute_result" 594 | } 595 | ], 596 | "source": [ 597 | "L" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 31, 603 | "id": "3a157ffa", 604 | "metadata": {}, 605 | "outputs": [ 606 | { 607 | "data": { 608 | "text/plain": [ 609 | "28" 610 | ] 611 | }, 612 | "execution_count": 31, 613 | "metadata": {}, 614 | "output_type": "execute_result" 615 | } 616 | ], 617 | "source": [ 618 | "functools.reduce(lambda x, y : x + y, L)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 32, 624 | "id": "fcfb18a8", 625 | "metadata": {}, 626 | "outputs": [ 627 | { 628 | "data": { 629 | "text/plain": [ 630 | "[12, 34, 56, 11, 21, 58]" 631 | ] 632 | }, 633 | "execution_count": 32, 634 | "metadata": {}, 635 | "output_type": "execute_result" 636 | } 637 | ], 638 | "source": [ 639 | "L1 = [12, 34, 56, 11, 21, 58]\n", 640 | "L1" 641 | ] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "execution_count": 33, 646 | "id": "f882f05b", 647 | "metadata": {}, 648 | "outputs": [ 649 | { 650 | "data": { 651 | "text/plain": [ 652 | "58" 653 | ] 654 | }, 655 | "execution_count": 33, 656 | "metadata": {}, 657 | "output_type": "execute_result" 658 | } 659 | ], 660 | "source": [ 661 | "functools.reduce(lambda x, y : x if x>y else y, L1)" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 34, 667 | "id": "a3d911b9", 668 | "metadata": {}, 669 | "outputs": [ 670 | { 671 | "data": { 672 | "text/plain": [ 673 | "11" 674 | ] 675 | }, 676 | "execution_count": 34, 677 | "metadata": {}, 678 | "output_type": "execute_result" 679 | } 680 | ], 681 | "source": [ 682 | "functools.reduce(lambda x, y : x if x`, `<`, `>=`, `<=`\n", 12 | "- **Logical**: `and`, `or`, `not`\n", 13 | "- **Bitwise**: `&`, `|`, `^`, `~`, `<<`, `>>`\n", 14 | "- **Assignment**: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `//=`\n", 15 | "- **Identity**: `is`, `is not`\n", 16 | "- **Membership**: `in`, `not in`" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "a3bc8ca3", 22 | "metadata": {}, 23 | "source": [ 24 | "## Arithmetic Operators" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "id": "c2a6b234", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "7\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "x = 5\n", 43 | "y = 2\n", 44 | "print(x + y)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "id": "30f459da", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "3\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "print(x - y)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "id": "0da6e085", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "10\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "print(x * y)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 4, 86 | "id": "d4d08539", 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "2.5\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "print(x / y)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "id": "3d5a14aa", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "1\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "print(x % y)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 6, 122 | "id": "1331e865", 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "25\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "print( x ** y)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 7, 140 | "id": "87faa23d", 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "2\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "print(x // y)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "id": "e87610eb", 158 | "metadata": {}, 159 | "source": [ 160 | "## Comparison Operators" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 8, 166 | "id": "1e55f373", 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "True\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "print(x > y)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 9, 184 | "id": "eb7f4b0d", 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "name": "stdout", 189 | "output_type": "stream", 190 | "text": [ 191 | "False\n" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "print(x < y)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 10, 202 | "id": "ac23f5fa", 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "True\n" 210 | ] 211 | } 212 | ], 213 | "source": [ 214 | "print(x >= y)" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 11, 220 | "id": "95f28be8", 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "False\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "print(x <= y)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 12, 238 | "id": "7e4c993b", 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "name": "stdout", 243 | "output_type": "stream", 244 | "text": [ 245 | "False\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "print(x == y)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 13, 256 | "id": "ca8cdb71", 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "True\n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "print(x != y)" 269 | ] 270 | }, 271 | { 272 | "cell_type": "markdown", 273 | "id": "2e59191b", 274 | "metadata": {}, 275 | "source": [ 276 | "## Logical Operators" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 14, 282 | "id": "54375bca", 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "name": "stdout", 287 | "output_type": "stream", 288 | "text": [ 289 | "True\n" 290 | ] 291 | } 292 | ], 293 | "source": [ 294 | "x = True\n", 295 | "y = False\n", 296 | "print( x or y)" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 15, 302 | "id": "3b08daf9", 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "False\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "print(x and y)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 16, 320 | "id": "5a7394a3", 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "False\n", 328 | "True\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "print(not x)\n", 334 | "print(not y)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "id": "9354200a", 340 | "metadata": {}, 341 | "source": [ 342 | "## Bitwise Operators" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 1, 348 | "id": "16f564ce", 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "x = 2\n", 353 | "y = 3" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 2, 359 | "id": "0340a092", 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "name": "stdout", 364 | "output_type": "stream", 365 | "text": [ 366 | "2\n" 367 | ] 368 | } 369 | ], 370 | "source": [ 371 | "print(x & y) # AND" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 3, 377 | "id": "d548aa5a", 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "name": "stdout", 382 | "output_type": "stream", 383 | "text": [ 384 | "3\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "print(x | y) # OR" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 4, 395 | "id": "64a0fb02", 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "1\n" 403 | ] 404 | } 405 | ], 406 | "source": [ 407 | "print(2 ^ 3) # XOR" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 5, 413 | "id": "deef09bf", 414 | "metadata": {}, 415 | "outputs": [ 416 | { 417 | "name": "stdout", 418 | "output_type": "stream", 419 | "text": [ 420 | "0\n" 421 | ] 422 | } 423 | ], 424 | "source": [ 425 | "print(x >> 2) # Right Shift" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 6, 431 | "id": "0682a2ad", 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "name": "stdout", 436 | "output_type": "stream", 437 | "text": [ 438 | "24\n" 439 | ] 440 | } 441 | ], 442 | "source": [ 443 | "print(y << 3) # Left Shift" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 7, 449 | "id": "def4b208", 450 | "metadata": {}, 451 | "outputs": [ 452 | { 453 | "name": "stdout", 454 | "output_type": "stream", 455 | "text": [ 456 | "-3\n" 457 | ] 458 | } 459 | ], 460 | "source": [ 461 | "print(~ x) # NOT" 462 | ] 463 | }, 464 | { 465 | "cell_type": "markdown", 466 | "id": "c71ea45b", 467 | "metadata": {}, 468 | "source": [ 469 | "## Assignment Operators" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 23, 475 | "id": "cc78c9f8", 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "name": "stdout", 480 | "output_type": "stream", 481 | "text": [ 482 | "3\n" 483 | ] 484 | } 485 | ], 486 | "source": [ 487 | "a = 3\n", 488 | "print(a)" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": 24, 494 | "id": "716d632d", 495 | "metadata": {}, 496 | "outputs": [ 497 | { 498 | "name": "stdout", 499 | "output_type": "stream", 500 | "text": [ 501 | "6\n" 502 | ] 503 | } 504 | ], 505 | "source": [ 506 | "a += 3 # a = a + 3\n", 507 | "print(a)" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 25, 513 | "id": "3de9d46d", 514 | "metadata": {}, 515 | "outputs": [ 516 | { 517 | "name": "stdout", 518 | "output_type": "stream", 519 | "text": [ 520 | "3\n" 521 | ] 522 | } 523 | ], 524 | "source": [ 525 | "a -= 3 # a = a - 3\n", 526 | "print(a)" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 26, 532 | "id": "d8442403", 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "name": "stdout", 537 | "output_type": "stream", 538 | "text": [ 539 | "9\n" 540 | ] 541 | } 542 | ], 543 | "source": [ 544 | "a *= 3 # a = a * 3\n", 545 | "print(a)" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 27, 551 | "id": "948de9b8", 552 | "metadata": {}, 553 | "outputs": [ 554 | { 555 | "name": "stdout", 556 | "output_type": "stream", 557 | "text": [ 558 | "3.0\n" 559 | ] 560 | } 561 | ], 562 | "source": [ 563 | "a /= 3 # a = a / 3\n", 564 | "print(a)" 565 | ] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "execution_count": 28, 570 | "id": "5a772dc8", 571 | "metadata": {}, 572 | "outputs": [ 573 | { 574 | "ename": "SyntaxError", 575 | "evalue": "invalid syntax (Temp/ipykernel_10668/3802894139.py, line 1)", 576 | "output_type": "error", 577 | "traceback": [ 578 | "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\pc\\AppData\\Local\\Temp/ipykernel_10668/3802894139.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m a++\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 579 | ] 580 | } 581 | ], 582 | "source": [ 583 | "a++\n", 584 | "++a" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 29, 590 | "id": "923b0206", 591 | "metadata": {}, 592 | "outputs": [], 593 | "source": [ 594 | "a = a + 3\n", 595 | "a += 3" 596 | ] 597 | }, 598 | { 599 | "cell_type": "markdown", 600 | "id": "f6e04a7b", 601 | "metadata": {}, 602 | "source": [ 603 | "## Identity Operators" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": 15, 609 | "id": "807aecf6", 610 | "metadata": {}, 611 | "outputs": [ 612 | { 613 | "name": "stdout", 614 | "output_type": "stream", 615 | "text": [ 616 | "True\n" 617 | ] 618 | } 619 | ], 620 | "source": [ 621 | "a = 3\n", 622 | "b = 3\n", 623 | "print(a is b)" 624 | ] 625 | }, 626 | { 627 | "cell_type": "code", 628 | "execution_count": 16, 629 | "id": "434647ad", 630 | "metadata": {}, 631 | "outputs": [ 632 | { 633 | "name": "stdout", 634 | "output_type": "stream", 635 | "text": [ 636 | "True\n" 637 | ] 638 | } 639 | ], 640 | "source": [ 641 | "a = \"Hello\"\n", 642 | "b = \"Hello\"\n", 643 | "print(a is b)" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 17, 649 | "id": "5bd365ab", 650 | "metadata": {}, 651 | "outputs": [ 652 | { 653 | "name": "stdout", 654 | "output_type": "stream", 655 | "text": [ 656 | "False\n" 657 | ] 658 | } 659 | ], 660 | "source": [ 661 | "a = [1, 2, 3]\n", 662 | "b = [1, 2, 3]\n", 663 | "print(a is b)" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 18, 669 | "id": "6b857d0d", 670 | "metadata": {}, 671 | "outputs": [ 672 | { 673 | "name": "stdout", 674 | "output_type": "stream", 675 | "text": [ 676 | "False\n", 677 | "True\n" 678 | ] 679 | } 680 | ], 681 | "source": [ 682 | "a = \"Hello-World\"\n", 683 | "b = \"Hello-World\"\n", 684 | "print(a is b)\n", 685 | "print(a is not b)" 686 | ] 687 | }, 688 | { 689 | "cell_type": "markdown", 690 | "id": "b57edec4", 691 | "metadata": {}, 692 | "source": [ 693 | "## Membership Operators" 694 | ] 695 | }, 696 | { 697 | "cell_type": "markdown", 698 | "id": "63dde763", 699 | "metadata": {}, 700 | "source": [ 701 | "`in` & `not in`" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": 19, 707 | "id": "b6f998b6", 708 | "metadata": {}, 709 | "outputs": [ 710 | { 711 | "name": "stdout", 712 | "output_type": "stream", 713 | "text": [ 714 | "True\n", 715 | "False\n" 716 | ] 717 | } 718 | ], 719 | "source": [ 720 | "x = \"Delhi\"\n", 721 | "print(\"D\" in x)\n", 722 | "print(\"D\" not in x)" 723 | ] 724 | }, 725 | { 726 | "cell_type": "code", 727 | "execution_count": 20, 728 | "id": "8b8231e7", 729 | "metadata": {}, 730 | "outputs": [ 731 | { 732 | "name": "stdout", 733 | "output_type": "stream", 734 | "text": [ 735 | "True\n", 736 | "False\n" 737 | ] 738 | } 739 | ], 740 | "source": [ 741 | "x = [1, 2, 3]\n", 742 | "print(1 in x)\n", 743 | "print(5 in x)" 744 | ] 745 | }, 746 | { 747 | "cell_type": "code", 748 | "execution_count": 21, 749 | "id": "984c4f5a", 750 | "metadata": {}, 751 | "outputs": [ 752 | { 753 | "name": "stdout", 754 | "output_type": "stream", 755 | "text": [ 756 | "True\n", 757 | "False\n" 758 | ] 759 | } 760 | ], 761 | "source": [ 762 | "x = (1, 2, 3)\n", 763 | "print(1 in x)\n", 764 | "print(5 in x)" 765 | ] 766 | }, 767 | { 768 | "cell_type": "code", 769 | "execution_count": 22, 770 | "id": "346fb486", 771 | "metadata": {}, 772 | "outputs": [ 773 | { 774 | "name": "stdout", 775 | "output_type": "stream", 776 | "text": [ 777 | "Enter a 3 digit number: 666\n", 778 | "18\n" 779 | ] 780 | } 781 | ], 782 | "source": [ 783 | "# Sum of digits of a 3-digit number\n", 784 | "number = int(input('Enter a 3 digit number: '))\n", 785 | "\n", 786 | "# Units digit\n", 787 | "a = number % 10\n", 788 | "number //= 10\n", 789 | "\n", 790 | "# Tens digit\n", 791 | "b = number % 10\n", 792 | "number //= 10\n", 793 | "\n", 794 | "# Hundreds digit\n", 795 | "c = number % 10\n", 796 | "\n", 797 | "print(a + b + c)" 798 | ] 799 | } 800 | ], 801 | "metadata": { 802 | "kernelspec": { 803 | "display_name": "Python 3 (ipykernel)", 804 | "language": "python", 805 | "name": "python3" 806 | }, 807 | "language_info": { 808 | "codemirror_mode": { 809 | "name": "ipython", 810 | "version": 3 811 | }, 812 | "file_extension": ".py", 813 | "mimetype": "text/x-python", 814 | "name": "python", 815 | "nbconvert_exporter": "python", 816 | "pygments_lexer": "ipython3", 817 | "version": "3.10.9" 818 | } 819 | }, 820 | "nbformat": 4, 821 | "nbformat_minor": 5 822 | } 823 | -------------------------------------------------------------------------------- /100 Days Python/Day 17 - built-in-modules.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6975c47a", 6 | "metadata": {}, 7 | "source": [ 8 | "# What are Modules?\n", 9 | "\n", 10 | "- **Code libraries** in Python.\n", 11 | "- **Contain functions** for use.\n", 12 | "\n", 13 | "*Examples:*\n", 14 | "- **Math**: Math ops\n", 15 | "- **Random**: Random numbers\n", 16 | "- **os**: OS interaction\n", 17 | "- **time**: Time tasks" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "id": "c827ad4b", 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "\n", 31 | "Please wait a moment while I gather a list of all available modules...\n", 32 | "\n" 33 | ] 34 | }, 35 | { 36 | "name": "stderr", 37 | "output_type": "stream", 38 | "text": [ 39 | "C:\\Users\\pc\\anaconda3\\lib\\site-packages\\IPython\\kernel\\__init__.py:12: ShimWarning: The `IPython.kernel` package has been deprecated since IPython 4.0.You should import from ipykernel or jupyter_client instead.\n", 40 | " warn(\"The `IPython.kernel` package has been deprecated since IPython 4.0.\"\n", 41 | "C:\\Users\\pc\\anaconda3\\lib\\pkgutil.py:93: UserWarning: The numpy.array_api submodule is still experimental. See NEP 47.\n", 42 | " __import__(info.name)\n" 43 | ] 44 | }, 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "IPython bs4 mailcap sspicon\n", 50 | "OpenSSL builtins markupsafe stat\n", 51 | "PIL bz2 marshal statistics\n", 52 | "PyQt5 cProfile math storemagic\n", 53 | "__future__ calendar matplotlib_inline string\n", 54 | "_abc certifi menuinst stringprep\n", 55 | "_aix_support cffi mimetypes struct\n", 56 | "_ast cgi mistune subprocess\n", 57 | "_asyncio cgitb mmap sunau\n", 58 | "_bisect chardet mmapfile symbol\n", 59 | "_blake2 charset_normalizer mmsystem sympyprinting\n", 60 | "_bootlocale chunk modulefinder symtable\n", 61 | "_bootsubprocess click msilib sys\n", 62 | "_bz2 clyent msvcrt sysconfig\n", 63 | "_cffi_backend cmath multiprocessing tabnanny\n", 64 | "_codecs cmd navigator_updater tarfile\n", 65 | "_codecs_cn code nbclassic telnetlib\n", 66 | "_codecs_hk codecs nbclient tempfile\n", 67 | "_codecs_iso2022 codeop nbconvert terminado\n", 68 | "_codecs_jp collections nbformat test\n", 69 | "_codecs_kr colorama nest_asyncio test_pycosat\n", 70 | "_codecs_tw colorsys netbios testpath\n", 71 | "_collections commctrl netrc tests\n", 72 | "_collections_abc compileall nntplib textwrap\n", 73 | "_compat_pickle concurrent notebook this\n", 74 | "_compression conda nt threading\n", 75 | "_contextvars conda_content_trust ntpath time\n", 76 | "_csv conda_env ntsecuritycon timeit\n", 77 | "_ctypes conda_package_handling nturl2path timer\n", 78 | "_ctypes_test conda_token numbers tkinter\n", 79 | "_datetime conda_verify numpy token\n", 80 | "_decimal configparser odbc tokenize\n", 81 | "_distutils_hack contextlib olefile tornado\n", 82 | "_elementtree contextvars opcode tqdm\n", 83 | "_functools copy operator trace\n", 84 | "_hashlib copyreg optparse traceback\n", 85 | "_heapq crypt os tracemalloc\n", 86 | "_imp cryptography packaging traitlets\n", 87 | "_io csv pandas tty\n", 88 | "_json ctypes pandocfilters turtle\n", 89 | "_locale curses parser turtledemo\n", 90 | "_lsprof cwp parso types\n", 91 | "_lzma cythonmagic past typing\n", 92 | "_markupbase dataclasses pathlib ujson\n", 93 | "_md5 datetime pdb unicodedata\n", 94 | "_msi dateutil perfmon unittest\n", 95 | "_multibytecodec dbi pickle urllib\n", 96 | "_multiprocessing dbm pickleshare urllib3\n", 97 | "_nsis dde pickletools uu\n", 98 | "_opcode debugpy pip uuid\n", 99 | "_operator decimal pipes venv\n", 100 | "_osx_support decorator pkg_resources warnings\n", 101 | "_overlapped defusedxml pkgutil wave\n", 102 | "_peg_parser difflib platform wcwidth\n", 103 | "_pickle dis plistlib weakref\n", 104 | "_py_abc distutils poplib webbrowser\n", 105 | "_pydecimal doctest posixpath webencodings\n", 106 | "_pyio email pprint werkzeug\n", 107 | "_pyrsistent_version encodings profile wheel\n", 108 | "_queue ensurepip prometheus_client widgetsnbextension\n", 109 | "_random entrypoints prompt_toolkit win2kras\n", 110 | "_sha1 enum pstats win32api\n", 111 | "_sha256 errno psutil win32clipboard\n", 112 | "_sha3 faulthandler pty win32com\n", 113 | "_sha512 filecmp pvectorc win32con\n", 114 | "_signal fileinput py_compile win32console\n", 115 | "_sitebuiltins flask pyclbr win32cred\n", 116 | "_socket fnmatch pycosat win32crypt\n", 117 | "_sqlite3 formatter pycparser win32cryptcon\n", 118 | "_sre fractions pydoc win32event\n", 119 | "_ssl ftplib pydoc_data win32evtlog\n", 120 | "_stat functools pyexpat win32evtlogutil\n", 121 | "_statistics future pygments win32file\n", 122 | "_string gc pyparsing win32gui\n", 123 | "_strptime genericpath pyrsistent win32gui_struct\n", 124 | "_struct getopt pythoncom win32help\n", 125 | "_symtable getpass pytz win32inet\n", 126 | "_system_path gettext pywin win32inetcon\n", 127 | "_testbuffer glob pywin32_bootstrap win32job\n", 128 | "_testcapi graphlib pywin32_testutil win32lz\n", 129 | "_testconsole gzip pywintypes win32net\n", 130 | "_testimportmultiple hashlib qtpy win32netcon\n", 131 | "_testinternalcapi heapq queue win32pdh\n", 132 | "_testmultiphase hmac quopri win32pdhquery\n", 133 | "_thread html random win32pdhutil\n", 134 | "_threading_local html5lib rasutil win32pipe\n", 135 | "_tkinter http re win32print\n", 136 | "_tracemalloc idlelib regcheck win32process\n", 137 | "_uuid idna regutil win32profile\n", 138 | "_warnings imaplib repo_cli win32ras\n", 139 | "_weakref imghdr reprlib win32rcparser\n", 140 | "_weakrefset imp requests win32security\n", 141 | "_win32sysloader importlib rlcompleter win32service\n", 142 | "_winapi importlib_metadata rmagic win32serviceutil\n", 143 | "_winxptheme inspect ruamel_yaml win32timezone\n", 144 | "_xxsubinterpreters io runpy win32trace\n", 145 | "_yaml ipaddress sched win32traceutil\n", 146 | "_zoneinfo ipykernel scripts win32transaction\n", 147 | "abc ipykernel_launcher secrets win32ts\n", 148 | "adodbapi ipython_genutils select win32ui\n", 149 | "afxres ipywidgets selectors win32uiole\n", 150 | "aifc isapi send2trash win32verstamp\n", 151 | "anaconda_navigator itertools servicemanager win32wnet\n", 152 | "antigravity itsdangerous setuptools win_inet_pton\n", 153 | "anyio jedi shelve wincertstore\n", 154 | "argon2 jinja2 shlex winerror\n", 155 | "argparse json shutil winioctlcon\n", 156 | "array json5 signal winnt\n", 157 | "ast jsonschema sip winperf\n", 158 | "async_generator jupyter sipconfig winpty\n", 159 | "asynchat jupyter_client sipdistutils winreg\n", 160 | "asyncio jupyter_core site winsound\n", 161 | "asyncore jupyter_server six winxpgui\n", 162 | "atexit jupyterlab smtpd winxptheme\n", 163 | "attr jupyterlab_pygments smtplib wsgiref\n", 164 | "audioop jupyterlab_server sndhdr xdrlib\n", 165 | "autoreload jupyterlab_widgets sniffio xml\n", 166 | "babel jwt socket xmlrpc\n", 167 | "backcall keyword socketserver xmltodict\n", 168 | "backports lib2to3 socks xxsubtype\n", 169 | "base64 libfuturize sockshandler yaml\n", 170 | "bdb libpasteurize soupsieve zipapp\n", 171 | "binascii linecache sqlite3 zipfile\n", 172 | "binhex locale sre_compile zipimport\n", 173 | "binstar_client logging sre_constants zipp\n", 174 | "bisect lxml sre_parse zlib\n", 175 | "bleach lzma ssl zmq\n", 176 | "brotli mailbox sspi zoneinfo\n", 177 | "\n", 178 | "Enter any module name to get more help. Or, type \"modules spam\" to search\n", 179 | "for modules whose name or summary contain the string \"spam\".\n", 180 | "\n" 181 | ] 182 | }, 183 | { 184 | "name": "stderr", 185 | "output_type": "stream", 186 | "text": [ 187 | "C:\\Users\\pc\\anaconda3\\lib\\pkgutil.py:108: VisibleDeprecationWarning: zmq.eventloop.minitornado is deprecated in pyzmq 14.0 and will be removed.\n", 188 | " Install tornado itself to use zmq with the tornado IOLoop.\n", 189 | " \n", 190 | " yield from walk_packages(path, info.name+'.', onerror)\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "help('modules')" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 2, 201 | "id": "b5c2ca44", 202 | "metadata": { 203 | "scrolled": true 204 | }, 205 | "outputs": [ 206 | { 207 | "name": "stdout", 208 | "output_type": "stream", 209 | "text": [ 210 | "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n" 211 | ] 212 | } 213 | ], 214 | "source": [ 215 | "# keyword module\n", 216 | "import keyword\n", 217 | "print(keyword.kwlist) # Reserved keywords in Python" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "id": "632f986e", 223 | "metadata": {}, 224 | "source": [ 225 | "### Math Module" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 2, 231 | "id": "ec581766", 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "# include \n", 236 | "import math" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 3, 242 | "id": "0c76afdd", 243 | "metadata": {}, 244 | "outputs": [ 245 | { 246 | "data": { 247 | "text/plain": [ 248 | "3.141592653589793" 249 | ] 250 | }, 251 | "execution_count": 3, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "math.pi" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 4, 263 | "id": "6ac1e347", 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "2.718281828459045" 270 | ] 271 | }, 272 | "execution_count": 4, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "math.e" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 5, 284 | "id": "92777285", 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "data": { 289 | "text/plain": [ 290 | "120" 291 | ] 292 | }, 293 | "execution_count": 5, 294 | "metadata": {}, 295 | "output_type": "execute_result" 296 | } 297 | ], 298 | "source": [ 299 | "math.factorial(5)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 6, 305 | "id": "81875d42", 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "data": { 310 | "text/plain": [ 311 | "7" 312 | ] 313 | }, 314 | "execution_count": 6, 315 | "metadata": {}, 316 | "output_type": "execute_result" 317 | } 318 | ], 319 | "source": [ 320 | "math.ceil(6.3)" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 7, 326 | "id": "5dc86635", 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "6" 333 | ] 334 | }, 335 | "execution_count": 7, 336 | "metadata": {}, 337 | "output_type": "execute_result" 338 | } 339 | ], 340 | "source": [ 341 | "math.floor(6.9)" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "id": "e954367e", 347 | "metadata": {}, 348 | "source": [ 349 | "### Random Module" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 8, 355 | "id": "894d866d", 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [ 359 | "import random" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 9, 365 | "id": "39ba8fe2", 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "data": { 370 | "text/plain": [ 371 | "15" 372 | ] 373 | }, 374 | "execution_count": 9, 375 | "metadata": {}, 376 | "output_type": "execute_result" 377 | } 378 | ], 379 | "source": [ 380 | "random.randint(1, 100)" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 10, 386 | "id": "09476dd9", 387 | "metadata": {}, 388 | "outputs": [ 389 | { 390 | "data": { 391 | "text/plain": [ 392 | "[1, 3, 2, 5, 4]" 393 | ] 394 | }, 395 | "execution_count": 10, 396 | "metadata": {}, 397 | "output_type": "execute_result" 398 | } 399 | ], 400 | "source": [ 401 | "a = [1, 2, 3, 4, 5]\n", 402 | "random.shuffle(a)\n", 403 | "a" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "id": "645bfaea", 409 | "metadata": {}, 410 | "source": [ 411 | "### Time Module" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 11, 417 | "id": "0ab1cfde", 418 | "metadata": {}, 419 | "outputs": [], 420 | "source": [ 421 | "import time" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 12, 427 | "id": "0f72a97f", 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "1661709696.581269" 434 | ] 435 | }, 436 | "execution_count": 12, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "time.time() # Current time (seconds)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 13, 448 | "id": "07fdd5d5", 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "data": { 453 | "text/plain": [ 454 | "'Sun Aug 28 23:31:38 2022'" 455 | ] 456 | }, 457 | "execution_count": 13, 458 | "metadata": {}, 459 | "output_type": "execute_result" 460 | } 461 | ], 462 | "source": [ 463 | "time.ctime() # Current time (string)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": 14, 469 | "id": "629230ac", 470 | "metadata": {}, 471 | "outputs": [ 472 | { 473 | "name": "stdout", 474 | "output_type": "stream", 475 | "text": [ 476 | "Hello\n", 477 | "World\n" 478 | ] 479 | } 480 | ], 481 | "source": [ 482 | "print(\"Hello\")\n", 483 | "time.sleep(5) # Delay for 5 seconds\n", 484 | "print(\"World\")" 485 | ] 486 | }, 487 | { 488 | "cell_type": "markdown", 489 | "id": "4d026824", 490 | "metadata": {}, 491 | "source": [ 492 | "### Datetime Module" 493 | ] 494 | }, 495 | { 496 | "cell_type": "code", 497 | "execution_count": 1, 498 | "id": "ec47dd18", 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "name": "stdout", 503 | "output_type": "stream", 504 | "text": [ 505 | "2024-06-27 00:03:28.535910\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "import datetime\n", 511 | "print(datetime.datetime.now()) # Current date/time" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "id": "e222133f", 517 | "metadata": {}, 518 | "source": [ 519 | "### OS Module" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 15, 525 | "id": "2839f55c", 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "import os" 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "execution_count": 16, 535 | "id": "d2eb69b8", 536 | "metadata": {}, 537 | "outputs": [ 538 | { 539 | "data": { 540 | "text/plain": [ 541 | "'C:\\\\Users\\\\pc\\\\100 Days of Python Programming'" 542 | ] 543 | }, 544 | "execution_count": 16, 545 | "metadata": {}, 546 | "output_type": "execute_result" 547 | } 548 | ], 549 | "source": [ 550 | "os.getcwd() # Current working dir" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 17, 556 | "id": "a2a14d8d", 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "data": { 561 | "text/plain": [ 562 | "['.ipynb_checkpoints',\n", 563 | " 'Day 01 - print-function.ipynb',\n", 564 | " 'Day 02 - data-types.ipynb',\n", 565 | " 'Day 03 - comments.ipynb',\n", 566 | " 'Day 04 - variables.ipynb',\n", 567 | " 'Day 05 - keywords-and-identifiers.ipynb',\n", 568 | " 'Day 06 - taking-user-input-and-type-conversion.ipynb',\n", 569 | " 'Day 07 - literals.ipynb',\n", 570 | " 'Day 08 - operators.ipynb',\n", 571 | " 'Day 09 - decision-control.ipynb',\n", 572 | " 'Day 10 - indentation.ipynb',\n", 573 | " 'Day 11 - while-loop.ipynb',\n", 574 | " 'Day 12 - guessing-game.ipynb',\n", 575 | " 'Day 13 - for-loop.ipynb',\n", 576 | " 'Day 14 - nested-loops.ipynb',\n", 577 | " 'Day 15 - break-continue-and-pass.ipynb',\n", 578 | " 'Day 16 - built-in-functions.ipynb',\n", 579 | " 'Day 17 - built-in-modules.ipynb',\n", 580 | " 'Day 18..ipynb',\n", 581 | " 'Day 19..ipynb',\n", 582 | " 'Day 20..ipynb',\n", 583 | " 'Day 21..ipynb',\n", 584 | " 'Day 22..ipynb',\n", 585 | " 'Day 23..ipynb',\n", 586 | " 'Day 24..ipynb',\n", 587 | " 'Day 25..ipynb']" 588 | ] 589 | }, 590 | "execution_count": 17, 591 | "metadata": {}, 592 | "output_type": "execute_result" 593 | } 594 | ], 595 | "source": [ 596 | "os.listdir() # Files in dir" 597 | ] 598 | } 599 | ], 600 | "metadata": { 601 | "kernelspec": { 602 | "display_name": "Python 3 (ipykernel)", 603 | "language": "python", 604 | "name": "python3" 605 | }, 606 | "language_info": { 607 | "codemirror_mode": { 608 | "name": "ipython", 609 | "version": 3 610 | }, 611 | "file_extension": ".py", 612 | "mimetype": "text/x-python", 613 | "name": "python", 614 | "nbconvert_exporter": "python", 615 | "pygments_lexer": "ipython3", 616 | "version": "3.10.9" 617 | } 618 | }, 619 | "nbformat": 4, 620 | "nbformat_minor": 5 621 | } 622 | --------------------------------------------------------------------------------