Docsity

basic python codes with python programming practice, Cheat Sheet of Computer Programming

Toggle sidebar
bg1
9.Program to print Fibonacci sequence up to n terms
n = int(input("Enter the number of terms: "))
# First two terms
a, b = 0, 1
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence up to", n, "term:")
print(a)
else:
print("Fibonacci sequence:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b
10.Write a python Program to check whether a given number is prime or
not.
num = int(input("Enter a number: "))
if num <= 1:
print(num, "is not a prime number")
else:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
pf3
pf4
pf5
pf8

Partial preview of the text

Download basic python codes with python programming practice and more Cheat Sheet Computer Programming in PDF only on Docsity!

9.Program to print Fibonacci sequence up to n terms n = int(input("Enter the number of terms: "))

First two terms

a, b = 0, 1 if n <= 0: print("Please enter a positive integer") elif n == 1: print("Fibonacci sequence up to", n, "term:") print(a) else: print("Fibonacci sequence:") for i in range(n): print(a, end=" ") a, b = b, a + b 10.Write a python Program to check whether a given number is prime or not. num = int(input("Enter a number: ")) if num <= 1: print(num, "is not a prime number") else: for i in range(2, int(num**0.5) + 1): if num % i == 0: print(num, "is not a prime number") break else: print(num, "is a prime number")

11.Program to find the factorial of a number using a user-defined function.

Function to calculate factorial

def factorial(n): if n < 0: return "Factorial is not defined for negative numbers" result = 1 for i in range(1, n + 1): result *= i return result

Taking input from user

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

Calling the function

fact = factorial(num) print("Factorial of", num, "is:", fact) 12.Program to find the sum of digits of a given number using a user-defined function.

Function to calculate sum of digits

def sum_of_digits(n): total = 0 while n > 0: digit = n % 10 total += digit n = n // 10 return total

Taking input from user

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

Calling the function

ascii_value = find_ascii(ch) print("The ASCII value of", ch, "is:", ascii_value) 15.Program to check whether a given string is a palindrome or not.

Program to check if a string is a palindrome

Taking input from user

text = input("Enter a string: ")

Reverse the string

reversed_text = text[::-1]

Check palindrome

if text == reversed_text: print("The string is a palindrome") else: print("The string is not a palindrome") 16.Program to perform the following operations on a list:

Program to perform various operations on a list

Taking list input from user

lst = list(map(int, input("Enter list elements separated by space: ").split())) print("Original List:", lst)

Length of list

print("Length of list:", len(lst))

Adding an element

lst.append(10) print("After appending 10:", lst)

Inserting an element at position 1

lst.insert(1, 5) print("After inserting 5 at index 1:", lst)

Removing an element

if 10 in lst: lst.remove(10) print("After removing 10:", lst)

Sorting the list

lst.sort() print("Sorted List:", lst)

Reversing the list

lst.reverse() print("Reversed List:", lst) 17.Find the average of elements in the list.

Program to find the average of elements in a list

Taking input from user

lst = list(map(int, input("Enter list elements separated by space: ").split()))

Calculate sum and average

total = sum(lst) average = total / len(lst) print("List:", lst) print("Average of elements:", average) 18.Write a program to Sort the elements in the list.

Program to sort elements in a list

Taking input from user

lst = list(map(int, input("Enter list elements separated by space: ").split()))

Sorting the list

lst.sort()

print("Original tuple:", tup) print("Tuple with even numbers:", even_tup) 21.Write a python program to input an integer โ€˜nโ€™ and generate a dictionary containing pairs (i:ii)for i ranging from 1 to โ€˜nโ€™ and print the dictionary in a suitable format.*

Program to create dictionary (i : i*i)

Taking input from user

n = int(input("Enter a number: "))

Creating dictionary

square_dict = {} for i in range(1, n + 1): square_dict[i] = i * i

Printing the dictionary

print("Generated Dictionary:") print(square_dict) 22.Write a python program to define a class called Rectangle that takes length and breadth as parameters and includes a method to compute its perimeter.

Define Rectangle class

class Rectangle: def init(self, length, breadth): self.length = length self.breadth = breadth

Method to calculate perimeter

def perimeter(self): return 2 * (self.length + self.breadth)

Taking input from user

length = float(input("Enter length: ")) breadth = float(input("Enter breadth: "))

Creating object

rect = Rectangle(length, breadth)

Displaying perimeter

print("Perimeter of rectangle:", rect.perimeter())