Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Python Basic Codes

This document contains Python code examples for basic concepts like Fibonacci series, palindrome checking, prime number checking, sorting arrays, and more. Some key examples include generating the Fibonacci series up to 15 numbers, checking if a number is a palindrome by reversing it and comparing, finding the largest and smallest number in an array, and sorting an array in ascending or descending order using the sort() method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Python Basic Codes

This document contains Python code examples for basic concepts like Fibonacci series, palindrome checking, prime number checking, sorting arrays, and more. Some key examples include generating the Fibonacci series up to 15 numbers, checking if a number is a palindrome by reversing it and comparing, finding the largest and smallest number in an array, and sorting an array in ascending or descending order using the sort() method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PYTHON BASIC CODES

FIBONACCI SERIES

n1, n2 = 0, 1

print("Fibonacci Series:", n1, n2, end=" ")

for i in range(2, 15):

n3 = n1 + n2

n1 = n2

n2 = n3

print(n3, end=" ")

print()

PALINDROME OR NOT

num = 22222

reverse = int(str(num)[::-1])

if num == reverse:

print('Palindrome')

else:

print("Not Palindrome")

ODD OR EVEN

num = int(input("Enter a Number:"))

if num % 2 == 0:

print("Even")

else:

print("Odd")

SUM OF NUMBERS

number,sum = 10,0

for i in range(number+1):

sum+=i

print(sum)
PRIME NUMBER OR NOT

num = 7

flag = 0

for i in range(2,num):

if num%i==0:

flag = 1

break

if flag == 1:

print('Not Prime')

else:

print("Prime")

REVERSE A NUMBER

num = 1234

reverse = 0

while num > 0:

remainder = num % 10

reverse = (reverse * 10) + remainder

num = num // 10

print(reverse)

ARMSTRONG NUMBER

The Numbers that can be represented as the sum of the digits raised to the power of the number of digits in the
number are called Armstrong numbers.

num = 371

digit, sum = 0, 0

length = len(str(num))

for i in range(length):

digit = int(num%10)

num = num/10

sum += pow(digit,length)

if sum==num:

print("Armstrong")

else:
print("Not Armstrong")

PERFECT NUMBER OR NOT

n = 28

sum = 0

for i in range(1, n):

if n % i == 0:

sum = sum + i

if sum == n:

print("Perfect number")

else:

print("not a Perfect number")

LCM OF TWO NUMBERS

num1 = 12

num2 = 24

for i in range(max(num1, num2), 1 + (num1 * num2)):

if i % num1 == i % num2 == 0:

lcm = i

break

print("LCM of", num1, "and", num2, "is", lcm)

HCF OF TWO NUMBERS

num1 = 36

num2 = 60

for i in range(1, min(num1, num2)):

if num1 % i == 0 and num2 % i == 0:

hcf = i

print("Hcf of", num1, "and", num2, "is", hcf)

BY EUCLIDEAN ALGORITHM

def getHCF(a, b):

return b == 0 and a or getHCF(b, a % b)

num1 = 36
num2 = 60

print(getHCF(num1, num2))

ASCII stands for American Standard Code for Information Interchange

Char = 'a'

val = ord(Char)

print(val)

VOWELS IN STRING

String = input('Enter the string :')

count = 0

String = String.lower()

for i in String:

if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':

count+=1

if count == 0:

print("No vowels")

else:

print(str(count))

STRING IS PALINDROME OR NOT

input_string = 'civic'

rev = input_string[::-1]

if input_string == rev:

print("Palindrome")

else:

print("not Palindrome")
FACTORIAL OF NUMBER

num = 6

count = 1

for i in range(1, num+1):

count = count * i

print(count)

THE SMALLEST AND LARGEST NUM IN THE ARRAY

def find_elements(lst):

smallest = min(lst)

largest = max(lst)

return smallest, largest

lst=[12, 45, 2, 41, 31, 10, 8, 6, 4]

print(find_elements(lst))

ANOTHER METHOD

arr = [10, 89, 9, 56, 4, 80, 8]

mini = arr[0]

maxi = arr[0]

for i in range(len(arr)):

if arr[i] < mini: mini = arr[i]

if arr[i] > maxi: maxi = arr[i]

print (mini)

print (maxi)

REVERSE THE ARRAY

def reverseList(A, start, end):

while start < end:

A[start], A[end] = A[end], A[start]

start += 1

end -= 1

A = [10, 20, 30, 40, 50]

reverseList(A, 0, 4)

print(A)
SORTING AN ARRAY

sort() function The sort() method is a built-in Python method that, by default, sorts the list in ascending order.
However, you can modify the order from ascending to descending by specifying the sorting criteria.

ASCENDING

numbers = [10, 30, 40, 20]

numbers.sort()

print(numbers)

DESCENDING

numbers = [10, 30, 40, 20]

numbers.sort(reverse=True)

print(numbers)

You might also like