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

Python DataTypes Methods

The document provides an overview of various Python data types including strings, lists, tuples, sets, dictionaries, booleans, and NoneType, along with examples of common methods and operations for each type. It demonstrates how to manipulate and interact with these data types through various built-in functions. Additionally, it covers common built-ins like length, type checking, and enumeration.
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)
13 views4 pages

Python DataTypes Methods

The document provides an overview of various Python data types including strings, lists, tuples, sets, dictionaries, booleans, and NoneType, along with examples of common methods and operations for each type. It demonstrates how to manipulate and interact with these data types through various built-in functions. Additionally, it covers common built-ins like length, type checking, and enumeration.
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

Python Data Types and Methods with

Examples
Strings (str)
s = " Hello World "

print([Link]()) # " HELLO WORLD "


print([Link]()) # " hello world "
print([Link]()) # " Hello World "
print([Link]())# " hello world "
print([Link]()) # "Hello World"
print([Link]()) # "Hello World "
print([Link]()) # " Hello World"
print([Link]("World")) # 8
print([Link]("World")) # 8
print([Link]("l")) # 3
print([Link]("World", "Python")) # " Hello Python "
print([Link]()) # ['Hello', 'World']
print("-".join(["a", "b", "c"])) # "a-b-c"
print("123".isdigit()) # True
print("abc".isalpha()) # True
print("abc123".isalnum())# True
print(" ".isspace()) # True

Lists (list)
lst = [1, 2, 3]
[Link](4)
print(lst) # [1, 2, 3, 4]
[Link]([5, 6])
print(lst) # [1, 2, 3, 4, 5, 6]
[Link](1, 100)
print(lst) # [1, 100, 2, 3, 4, 5, 6]
[Link](100)
print(lst) # [1, 2, 3, 4, 5, 6]
print([Link]()) # 6
print(lst) # [1, 2, 3, 4, 5]
[Link]()
print(lst) # [1, 2, 3, 4, 5]
[Link]()
print(lst) # [5, 4, 3, 2, 1]
print([Link](3)) # 1
print([Link](4)) # 1
copy_lst = [Link]()
print(copy_lst) # [5, 4, 3, 2, 1]
[Link]()
print(lst) # []

Tuples (tuple)
t = (10, 20, 30, 20)
print([Link](20)) # 2
print([Link](30)) # 2

Sets (set)
s = {1, 2, 3}
[Link](4)
print(s) # {1, 2, 3, 4}
[Link](2)
print(s) # {1, 3, 4}
[Link](10)
print(s) # {1, 3, 4}
print([Link]()) # removes random element
a, b = {1, 2, 3}, {3, 4, 5}
print([Link](b)) # {1, 2, 3, 4, 5}
print([Link](b)) # {3}
print([Link](b)) # {1, 2}
print(a.symmetric_difference(b)) # {1, 2, 4, 5}
print({1,2}.issubset({1,2,3})) # True
print({1,2,3}.issuperset({2,3})) # True
print({1,2}.isdisjoint({3,4})) # True
c = [Link]()
print(c) # {1, 2, 3}
[Link]()
print(a) # set()
Dictionaries (dict)
d = {"name": "Alice", "age": 25}
print([Link]("name")) # Alice
print([Link]("city", "NA")) # NA
print([Link]()) # dict_keys(['name', 'age'])
print([Link]()) # dict_values(['Alice', 25])
print([Link]()) # dict_items([('name','Alice'),('age',25)])
[Link]({"city": "Paris"})
print(d) # {'name': 'Alice', 'age': 25, 'city': 'Paris'}
d["age"] = 30
print(d) # {'name': 'Alice', 'age': 30, 'city': 'Paris'}
print([Link]("city")) # Paris
print([Link]()) # removes last item
print([Link]("gender", "female")) # female
print(d) # {'name': 'Alice', 'gender':'female'}
copy_d = [Link]()
print(copy_d) # {'name': 'Alice', 'gender':'female'}
[Link]()
print(d) # {}

Booleans (bool)
print(True and False) # False
print(True or False) # True
print(not True) # False
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("hi")) # True

NoneType
x = None
print(x) # None
print(type(x)) # <class 'NoneType'>
def f():
return
print(f()) # None
Common Built-ins
lst = [10, 20, 30]
print(len(lst)) #3
print(type(lst)) # <class 'list'>
print(isinstance(lst, list))# True
print(sorted([3, 1, 2])) # [1, 2, 3]
for i, val in enumerate(["a", "b", "c"]):
print(i, val) # 0 a, 1 b, 2 c
for a, b in zip([1, 2], ["x", "y"]):
print(a, b) # (1,'x'), (2,'y')

You might also like