python-practicals (7)
python-practicals (7)
def demonstrate_math_module():
# Basic math functions
print("Basic Math Functions:")
numbers = [4, 9, 16, 25]
for num in numbers:
print(f"Square root of {num}: {math.sqrt(num)}")
# Trigonometric functions
angles = [0, 30, 45, 60, 90]
print("\nTrigonometric Functions (angles in degrees):")
for angle in angles:
# Convert to radians for trig functions
rad = math.radians(angle)
print(f"Angle: {angle}°")
print(f"Sin: {math.sin(rad):.4f}")
print(f"Cos: {math.cos(rad):.4f}")
print(f"Tan: {math.tan(rad):.4f if angle != 90 else 'undefined'}")
demonstrate_math_module()
Output:
Basic Math Functions:
Square root of 4: 2.0
1
Square root of 9: 3.0
Square root of 16: 4.0
Square root of 25: 5.0
Math Constants:
� (pi): 3.141593
e: 2.718282
tau (2�): 6.283185
Rounding Functions:
Number: 3.1
Floor: 3
Ceil: 4
Round: 3
Number: 3.5
Floor: 3
Ceil: 4
Round: 4
Number: 3.9
Floor: 3
Ceil: 4
Round: 4
2
Number: -3.1
Floor: -4
Ceil: -3
Round: -3
Number: -3.5
Floor: -4
Ceil: -3
Round: -4
Number: -3.9
Floor: -4
Ceil: -3
Round: -4
# String slicing
print("\nString Slicing:")
print("First 6 characters:", text[:6])
print("Last 11 characters:", text[-11:])
print("Every second character:", text[::2])
print("Reversed string:", text[::-1])
# String methods
print("\nString Methods:")
sentence = " Python is a great programming language "
print("Original:", sentence)
print("Stripped:", sentence.strip())
print("Split words:", sentence.split())
print("Replace 'great' with 'awesome':", sentence.replace('great', 'awesome'))
# String formatting
print("\nString Formatting:")
3
name = "Alice"
age = 25
height = 1.75
# Using f-strings
print(f"Using f-string: {name} is {age} years old and {height:.2f} meters tall")
demonstrate_string_operations()
Output:
Original string: Python Programming
Length: 18
Uppercase: PYTHON PROGRAMMING
Lowercase: python programming
String Slicing:
First 6 characters: Python
Last 11 characters: Programming
Every second character: PtoPormig
Reversed string: gnimmargorP nohtyP
String Methods:
Original: Python is a great programming language
Stripped: Python is a great programming language
Split words: ['Python', 'is', 'a', 'great', 'programming', 'language']
Replace 'great' with 'awesome': Python is a awesome programming language
String Formatting:
Using format(): Alice is 25 years old and 1.75 meters tall
Using f-string: Alice is 25 years old and 1.75 meters tall
4
String Checking:
Checking string: 'Python123'
Is alphanumeric? True
Is alphabetic? False
Is numeric? False
Is uppercase? False
Is lowercase? False
Is space? False
5
# Method 1: Direct assignment
student['age'] = 21
print("\nAfter changing age directly:", student)
demonstrate_dictionary_modifications()
Output:
Original dictionary: {'name': 'John Doe', 'age': 20, 'grades': {'math': 85, 'science': 90, '
After changing age directly: {'name': 'John Doe', 'age': 21, 'grades': {'math': 85, 'science
After using update(): {'name': 'John Smith', 'age': 22, 'grades': {'math': 85, 'science': 90
After changing math grade: {'name': 'John Smith', 'age': 22, 'grades': {'math': 95, 'science
After using setdefault(): {'name': 'John Smith', 'age': 22, 'grades': {'math': 95, 'science'
6
After updating all grades: {'name': 'John Smith', 'age': 22, 'grades': {'math': 100, 'scienc
After adding new course: {'name': 'John Smith', 'age': 22, 'grades': {'math': 100, 'science'
After converting grades to letters: {'name': 'John Smith', 'age': 22, 'grades': {'math': 'A'
Would you like me to continue with the next set of practicals? We can cover
file operations, error handling, and some advanced Python concepts next.