python examples 4 ، كورس تعلم بايثون للمبتدى والمحترف دوره شاملة بايثون ، تمارين بايثون تعلم لغة بايثون .
Write a Python program to create a sequence where the first four members of the sequence are equal to one, and each successive term of the sequence is equal to the sum of the four previous ones. Find the Nth member of the sequence.
Write a Python program to create a sequence where the first four members of the sequence are equal to one, and each successive term of the sequence is equal to the sum of the four previous ones. Find the Nth member of the sequence.
Example
# Code : def new_seq(n): if n==1 or n==2 or n==3 or n==4: return 1 return new_seq(n-1) + new_seq(n-2) + new_seq(n-3) + new_seq(n-4) print(new_seq(5)) print(new_seq(6)) print(new_seq(7))
Write a Python program to add two positive integers without using the '+' operator.
Write a Python program to add two positive integers without using the '+' operator.
Example
# Note: Use bitwise operations to add two numbers. # Code : def add_without_plus_operator(a, b): while b != 0: data = a & b a = a ^ b b = data << 1 return a print(add_without_plus_operator(2, 10)) print(add_without_plus_operator(-20, 10)) print(add_without_plus_operator(-10, -20))
Write a Python program to check the priority of the four operators (+, -, *, /).
Write a Python program to check the priority of the four operators (+, -, *, /).
Example
# Code : from collections import deque import re operators = "+-/*" parenthesis = "()" priority = { '+': 0, '-': 0, '*': 1, '/': 1, } def test_higher_priority(operator1, operator2): return priority[operator1] >= priority[operator2] print(test_higher_priority('*','-')) print(test_higher_priority('+','-')) print(test_higher_priority('+','*')) print(test_higher_priority('+','/')) print(test_higher_priority('*','/'))
Write a Python program to get the third side of right angled triangle from two given sides.
Write a Python program to get the third side of right angled triangle from two given sides.
Example
# Note: Use bitwise operations to add two numbers. # Code: def pythagoras(opposite_side,adjacent_side,hypotenuse): if opposite_side == str("x"): return ("Opposite = " + str(((hypotenuse**2) - (adjacent_side**2))**0.5)) elif adjacent_side == str("x"): return ("Adjacent = " + str(((hypotenuse**2) - (opposite_side**2))**0.5)) elif hypotenuse == str("x"): return ("Hypotenuse = " + str(((opposite_side**2) + (adjacent_side**2))**0.5)) else: return "You know the answer!" print(pythagoras(3,4,'x')) print(pythagoras(3,'x',5)) print(pythagoras('x',4,5)) print(pythagoras(3,4,5))
Write a Python program to find the median among three given numbers.
Write a Python program to find the median among three given numbers.
Example
# code : x = input("Input the first number") y = input("Input the second number") z = input("Input the third number") print("Median of the above three numbers -") if y < x and x < z: print(x) elif z < x and x < y: print(x) elif z < y and y < x: print(y) elif x < y and y < z: print(y) elif y < z and z < x: print(z) elif x < z and z < y: print(z)
Python Program to Find Armstrong Number in an Interval
Python Program to Find Armstrong Number in an Interval
Example
# Code: lower = 100 upper = 2000 for num in range(lower, upper + 1): # order of number order = len(str(num)) # initialize sum sum = 0 temp = num while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 if num == sum: print(num)
Python Program To Display Powers of 2 Using Anonymous Function
Python Program To Display Powers of 2 Using Anonymous Function
Example
# Code: # Display the powers of 2 using anonymous function terms = 10 # Uncomment code below to take input from the user # terms = int(input("How many terms? ")) # use anonymous function result = list(map(lambda x: 2 ** x, range(terms))) print("The total terms are:",terms) for i in range(terms): print("2 raised to power",i,"is",result[i])
Python Program to Find Numbers Divisible by Another Number
Python Program to Find Numbers Divisible by Another Number
Example
#Code: # Take a list of numbers my_list = [12, 65, 54, 39, 102, 339, 221,] # use anonymous function to filter result = list(filter(lambda x: (x % 13 == 0), my_list)) # display the result print("Numbers divisible by 13 are",result)
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Example
# Code: # Python program to convert decimal into other number systems dec = 344 print("The decimal value of", dec, "is:") print(bin(dec), "in binary.") print(oct(dec), "in octal.") print(hex(dec), "in hexadecimal.")
Python Program to Make a Simple Calculator
Python Program to Make a Simple Calculator
Example
# Code: # Program make a simple calculator # This function adds two numbers def add(x, y): return x + y # This function subtracts two numbers def subtract(x, y): return x - y # This function multiplies two numbers def multiply(x, y): return x * y # This function divides two numbers def divide(x, y): return x / y print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") while True: # Take input from the user choice = input("Enter choice(1/2/3/4): ") # Check if choice is one of the four options if choice in ('1', '2', '3', '4'): num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) break else: print("Invalid Input")
Python program to interchange first and last elements in a list
Python program to interchange first and last elements in a list
Example
# Examples: # Input : [12, 35, 9, 56, 24] # Output : [24, 35, 9, 56, 12] # Input : [1, 2, 3] # Output : [3, 2, 1] # Code : # Python3 program to swap first # and last element of a list # Swap function def swapList(newList): size = len(newList) # Swapping temp = newList[0] newList[0] = newList[size - 1] newList[size - 1] = temp return newList # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList))
Python program to swap two elements in a list
Python program to swap two elements in a list
Example
# Examples: # Input : List = [23, 65, 19, 90], pos1 = 1, pos2 = 3 # Output : [19, 65, 23, 90] # Input : List = [1, 2, 3, 4, 5], pos1 = 2, pos2 = 5 # Output : [1, 5, 3, 4, 2] #Code : # Python3 program to swap elements # at given positions # Swap function def swapPositions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1-1, pos2-1))
Python | Ways to find length of list
Python | Ways to find length of list
Example
# Code : # Python code to demonstrate # length of list # using naive method # Initializing list test_list = [ 1, 4, 5, 7, 8 ] # Printing test_list print ("The list is : " + str(test_list)) # Finding length of list # using loop # Initializing counter counter = 0 for i in test_list: # incrementing counter counter = counter + 1 # Printing length of list print ("Length of list using naive method is : " + str(counter))
Check if element exists in list in Python
Check if element exists in list in Python
Example
# Code : # Python code to demonstrate # checking of element existence # using loops and in # Initializing list test_list = [ 1, 6, 3, 5, 3, 4 ] print("Checking if 4 exists in list ( using loop ) : ") # Checking if 4 exists in list # using loop for i in test_list: if(i == 4) : print ("Element Exists") print("Checking if 4 exists in list ( using in ) : ") # Checking if 4 exists in list # using in if (4 in test_list): print ("Element Exists")
Python | Reversing a List
Python | Reversing a List
Example
# Examples: # Input : list = [10, 11, 12, 13, 14, 15] # Output : [15, 14, 13, 12, 11, 10] # Input : list = [4, 5, 6, 7, 8, 9] # Output : [9, 8, 7, 6, 5, 4] # Code : # Reversing a list using reversed() def Reverse(lst): return [ele for ele in reversed(lst)] # Driver Code lst = [10, 11, 12, 13, 14, 15] print(Reverse(lst))