Here are 30 Python script examples:
- Hello World:
print("Hello World!")
- Sum of two numbers:
num1 = 10
num2 = 20
sum = num1 + num2
print("Sum of {0} and {1} is {2}".format(num1, num2, sum))
- Factorial of a number:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
- Fibonacci series:
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
for i in range(10):print(fibonacci(i))
- Reverse a string:
def reverse_string(s):
return s[::-1]
print(reverse_string(“hello”))
- Count the number of words in a string:
def count_words(s):
words = s.split()
return len(words)
print(count_words(“Hello World”))
- Palindrome check:
def is_palindrome(s):
if s == s[::-1]:
return True
else:
return False
print(is_palindrome(“racecar”))
- Check if a 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
print(is_prime(17))
- Sum of digits in a number:
def sum_of_digits(n):
sum = 0
while n > 0:
sum += n % 10
n //= 10
return sum
print(sum_of_digits(123))
- Check if a string is a pangram:
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”))
- Find the largest element in a list:
def largest_element(lst):
return max(lst)
print(largest_element([1, 2, 3, 4, 5]))
- Find the second largest element in a list:
def second_largest_element(lst):
sorted_lst = sorted(lst, reverse=True)
return sorted_lst[1]
print(second_largest_element([1, 2, 3, 4, 5]))
- Find the frequency of each element in a list:
def frequency(lst):
freq = {}
for i in lst:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
return freq
print(frequency([1, 2, 3, 1, 2, 1]))
- Check if a list is sorted:
def is_sorted(lst):
return all(lst[i] <= lst[i+1] for i in range(len(lst)-1))
print(is_sorted([1, 2, 3, 4, 5]))
- Merge two lists:
list1 = [1, 2, 3]
list2 = [ 4, 5] merged_list = list1 + list2 print(merged_list)
16. Remove duplicates from a list:
“`pythonlst = [1, 2, 3, 1, 2, 1]
unique_lst = list(set(lst))
print(unique_lst)
- Convert a string to a list:
s = "Hello World"
lst = list(s)
print(lst)
- Check if two strings are anagrams:
def is_anagram(s1, s2):
return sorted(s1) == sorted(s2)
print(is_anagram(“listen”, “silent”))
- Count the number of vowels in a string:
def count_vowels(s):
vowels = "aeiou"
count = 0
for i in s:
if i in vowels:
count += 1
return count
print(count_vowels(“Hello World”))
- Check if a string is a palindrome:
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome(“racecar”))
- Find the largest palindrome in a string:
def largest_palindrome(s):
max_palindrome = ""
for i in range(len(s)):
for j in range(i+1, len(s)):
substr = s[i:j]
if substr == substr[::-1] and len(substr) > len(max_palindrome):
max_palindrome = substr
return max_palindrome
print(largest_palindrome(“forgeeksskeegfor”))
- Sort a list of dictionaries based on a key:
lst = [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}, {'name': 'Bob', 'age': 35}]
sorted_lst = sorted(lst, key=lambda x: x['age'])
print(sorted_lst)
- Convert a list of strings to lowercase:
lst = ["Hello", "WORLD"]
lower_lst = [s.lower() for s in lst]
print(lower_lst)
- Convert a list of integers to binary:
lst = [1, 2, 3, 4, 5]
binary_lst = [bin(i)[2:] for i in lst]
print(binary_lst)
- Convert a list of strings to integers:
lst = ["1", "2", "3", "4", "5"]
int_lst = [int(s) for s in lst]
print(int_lst)
- Calculate the average of a list of numbers:
lst = [1, 2, 3, 4, 5]
average = sum(lst) / len(lst)
print(average)
- Swap two variables:
a = 10
b = 20
a, b = b, a
print(a, b)
- Find the common elements between two lists:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common_elements = list(set(list1) & set(list2))
print(common_elements)
- Calculate the factorial of a number using recursion:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
- Check if a number is a power of 2:
def is_power_of_two(n):
if n == 0:
return False
else:
return n & (n-1) == 0
print(is_power_of_two(8))
These are just a few examples of what you can do with Python. There are countless other possibilities, and the more you learn, the more you’ll be able to do with the language.