0% found this document useful (0 votes)
0 views

Python Lecture II1235

The document provides an overview of Python programming concepts, including variables, tokens, operators, and data types. It explains the different types of operators (arithmetic, comparison, assignment, and logical) and categorizes Python's built-in data types such as numeric types, sequence types, mapping types, set types, and boolean types. Examples are included to illustrate the use of these concepts in Python code.

Uploaded by

bonedobnd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Lecture II1235

The document provides an overview of Python programming concepts, including variables, tokens, operators, and data types. It explains the different types of operators (arithmetic, comparison, assignment, and logical) and categorizes Python's built-in data types such as numeric types, sequence types, mapping types, set types, and boolean types. Examples are included to illustrate the use of these concepts in Python code.

Uploaded by

bonedobnd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Lecture II

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

Smallest meaningful components in a program

1. Keywords
2. Identifiers
3. Literals
4. Operators

Keywords: Keywords are special reserved words

False Class Finally Is Return


None Def For Lambda Try
True Continue From Nonlocal While
And Del Global Not With
As Elif If Or Yield

Identifiers: Identifiers are names used for variables, functions or objects

Rule

1. No special character except _(underscore)


2. Identifiers are case sensitive
3. First letter cannot be digit

Literals: Literals are constants in Python

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

These operators are used to perform mathematical operations.

Operator Description Example


Operator Description Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus (remainder) x%y
** Exponentiation x ** y
// Floor Division x // y

2. Comparison Operators

These operators compare two values and return a Boolean (True or False).

Operator Description Example


== Equal to x == y
!= Not equal to x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

3. Assignment Operators

These operators are used to assign values to variables.

Operator Description Example


= Assignment x=y
+= Add and assign x += y
-= Subtract and assign x -= y
*= Multiply and assign x *= y
/= Divide and assign x /= y
%= Modulus and assign x %= y
**= Exponent and assign x **= y
//= Floor divide and assign x //= y

4. Logical Operators

These operators are used to perform logical operations.

Operator Description Example


and Logical AND x and y
or Logical OR x or y
not Logical NOT not x

Examples:
Arithmetic Operator Example

a = 10

b=3

type(a)

type(b)

print(a + b) # Output: 13

print(a / b) # Output: 3.3333333333333335

print(a // b) # Output: 3 (floor division)

Comparision Operator Example

x=5

y = 10

print(x > y) # Output: False

print(x <= y) # Output: True

Logical Operator Example

x = True

y = False

type(x)

type(y)

print(x and y) # Output: False

print(x or y) # Output: True

print(not x) # Output: False

Data Types In Python

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

c = 2.5e2 # 2.5 * 10^2 = 250.0

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

my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}

my_dict['age'] = 26 # Modifying a value

4. Set Types

 set: Represents an unordered collection of unique elements. Sets are mutable.


my_set = {1, 2, 3, 4, 5}

my_set.add(6) # Adding an element

 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

You might also like