python examples 2 كورس تعلم بايثون كامل بالعربي , كورس تعلم بايثون من الصفر تعلم بايثون للمبتدى والمحترف ، دوره شاملة بايثون ، تمارين بايثون تعلم لغة بايثون .
Python program to print Even Numbers in a List
Python program to print Even Numbers in a List
Example
# list of numbers list1 = [10, 21, 4, 45, 66, 93] # iterating each number in list for num in list1: # checking condition if num % 2 == 0: print(num, end = " ")
Python program to check if a string is palindrome or not
Python program to check if a string is palindrome or not
Example
def isPalindrome(s): rev = s[::-1] # Checking if both string are equal or not if (s == rev): return True return False # Driver code s = "malayalam" ans = isPalindrome(s) if ans == 1: print("Yes") else: print("No")
function to check if small string is there in big string
function to check if small string is there in big string
Example
def check(string, sub_str): if (string.find(sub_str) == -1): print("NO") else: print("YES") # driver code string = "zeus is a programmer" sub_str ="zeus" check(string, sub_str)
Function to find permutations of a given string
Function to find permutations of a given string
Example
from itertools import permutations def allPermutations(str): # Get all permutations of string 'ABC' permList = permutations(str) # print all permutations for perm in list(permList): print (''.join(perm)) # Driver program if name == "main": str = 'ABC' allPermutations(str)
Python3 code to find largest prime factor of number
Python3 code to find largest prime factor of number
Example
import math def maxPrimeFactors (n): maxPrime = -1 while n % 2 == 0: maxPrime = 2 n >>= 1 # equivalent to n /= 2 for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: maxPrime = i n = n / i if n > 2: maxPrime = n return int(maxPrime) n = 15 print(maxPrimeFactors(n)) n = 25698751364526 print(maxPrimeFactors(n))
Python Program to find sum of array
Python Program to find sum of array
Example
# Python 3 code to find sum # of elements in given array def _sum(arr,n): # return sum using sum # inbuilt sum() function return(sum(arr)) # driver function arr=[] # input values to list arr = [12, 3, 4, 15] # calculating length of array n = len(arr) ans = _sum(arr,n) # display sum print ('Sum of the array is ', ans)
Python Counter| Find all duplicate characters in string
Python Counter| Find all duplicate characters in string
Example
from collections import Counter def find_dup_char(input): WC = Counter(input) j = -1 for i in WC.values(): j = j + 1 if( i > 1 ): print WC.keys()[j], if name == "main": input = 'hellowelcome' find_dup_char(input)
Python Program to find largest element in an array
Python Program to find largest element in an array
Example
# Python3 program to find maximum # in arr[] of size n # python function to find maximum # in arr[] of size n def largest(arr,n): # Initialize maximum element max = arr[0] # Traverse array elements from second # and compare every element with # current max for i in range(1, n): if arr[i] > max: max = arr[i] return max # Driver Code arr = [10, 324, 45, 90, 9808] n = len(arr) Ans = largest(arr,n) print ("Largest in given array is",Ans)
Python Program for Find remainder of array multiplication divided by n
Python Program for Find remainder of array multiplication divided by n
Example
#Input : #arr[] = {100, 10, 5, 25, 35, 14}, #n = 11 #Output : 9 #Explanation : #100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9 #Code : # Python3 program to # find remainder when # all array elements # are multiplied. # Find remainder of arr[0] * arr[1] # * .. * arr[n-1] def findremainder(arr, lens, n): mul = 1 # find the individual # remainder and # multiple with mul. for i in range(lens): mul = (mul * (arr[i] % n)) % n return mul % n # Driven code arr = [ 100, 10, 5, 25, 35, 14 ] lens = len(arr) n = 11 # print the remainder # of after multiple # all the numbers print( findremainder(arr, lens, n))
Python Program to Split the array and add the first part to the end
Python Program to Split the array and add the first part to the end
Example
#Code : # Python program to split array and move first # part to end. def splitArr(arr, n, k): for i in range(0, k): x = arr[0] for j in range(0, n-1): arr[j] = arr[j + 1] arr[n-1] = x # main arr = [12, 10, 5, 6, 52, 36] n = len(arr) position = 2 splitArr(arr, n, position) for i in range(0, n): print(arr[i], end = ' ')