Python has the following data types built-in by default, in these categories:
Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview None Type: NoneType
Example Data Type x = "Hello World" str x = 20 int x = 20.5 float x = 1j complex x = ["apple", "banana", "cherry"] list x = ("apple", "banana", "cherry") tuple x = range(6) range x = {"name" : "John", "age" : 36} dict x = {"apple", "banana", "cherry"} set x = frozenset({"apple", "banana", "cherry"}) frozenset x = True bool x = b"Hello" bytes x = bytearray(5) bytearray x = memoryview(bytes(5)) memoryview x = None NoneType
Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers: Example Import the random module, and display a random number between 1 and 9:
import random print(random.randrange(1, 10))
You can assign a multiline string to a variable by using three double quotes or three single quotes: Example: a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.""" print(a)
strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. remember that the first character has the position 0 and the last character has the position -1
a = "Hello, World!" print(a[1])
Since strings are arrays, we can loop through the characters in a string, with a for loop. for x in "banana": print(x)
To get the length of a string, use the len() function. To check if a certain phrase or character is present in a string, we can use the keyword "in" To check if a certain phrase or character is NOT present in a string, we can use the keyword "not in". txt = "The best things in life are free!" if "free" in txt: print("Yes, 'free' is present.")