0% found this document useful (0 votes)
11 views10 pages

Python String and Data Type Methods

The document provides an overview of various Python concepts including string methods, booleans, arithmetic, comparison, logical, assignment, bitwise, and membership operators. It also covers lists, their creation, modification, and usage, along with examples for each concept. Additionally, it briefly mentions tuples, sets, and dictionaries without detailed explanations.

Uploaded by

aliya.pathan0505
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)
11 views10 pages

Python String and Data Type Methods

The document provides an overview of various Python concepts including string methods, booleans, arithmetic, comparison, logical, assignment, bitwise, and membership operators. It also covers lists, their creation, modification, and usage, along with examples for each concept. Additionally, it briefly mentions tuples, sets, and dictionaries without detailed explanations.

Uploaded by

aliya.pathan0505
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

Python 2

07 November 2024 13:08

String methods in python

Name = "aliya"
Uname = [Link]()
Print(uname)

Booleans
• Booleans represent as "true" or "false"
• In programming, we use to get a need to know expression is true of false. We
can evaluate any expression in python and get output as either true or false.

Bool()
• Bool() function allows you to evaluate a value and return true or false
• Any string will return true, except empty string("")
• Any number return true, except 0.
• Any list, tuples, set return true, except empty ones [],{},().

Arithmetic Operators:
• Addition (+): Adds two operands. Example: a + b
• Subtraction (-): Subtracts the second operand from the first. Example: a - b
• Multiplication (*): Multiplies two operands. Example: a * b
• Division (/): Divides the first operand by the second. Example: a / b
• Modulus (%): Returns the remainder of the division of two operands. Example: a % b
• Exponentiation (**): Raises the first operand to the power of the second. Example: a ** b
• Floor Division (//): Performs floor division, which returns the largest possible integer. Example: a // b
2. Comparison Operators:
• Equal (==): Checks if the values of two operands are equal. Example: a == b
• Not Equal (!=): Checks if the values of two operands are not equal. Example: a != b
• Greater Than (>): Checks if the value of the left operand is greater than the right. Example: a > b
• Less Than (<): Checks if the value of the left operand is less than the right. Example: a < b
• Greater Than or Equal To (>=): Checks if the value of the left operand is greater than or equal to the right. Example: a >= b
• Less Than or Equal To (<=): Checks if the value of the left operand is less than or equal to the right. Example: a <= b
3. Logical Operators:
• AND (and): Returns True if both operands are true. Example: a and b
• OR (or): Returns True if at least one of the operands is true. Example: a or b
• NOT (not): Reverses the logical state of the operand. Example: not a
4. Assignment Operators:
• Assignment (=): Assigns a value to a variable. Example: a = b
• Add and Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand. Example: a += b
• Subtract and Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
Example: a -= b
• Multiply and Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
Example: a *= b
• Divide and Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand. Example: a /
=b

PYTHON Page 1
• Multiply and Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
Example: a *= b
• Divide and Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand. Example: a /
=b
• Modulus and Assignment (%=): Takes modulus using two operands and assigns the result to the left operand. Example: a %= b
• Exponent and Assignment (**=): Performs exponential (power) calculation on operators and assigns the value to the left
operand. Example: a **= b
• Floor Division and Assignment (//=): Performs floor division on operators and assigns the value to the left operand. Example: a //
=b
5. Bitwise Operators:
• AND (&): Performs bitwise AND on two operands. Example: a & b
• OR (|): Performs bitwise OR on two operands. Example: a | b
• XOR (^): Performs bitwise XOR on two operands. Example: a ^ b
• NOT (~): Performs bitwise NOT on the operand. Example: ~a
• Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand. Example: a
<< b
• Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
Example: a >> b

Membership operators in Python are used to test whether a value or variable is found in a sequence (such as a string, list, tuple, set, or
dictionary). There are two main membership operators:
1. in Operator
• Usage: Returns True if the specified value is present in the sequence.
• Example:
python
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # Output: True
2. not in Operator
• Usage: Returns True if the specified value is not present in the sequence.
• Example:
python
fruits = ["apple", "banana", "cherry"]
print("grape" not in fruits) # Output: True

Bitwise operators in Python are used to perform bit-level operations on integers. They work directly on the binary representations of
the numbers. Here’s a rundown of the bitwise operators available in Python:
1. Bitwise AND (&)
• Usage: Each bit of the output is 1 if the corresponding bits of both operands are 1, otherwise it is 0.
• Example:
python
a = 5 # binary: 0101
b = 3 # binary: 0011
result = a & b # binary: 0001 (decimal: 1)
2. Bitwise OR (|)
• Usage: Each bit of the output is 1 if at least one of the corresponding bits of either operand is 1.
• Example:
python
a = 5 # binary: 0101
b = 3 # binary: 0011
result = a | b # binary: 0111 (decimal: 7)
3. Bitwise XOR (^)
• Usage: Each bit of the output is 1 if the corresponding bits of the operands are different.
• Example:
python
a = 5 # binary: 0101
b = 3 # binary: 0011
result = a ^ b # binary: 0110 (decimal: 6)
4. Bitwise NOT (~)
• Usage: Inverts all the bits of the operand.
• Example:

PYTHON Page 2
• Example:
python
a = 5 # binary: 0101
result = ~a # binary: ...1010 (in a signed 32-bit system, it would be -6)
5. Bitwise Left Shift (<<)
• Usage: Shifts the bits of the first operand to the left by the number of positions specified by the second operand. Each shift to the
left multiplies the number by 2.
• Example:
python
a = 5 # binary: 0101
result = a << 1 # binary: 1010 (decimal: 10)
6. Bitwise Right Shift (>>)
• Usage: Shifts the bits of the first operand to the right by the number of positions specified by the second operand. Each shift to
the right divides the number by 2.
• Example:
python
a = 5 # binary: 0101
result = a >> 1 # binary: 0010 (decimal: 2)

Lists
• Lists are used to store multiple items in single variable
• Lists of one of 4 built in data types in python used to store multiple
items(collection of data).
• List items are ordered, changeable, and allow duplicates values.

Creating a List
You can create a list by placing all items (elements) inside square brackets [], separated by commas:
python
my_list = [1, 2, 3, 4, 5]
Accessing Elements
You can access elements by their index (starting from 0):
python
print(my_list[0]) # Output: 1
print(my_list[2]) # Output: 3
Modifying Elements
You can change the value of a specific element:
python
my_list[1] = 10
print(my_list) # Output: [1, 10, 3, 4, 5]
Adding Elements
You can add elements using the append() method or insert() method:
python
my_list.append(6)
print(my_list) # Output: [1, 10, 3, 4, 5, 6]
my_list.insert(2, 99)
print(my_list) # Output: [1, 10, 99, 3, 4, 5, 6]
Removing Elements
You can remove elements using remove(), pop(), or del:
python
my_list.remove(10)
print(my_list) # Output: [1, 99, 3, 4, 5, 6]
my_list.pop(2)
print(my_list) # Output: [1, 99, 4, 5, 6]
del my_list[1]
print(my_list) # Output: [1, 4, 5, 6]
List Slicing
You can slice lists to get a subset:
python
sub_list = my_list[1:4]
print(sub_list) # Output: [4, 5, 6]
Looping Through a List
You can loop through a list using a for loop:
python

PYTHON Page 3
print(sub_list) # Output: [4, 5, 6]
Looping Through a List
You can loop through a list using a for loop:
python
for item in my_list:
print(item)
List Comprehensions
You can create lists using list comprehensions for more concise code:
python
squared_list = [x**2 for x in range(1, 6)]
print(squared_list) # Output: [1, 4, 9, 16, 25]

PYTHON Page 4
Tuples

PYTHON Page 5
PYTHON Page 6
Sets

PYTHON Page 7
Dictionary

PYTHON Page 8
PYTHON Page 9
PYTHON Page 10

You might also like