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

Data Types in Python

The document provides an overview of data types in Python, including int, float, str, bool, list, tuple, dict, and set, along with examples for each. It also outlines various types of operators such as arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators, with examples of their usage. Additionally, it discusses type conversion functions, common exceptions in Python, and includes practice questions for further understanding.

Uploaded by

satya.apm01
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)
1 views4 pages

Data Types in Python

The document provides an overview of data types in Python, including int, float, str, bool, list, tuple, dict, and set, along with examples for each. It also outlines various types of operators such as arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators, with examples of their usage. Additionally, it discusses type conversion functions, common exceptions in Python, and includes practice questions for further understanding.

Uploaded by

satya.apm01
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

Data Types in Python

Python provides several built-in data types to store values of different kinds:
int: Integer values (e.g., x = 7)

float: Floating-point (decimal) numbers (e.g., y = 3.14)

str: Strings or sequences of characters (e.g., name = "Alice")

bool: Boolean values (True or False)


list: Ordered collection of elements (e.g., nums = )
tuple: Immutable ordered sequence (e.g., point = (4, 5))

dict: Key-value pairs (e.g., person = {"name": "Bob", "age": 25})

set: Unordered collection of unique elements (e.g., colors = {"red", "blue"})

Examples:

a = 10 # int
b = 3.5 # float
c = "Hello" # str
d = True # bool
e = [1, 2, 3] # list
f = (4, 5) # tuple
g = {"x": 1, "y": 2} # dict
h = {"apple", "banana"} # set

Types of Operators in Python


Operators are special symbols used to perform operations:
Arithmetic Operators: +, -, *, /, //, %, **
Example: 5 + 3 = 8

Comparison (Relational) Operators: ==, !=, >, <, >=, <=


Example: x > y

Assignment Operators: =, +=, -=, *=, /=


Example: x += 2

Logical Operators: and, or, not


Example: a and b
Bitwise Operators: &, |, ^, ~, <<, >>
Example: a | b

Membership Operators: in, not in

Example: 'a' in 'cat'

Identity Operators: is, is not

Example: x is y

Example:

x = 5
y = 3

print(x + y) # 8
print(x > y) # True
print(x and y) # 3 (non-zero is True, so returns y)
print(x is y) # False
print(x in [1, 2, 5]) # True

Conversion Functions (Type Casting)


Type conversion allows you to change the type of a value:
int(value): Converts value to integer.
float(value): Converts value to float.
str(value): Converts value to string.
list(value): Converts value to list, if possible.
tuple(value): Converts value to tuple.
dict(list_of_tuples): Converts list of tuples to a dictionary.
set(value): Converts value to set.

Examples:

num = "10"
int_num = int(num) # 10 (string to int)
str_num = str(25) # "25" (int to str)
flt = float("3.14") # 3.14 (string to float)
lst = list("hello") # ['h', 'e', 'l', 'l', 'o']
tpl = tuple([1, 2, 3]) # (1, 2, 3)
Exceptional Errors (Exceptions) in Python
Python uses exceptions to handle errors gracefully.

Common Exceptions:
ZeroDivisionError: Division by zero.
Example: 1 / 0

TypeError: Incompatible types used.


Example: "abc" + 5

ValueError: Value not appropriate type.


Example: int("abc")
IndexError: Index out of bounds.
Example: lst where lst has fewer than 11 elements.
KeyError: Key not found in dictionary.
Example: dict['x'] if 'x' not in dict.
NameError: Variable not defined.
Example: print(undeclared_var)

Handling Exceptions:
You can use try, except blocks:

try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero!")

Practice Questions
1. Type Identification
Write a Python script to input a value and print its type.
2. Operator Use
Given a = 12 and b = 7, calculate and print their sum, difference, product, and check if a is
greater than b.
3. Type Conversion
Convert the float value num = 8.675 to integer and string, then print them.
4. Exception Handling
Write code that handles division by zero using try and except.
5. Error Detection
What type of error is raised by my_list if my_list only contains 10 elements? Explain.
Feel free to clarify or request more examples on any topic above!

You might also like