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

AI Holiday Homework

The document provides an overview of data types and operators in Python, highlighting five main data types: Numbers, Bool, Sequence, Mapping, and Sets. It also explains seven types of operators including Arithmetic, Relational, Assignment, Logical, Bitwise, Membership, and Identity operators. Each section includes definitions and examples to illustrate their usage in Python programming.

Uploaded by

Sarah Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

AI Holiday Homework

The document provides an overview of data types and operators in Python, highlighting five main data types: Numbers, Bool, Sequence, Mapping, and Sets. It also explains seven types of operators including Arithmetic, Relational, Assignment, Logical, Bitwise, Membership, and Identity operators. Each section includes definitions and examples to illustrate their usage in Python programming.

Uploaded by

Sarah Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AI Holiday Homework

Data Types and Operators in Python

i. Data Types available in Python


In Python, data types define the kind of value a variable can hold and the operations that
can be performed on that value. Every piece of data in a Python program—whether it’s a
number, text, list, or something more complex—has an associated data type. Python is a
dynamically typed language, meaning the interpreter automatically identifies the data type
based on the value assigned to a variable. There are five main data types in Python:

Numbers
Python's numeric types include int, float, and complex.

 An int is a whole number without a decimal point. It is used for counting or


indexing, e.g., x = 10.

 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.

 A complex number includes a real and an imaginary part and is written as a +


bj, such as z = 2 + 3j. These are used mostly in advanced math and
engineering applications.
Bool

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

Booleans are the result of comparison operations like 5 > 3 or a == b.

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.

A frozenset is the immutable version of a set. Once defined, it cannot be changed.


This makes it useful when you need a set as a dictionary key or to ensure the data
remains constant.

i. Data Types available in Python


In Python, operators are special symbols or keywords used to perform operations on
variables and values. They are essential for manipulating data, controlling program flow, and
evaluating conditions. Python offers a wide range of operators for arithmetic and logical
evaluations. There are seven main operator types in Python:

Arithmetic Operators are used to perform mathematical operations such as


addition, subtraction, multiplication, division, modulus, exponentiation, and floor
division. For example, a + b adds two values, a - b subtracts them, and a ** b
raises a to the power of b. These are fundamental for calculations in Python.
Relational Operators, also known as comparison operators, compare the values of
two operands and return a Boolean value. These include ==, !=, >, <, >=, and
<=. For instance, a > b will return True if a is greater than b; otherwise, it returns
False.

Assignment Operator assigns values to variables. The basic assignment operator is =,


but Python also provides shorthand operators like +=, -=, *=, and /=, which
combine assignment with an arithmetic operation. For example, a += 1 is the same
as a = a + 1.
Logical Operators are used to combine conditional statements. Python provides and,
or, and not. For instance, a > 2 and b < 5 returns True only if both conditions are
true. The not operator inverses the result, so not True evaluates to False.

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.

You might also like