Simple Python Programs Part 01
Convert a list into a string
list1 = ['R', 'a', 'j', 'a', 'n', 'a', 'n', 'd']
str1 = ''.join(list1)
print(str1)
print(type(str1))
Output:
Rajanand
<class 'str'>
Reverse a string
str1 = "Programming"
str2 = str1[::-1]
print(str2)
Output:
gnimmargorP
Check if a string is a palindrome using a function
def is_palindrome(str1):
"""
Palindrome is a string that reads the same forward and backward.
"""
str2 = str1[::-1]
if str1 == str2:
return "The string is a palindrome"
else:
return "The string is not a palindrome"
print(is_palindrome("palindrome"))
print(is_palindrome("malayalam"))
Output:
The string is not a palindrome
The string is a palindrome
Count the whitespace in a string
str1 = "Python is a programming language"
count = str1.count(" ")
print(count)
Output:
4
Count a character in a string
str1 = "Python is a programming language"
count = str1.upper().count("P")
print(count)
Output:
2
Check a number is prime or not
it is not handling the negative numbers and zero
num1 = 10
if num1 > 1:
for i in range(2, num1):
if num1 % i == 0:
print("The number is not a prime number")
break
else:
print("The number is a prime number")
Find a factorial of a number
def factorial(num1):
"""
Factorial is the product of all positive integers less than or equal to a given positive integer.
"""
if num1 == 0 or num1 == 1:
return 1
else:
return num1 * factorial(num1 - 1)
print(factorial(8))
Output:
40320
Find a fibonacci series
def fibonacci(num1):
"""
Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.
"""
if num1 == 0:
return 0
elif num1 == 1:
return 1
else:
return fibonacci(num1 - 1) + fibonacci(num1 - 2)
print(fibonacci(10))
Output:
55
Find an average of a tuple
def average(*args):
"""
Average is the sum of all numbers divided by the number of numbers.
"""
return sum(args) / len(args)
print(average(1, 2, 3, 4, 5, 9))
Output:
4.0
Find an average of a dictionary
def average_dict(**kwargs):
"""
Average is the sum of all numbers divided by the number of numbers.
"""
return sum(kwargs.values()) / len(kwargs)
print(average_dict(a=1, b=2, c=3, d=4, e=5))
Output:
3.0
Count vowels in a string
def count_vowels(str1):
"""
Vowels are the letters a, e, i, o, and u.
"""
vowels = "aeiouAEIOU"
count = sum(1 for char in str1 if char in vowels)
return count
print(count_vowels("programming"))
Output:
3
Find a minimum value in a list
def find_min(list1):
"""
Minimum is the smallest number in a list.
"""
return min(list1)
print(find_min([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Output:
1
Find a maximum value in a list
def find_max(list1):
"""
Maximum is the largest number in a list.
"""
return max(list1)
print(find_max([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Output:
10
Find a sum of a list
def sum_list(list1):
"""
Sum is the sum of all numbers in a list.
"""
return sum(list1)
print(sum_list([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Output:
55
Convert an integer to a decimal
def convert_to_decimal(num1):
return float(num1)
print(convert_to_decimal(10))
Output:
10.0
<class 'float'>
Check if a number is even or odd
def is_even(num1):
"""
Even is a number that is divisible by 2.
"""
if num1 % 2 == 0:
return "The number is even"
else:
return "The number is odd"
print(is_even(10))
Output:
The number is even
Check if a number is positive or negative or zero
def is_positive(num1):
"""
Positive is a number that is greater than 0.
"""
if num1 > 0:
return "The number is positive"
elif num1 < 0:
return "The number is negative"
else:
return "The number is zero"
print(is_positive(10))
Output:
The number is positive
anagram of a string
def is_anagram(str1, str2):
"""
Anagram is a string that has the same letters in a different order.
"""
return sorted(str1.lower()) == sorted(str2.lower())
print(is_anagram("listen", "silent"))
print(is_anagram("Grow", "Word"))
Output:
True
False
On this page
- Convert a list into a string
- Reverse a string
- Check if a string is a palindrome using a function
- Count the whitespace in a string
- Count a character in a string
- Check a number is prime or not
- Find a factorial of a number
- Find a fibonacci series
- Find an average of a tuple
- Find an average of a dictionary
- Count vowels in a string
- Find a minimum value in a list
- Find a maximum value in a list
- Find a sum of a list
- Convert an integer to a decimal
- Check if a number is even or odd
- Check if a number is positive or negative or zero
- anagram of a string