AI Holiday Homework
AI Holiday Homework
Numbers
Python's numeric types include int, float, and complex.
A float represents real numbers and is written with a decimal point. For
instance, pi = 3.14. It's used in mathematical and scientific calculations
where precision matters.
Booleans represent logical values and are part of the bool type.
There are only two boolean values: True and False. They're used in conditions,
loops, and logical expressions. For example:
x = 10
y = 5
result = x > y
print(result) # True
Sequence
Python sequences include str, list, and tuple.
A string (str) is a sequence of characters, enclosed in single or double quotes.
Example: "Hello, world!". Strings are immutable and support powerful operations
like slicing and string formatting.
A list is an ordered, mutable collection. Lists are created using square brackets,
e.g., [1, 2, 3] or ["apple", "banana"]. You can add, remove, or change elements
easily.
A tuple is similar to a list but immutable, meaning once it’s created, it can’t be
changed. Defined using parentheses like (1, 2, 3), tuples are ideal for storing fixed
data collections like coordinates or config values.
Mapping
Python’s main mapping type is the dict.
A dictionary stores data as key-value pairs, like {"name": "Sam", "age": 16}. Keys
must be unique and immutable, while values can be any data type. Dictionaries are
incredibly useful for representing structured data and can be nested, updated, and
accessed quickly using keys.
Sets
Python has two primary set types: set and frozenset.
A set is an unordered collection of unique items. It’s created with curly braces: {1,
2, 3}. Sets are mutable—you can add or remove items—and they support
operations like union and intersection.
Bitwise Operators work on bits and perform bit-by-bit operations. These include &
(AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift). For example, 5 &
3 performs a bitwise AND on the binary representations of 5 ( 0101) and 3 (0011),
resulting in 0001, which is 1.
Membership Operators are used to test whether a value is part of a sequence like a
list, tuple, or string. The keywords are in and not in. For example, 'a' in 'apple'
returns True, while 3 not in [1, 2, 4] also returns True.
Identity Operators check whether two variables refer to the same object in memory.
These are is and is not. For instance, a is b is True if both a and b point to the
same object, not just equal values. This is often used when dealing with objects or
comparing None.