0% found this document useful (0 votes)
13 views

Python 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import math c) String slicing.

x = 3.6
ARMSTRONG FIBONACII # floor(): Returns the largest integer less than or equal to x
def string_slicing(text):
print("floor({}) = {}".format(x, math.floor(x))) print("Original string:", text)
def is_armstrong_number(num): def fibonacci(n):
fib_series = [] print("First three characters:", text[:3])
num_str = str(num) import math
x = 3.6 print("Characters from index 3 to 7:", text[3:8])
if n <= 0:
num_digits = len(num_str) result = math.ceil(x)
return fib_series print("ceil({}) = {}".format(x, result)) print("Last five characters:", text[-5:])
sum_of_cubes = 0 elif n == 1: print("Every second character:", text[::2])
fib_series.append(0) print("Reversed string:", text[::-1])
for digit in num_str: else:
import math
x = 3.6 result = math.trunc(x) def main():
sum_of_cubes += int(digit) ** num_digits fib_series = [0, 1] print("trunc({}) = {}".format(x, result)) text = "Python Programming"
return sum_of_cubes == num for i in range(2, n): string_slicing(text)
fib_series.append(fib_series[-1] + fib_series[-2])
def main(): return fib_series
if __name__ == "__main__":
main()
num = int(input("Enter a number: ")) def main(): x = 100
import math

n = int(input("Enter the value of n to display the first n Fibonacci numbers: "))


if is_armstrong_number(num): result = math.log10(x)
print("log10({}) = {}".format(x, result))
if n <= 0: CHECK STRING OPERATIONS
print(num, "is an Armstrong number.") print("Please enter a positive integer.") a) len(), split(), join(), upper(), lower(), swapcase(), title(),
else: else: import math
fib_series = fibonacci(n) def string_operations(text): a = 12
print(num, "is not an Armstrong number.") print("First", n, "Fibonacci numbers:") b = 18
print("Original string:", text) result = math.gcd(a, b)
if __name__ == "__main__": print(fib_series) print("Length of the string:", len(text)) print("gcd({}, {}) = {}".format(a, b, result))

main() if __name__ == "__main__": print("Splitting the string:", text.split())


main() print("Joining the string with '_':", '_'.join(text.split()))
import math print("Uppercase:", text.upper())
FACTORIAL OF A NUMBER degree_angle = 45 print("Lowercase:", text.lower()) print("Swapping case:", text.swapcase())
def factorial_recursive(n): radian_angle = math.radians(degree_angle)
print("Title case:", text.title())
print("radians({}) = {}".format(degree_angle, radian_angle)) import math
if n == 0: import math
def main(): result = pow(2, 3)
text = input("Enter a string: ") print("2 raised to the power of 3 is:", result)
return 1 angle_in_radians = math.pi / 6
sin_value = math.sin(angle_in_radians) string_operations(text)
else: print("sin({} radians) = {}".format(angle_in_radians, sin_value))
if __name__ == "__main__":
return n * factorial_recursive(n - 1) import math main()
angle_in_radians = math.pi / 3 # 60 degrees in radians
def main(): b) Find(), index(), count(), replace(), sorted(), strip()
cos_value = math.cos(angle_in_radians)
print("cos({} radians) = {}".format(angle_in_radians, cos_value)) def string_operations(text): import math
num = int(input("Enter a number: ")) print("Original string:", text) fractional, integer = math.modf(3.14)
if num < 0: print("Finding 'world':", text.find('world'))
print("Fractional part:", fractional)
0.14000000000000012
print("Factorial is not defined for negative numbers.") print("Index of 'world':", text.index('world')) print("Integer part:", integer)
import math print("Count of 'o':", text.count('o'))
else: angle_in_radians = math.pi / 6 # 30 degrees in radians
print("Replacing 'world' with 'Python':", text.replace('world', 'Python'))
result = factorial_recursive(num) tan_value = math.tan(angle_in_radians)
print("tan({} radians) = {}".format(angle_in_radians, tan_value)) print("Sorted characters:", sorted(text)) import math
result = math.sqrt(16)
print("Factorial of", num, "is:", result) import math print("Stripped string:", text.strip()) print("Square root of 16:", result)
x = 10 def main():
y=3 import math
remainder = math.fmod(x, y)
text = " hello world! " result = math.exp(1)
if __name__ == "__main__": main() print("fmod({}, {}) = {}".format(x, y, remainder)) string_operations(text) print("Exponential of 1:", result)
main() if __name__ == "__main__":
main()
Check List and Tuple Operations. Check Dictionary and Set Operations Set Operations
a. len(), append(), extend(), insert(), remove(). i. Add Element ix.union()
my_dict = {'a': 1, 'b': 2} set1 = {1, 2, 3}
my_list = [1, 2, 3, 4, 5]
my_dict['c'] = 3 set2 = {3, 4, 5}
print(len(my_list))
print(my_dict) union_set = set1.union(set2)
my_list = [1, 2, 3]
ii.Modify Element print(union_set)
my_list.append(4) my_dict = {'a': 1, 'b': 2} x. intersection()
print(my_list) my_dict['a'] = 10 set1 = {1, 2, 3}
Print(my_dict) set2 = {3, 4, 5}
my_list = [1, 2, 3] iii. Delete Element intersection_set = set1.intersection(set2)
my_list.extend([4, 5]) my_dict = {'a': 1, 'b': 2, 'c': 3} print(intersection_set)
print(my_list) del my_dict['b'] xi. difference()
print(my_dict) set1 = {1, 2, 3}
my_list = [1, 2, 4]
iv. clear() set2 = {3, 4, 5}
my_list.insert(2, 3)
print(my_list)
my_dict = {'a': 1, 'b': 2, 'c': 3} difference_set = set1.difference(set2)
my_dict.clear() print(difference_set)
my_list = [1, 2, 3, 4, 3] print(my_dict) xii. symmetric_difference()
my_list.remove(3) v. copy() set1 = {1, 2, 3}
print(my_list) my_dict = {'a': 1, 'b': 2} new_dict = set2 = {3, 4, 5}
my_dict.copy() symmetric_difference_set =set1.symmetric_difference(set2)
b. reverse(), clear(), sort(), sorted(), count() print(new_dict) print(symmetric_difference_set)
my_list = [1, 2, 3, 4]
vi. values()
my_list.reverse() b) Modify Index of the Data and Sort the Index
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_list)
values = my_dict.values() Modify
print(values) df.set_index('Name', inplace=True)
my_list = [1, 2, 3, 4]
my_list.clear() vii. keys() print("\nDataFrame with modified index:")
print(my_list) my_dict = {'a': 1, 'b': 2, 'c': 3} print(df)
keys = my_dict.keys()
my_list = [4, 2, 1, 3] print(keys) Sort
my_list.sort() viii.items() df.sort_index(inplace=True)
print(my_list) print("\nDataFrame after sorting index:")
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_list = [4, 2, 1, 3] print(df)
items = my_dict.items()
new_list = sorted(my_list)
print(new_list)
print(items)
print(my_list)

my_list = [1, 2, 3, 2, 2, 4]
print(my_list.count(2))

You might also like