25 Basic Exercises of Python for Beginners – Part 2
Python is a popular high-level programming language used for various applications such as web development, data analysis, and artificial intelligence. If you are a beginner in Python, it is essential to practice writing code to improve your programming skills. In this regard, here are 25 basic practice exercises for Python beginners to solve in PyCharm. These exercises cover fundamental concepts such as data types, control structures, and functions. By completing these exercises, you will become more familiar with the Python syntax and develop your problem-solving abilities.
Exercise 26: Write a Python program to calculate the factorial of a given number.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # Output: 120
Exercise 27: Write a Python program to print the Fibonacci series up to a given number.
def fibonacci(n):
a, b = 0, 1
while a < n:
print(a)
a, b = b, a+b
fibonacci(10) # Output: 0 1 1 2 3 5 8
Exercise 28: Write a Python program to check whether a given number is prime or not.
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
print(is_prime(7)) # Output: True
Exercise 29: Write a Python program to find the largest and smallest elements in a given list.
def find_min_max(lst):
if not lst:
return None, None
min_elem, max_elem = lst[0], lst[0]
for elem in lst:
if elem < min_elem:
min_elem = elem
elif elem > max_elem:
max_elem = elem
return min_elem, max_elem
lst = [3, 5, 1, 9, 2]
print(find_min_max(lst)) # Output: (1, 9)
Exercise 30: Write a Python program to check whether a given string is palindrome or not.
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("radar")) # Output: True
Exercise 31: Write a Python program to reverse a given string.
def reverse_string(s):
return s[::-1]
print(reverse_string("hello")) # Output: olleh
Exercise 32: Write a Python program to remove the duplicates from a given list.
def remove_duplicates(lst):
return list(set(lst))
lst = [1, 2, 3, 2, 4, 5, 1]
print(remove_duplicates(lst)) # Output: [1, 2, 3, 4, 5]
Exercise 33: Write a Python program to find the second largest number in a given list.
def second_largest(lst):
if len(lst) < 2:
return None
largest = max(lst[0], lst[1])
second_largest = min(lst[0], lst[1])
for i in range(2, len(lst)):
if lst[i] > largest:
second_largest = largest
largest = lst[i]
elif lst[i] > second_largest and lst[i] != largest:
second_largest = lst[i]
return second_largest
lst = [3, 5, 1, 9, 2]
print(second_largest(lst)) # Output: 5
Exercise 34: Write a Python program to check whether a given string is a pangram or not.
import string
def is_pangram(s):
s = s.lower()
alphabet = set(string.ascii_lowercase)
return set(s) >= alphabet
print(is_pangram("The quick brown fox jumps over the lazy dog")) # Output: True
Exercise 35: Write a Python program to count the number of words in a given sentence.
def count_words(sentence):
return len(sentence.split())
sentence = "The quick brown fox jumps over the lazy dog."
print(count_words(sentence)) # Output: 9
Exercise 36: Write a Python program to find the first non-repeating character in a given string.
from collections import Counter
def first_non_repeating_char(s):
count = Counter(s)
for char in s:
if count[char] == 1:
return char
return None
s = "aabbcceffggh"
print(first_non_repeating_char(s)) # Output: e
Exercise 37: Write a Python program to find the intersection of two given lists.
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
lst1 = [1, 2, 3, 4, 5]
lst2 = [3, 4, 5, 6, 7]
print(intersection(lst1, lst2)) # Output: [3, 4, 5]
Exercise 38: Write a Python program to find the union of two given lists.
def union(lst1, lst2):
return list(set(lst1) | set(lst2))
lst1 = [1, 2, 3, 4, 5]
lst2 = [3, 4, 5, 6, 7]
print(union(lst1, lst2)) # Output: [1, 2, 3, 4, 5, 6, 7]
Exercise 39: Write a Python program to find the difference between two given lists.
def difference(lst1, lst2):
return list(set(lst1) - set(lst2))
lst1 = [1, 2, 3, 4, 5]
lst2 = [3, 4, 5, 6, 7]
print(difference(lst1, lst2)) # Output: [1, 2]
Exercise 40: Write a Python program to remove the leading and trailing whitespaces from a given string.
def remove_whitespaces(s):
return s.strip()
s = " hello, world! "
print(remove_whitespaces(s)) # Output: "hello, world!"
Exercise 41: Write a Python program to find the largest palindrome in a given string.
def find_largest_palindrome(s):
largest_palindrome = ""
for i in range(len(s)):
for j in range(i+1, len(s)):
substring = s[i:j]
if substring == substring[::-1] and len(substring) > len(largest_palindrome):
largest_palindrome = substring
return largest_palindrome
s = "abcbdeffe"
print(find_largest_palindrome(s)) # Output: "effe"
Exercise 42: Write a Python program to find the common characters between two given strings.
def common_characters(s1, s2):
return "".join(sorted(set(s1) & set(s2)))
s1 = "hello"
s2 = "world"
print(common_characters(s1, s2)) # Output: "lo"
Exercise 43: Write a Python program to find the sum of all the elements in a given list.
def sum_list(lst):
return sum(lst)
lst = [1, 2, 3, 4, 5]
print(sum_list(lst)) # Output: 15
Exercise 44: Write a Python program to find the product of all the elements in a given list.
def product_list(lst):
product = 1
for num in lst:
product *= num
return product
lst = [1, 2, 3, 4, 5]
print(product_list(lst)) # Output: 120
Exercise 45: Write a Python program to find the maximum and minimum elements in a given list.
def max_min(lst):
return max(lst), min(lst)
lst = [1, 2, 3, 4, 5]
print(max_min(lst)) # Output: (5, 1)
Exercise 46: Write a Python program to reverse a given string.
def reverse_string(s):
return s[::-1]
s = "hello, world!"
print(reverse_string(s)) # Output: "!dlrow ,olleh"
Exercise 47: Write a Python program to check if a given string is a palindrome.
def is_palindrome(s):
return s == s[::-1]
s1 = "racecar"
s2 = "hello"
print(is_palindrome(s1)) # Output: True
print(is_palindrome(s2)) # Output: False
Exercise 48: Write a Python program to check if a given number is prime.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
n1 = 7
n2 = 10
print(is_prime(n1)) # Output: True
print(is_prime(n2)) # Output: False
Exercise 49: Write a Python program to generate all the prime numbers between a given range.
def generate_primes(start, end):
primes = []
for num in range(start, end+1):
if is_prime(num):
primes.append(num)
return primes
print(generate_primes(10, 30)) # Output: [11, 13, 17, 19, 23, 29]
Exercise 50: Write a Python program to check if a given string is a valid email address.
import re
def is_valid_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return bool(re.match(pattern, email))
email1 = "[email protected]"
email2 = "john.doe@example"
print(is_valid_email(email1)) # Output: True
print(is_valid_email(email2)) # Output: False
Photo by Christina Morillo
Leave a Comment