Python Lecture II1235
Python Lecture II1235
Variable
Data/Values can be stored in temporary storage areas called variables. Each variable is associated with a data type
for example:
var1=”John”
type(var1)
Print(var1)
Python Tokens
1. Keywords
2. Identifiers
3. Literals
4. Operators
Rule
Operator
In Python, operators are special symbols or keywords used to perform operations on variables and values. Python
supports various types of operators, each designed for different kinds of operations. Here's a breakdown of the main
categories of operators in Python:
1. Arithmetic Operators
2. Comparison Operators
These operators compare two values and return a Boolean (True or False).
3. Assignment Operators
4. Logical Operators
Examples:
Arithmetic Operator Example
a = 10
b=3
type(a)
type(b)
print(a + b) # Output: 13
x=5
y = 10
x = True
y = False
type(x)
type(y)
Python has several built-in data types that are used to store and manipulate data. These data types can be broadly
categorized into various types, including numeric types, sequence types, set types, mapping types, and more. Here's
an overview of the key data types in Python:
1. Numeric Types
int (Integer): Represents whole numbers, positive or negative, without decimals. There is no limit to the
size of an integer in Python.
x = 10
y = -3
z = 1234567890123456789
float (Floating Point): Represents real numbers with a decimal point. It can also represent numbers in
scientific notation.
a = 3.14
b = -0.001
complex (Complex Numbers): Represents complex numbers, which are written in the form a + bj,
where a is the real part and b is the imaginary part.
num = 2 + 3j
2. Sequence Types
str (String): Represents a sequence of characters enclosed in single, double, or triple quotes. Strings are
immutable.
s1 = 'Hello'
s2 = "World"
s3 = '''This is a
multi-line string'''
list: Represents an ordered, mutable sequence of elements, which can be of different types.
my_list = [1, 2, 3, 'a', 'b', 'c']
my_list[0] = 10 # Modifying an element
tuple: Represents an ordered, immutable sequence of elements, which can be of different types.
my_tuple = (1, 2, 3, 'a', 'b', 'c')
range: Represents an immutable sequence of numbers, commonly used in loops
r = range(0, 10, 2) # Start at 0, up to 10, with a step of 2
3. Mapping Type
dict (Dictionary): Represents a collection of key-value pairs, where keys are unique and are used to
retrieve the corresponding values. Dictionaries are mutable.
4. Set Types
frozenset: Represents an immutable set, where elements cannot be changed once assigned.
my_frozenset = frozenset([1, 2, 3, 4, 5])
5. Boolean Type
bool: Represents one of two values: True or False. Booleans are a subclass of integers.
is_active = True
is_deleted = False