0% found this document useful (0 votes)
1 views4 pages

Python L

The document provides examples of using Python's built-in functions and libraries for handling numbers, strings, lists, and tuples. It demonstrates operations such as calculating absolute values, string manipulation, and list modifications, highlighting the mutability of lists versus the immutability of strings and tuples. Each section includes sample code and expected output for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Python L

The document provides examples of using Python's built-in functions and libraries for handling numbers, strings, lists, and tuples. It demonstrates operations such as calculating absolute values, string manipulation, and list modifications, highlighting the mutability of lists versus the immutability of strings and tuples. Each section includes sample code and expected output for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import math

# sample numbers

a = -5

b = -3.7

print("abs(a):", abs(a)) # absolute value (built-in)

print("[Link](b):", [Link](b)) # smallest integer ≥ b

print("[Link](1):", [Link](1)) # e^1

print("[Link](b):", [Link](b)) # absolute value (float)

OUTPUT

abs(a): 5

[Link](b): -3

[Link](1): 2.718281828459045

[Link](b): 3.7

s = " Hello "

2)STRING

print(len(s)) # len()

print([Link]()) # upper()

print([Link]()) # lower()

print([Link]()) # strip()

print([Link]("He", "Me")) # replace()

print(s[2:5]) # slicing

print(s[::-1]) # reverse slicing

for ch in s: # iteration

print(ch, end=" ")


try:

s[0] = 'h' # immutability

except TypeError:

print("\nString is immutable")

OUTPUT:

HELLO

hello

Hello

Mello

He

olleH

Hello

String is immutable

3)LIST

lst = [10, 20, 30, 40, 50]

print(len(lst)) # length of list

print(max(lst)) # largest value

print(min(lst)) # smallest value

[Link](60) # add an item

[Link](30) # remove an item

print(lst)

print(lst[1:4]) # slicing

for item in lst: # iteration

print(item, end=" ")


lst[0] = 100 # lists are mutable

print("\nList is mutable, modified:", lst)

OUTPUT:

50

10

[10, 20, 40, 50, 60]

[20, 40, 50]

10 20 40 50 60

List is mutable, modified: [100, 20, 40, 50, 60]

4)TUPLES

t = (10, 20, 30, 40, 50, 20)

print(len(t)) # length

print(max(t)) # largest

print(min(t)) # smallest

print([Link](20)) # count occurrences

print([Link](40)) # index of value

print(t[1:4]) # slicing

for item in t: # iteration

print(item, end=" ")

try:

t[0] = 100 # tuples are immutable


except TypeError:

print("\nTuple is immutable")

OUTPUT

50

10

(20, 30, 40)

10 20 30 40 50 20

Tuple is immutable

You might also like