Python Self Notes
Python Self Notes
Python offers a variety of built-in data structures and data types to organize and manipulate
data efficiently. Here's a breakdown of the most common ones with examples:
Data Structures &
Data Types: Data Types
Integers (int): Represents whole numbers, positive or negative, without decimals.
x = 10
y = 3.14
name = "Alice"
is_valid = True
Data Structures:
Linked lists: Linear data structure where elements are linked together.
Stacks and Queues: Implement specific data access patterns (LIFO and FIFO, respectively).
Choosing the right data structure and type is crucial for efficient programming. It depends on
the specific requirements of your application.
PYTHON - August - 2024 BATCH (MGM)
print("Hello,", name)
Indentation :
for i in range(3):
print("Hello")
print("Bye!")
a=1
print(a)
Note : Python automatically detected that the type of a must be int (an integer).
a="some text"
type(a)
Program Implementation :
In Python, the single equals (=) and double equals (==) serve different purposes:
PYTHON - August - 2024 BATCH (MGM)
= (Assignment operator):
This operator assigns a value to a variable. For example, x = 5 assigns the value 5 to the
variable x.
== (Equality operator):
This operator compares two values and checks if they are equal. It returns True if the values
are equal, and False otherwise. For example, x == 5 checks if the value of x is equal to 5.
Example :
else:
a="first"
b="second"
print(a+b)
print(str(1) + " plus " + str(3) + " is equal to " + str(4)) # slightly better
LOOPS :
PYTHON - August - 2024 BATCH (MGM)
i=1
i=i+1
s=0
for i in [0,1,2,3,4,5,6,7,8,9]:
s=s+i
IF - ELSE STATEMENT :
x=int(x)
if x >= 0:
a=x
else:
a=-x
SPLITS :
temperatures=[10,12,12,9,9,11,11]
print(f"On {day} it was {weather} and the temperature was {temperature} degrees celsius.")
PYTHON - August - 2024 BATCH (MGM)
OPERATIONS STRING :
1. Creating Strings
You can create strings using either single quotes (`'`) or double quotes (`"`).
2. Accessing Characters
You can access individual characters in a string using indexing (indices start at 0).
text = "Python"
print(text[0]) # Output: P
print(text[5]) # Output: n
3. Slicing Strings
4. String Length
text = "Hello"
print(len(text)) # Output: 5
5. Concatenation
str1 = "Hello"
str2 = "World"
8. Strip Whitespace
Remove leading and trailing whitespace using `strip()`, `lstrip()`, and `rstrip()`.
9. Replacing Substrings
words = text.split()
text = "apple,orange,banana"
fruits = text.split(",")
Use methods like `isdigit()`, `isalpha()`, and `isspace()` to check the content of strings.
text = "123"
text = "abc"
Use formatted string literals (f-strings) or the `format()` method for formatting.
name = "Alice"
age = 30
# Using f-strings
These are some of the fundamental string operations you'll use often in Python.
23 - 08 - 2024
Code :
a = 200
b = 33
if b > a:
elif a == b:
else:
Equals: a == b, Not Equals: a != b, Less than: a < b, Less than or equal to: a <= b,
i=1
while i < 6:
print(i)
i += 1
The break Statement: With the break statement we can stop the loop even if the while
condition is true
i=1
while i < 6:
print(i)
if (i == 3):
break
i += 1
The Continue Statement: With the continue statement we can stop the current iteration,
and continue with the next
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
The Else Statement: With the else statement we can run a block of code once when the
condition no longer is true
i=1
while i < 6:
print(i)
i += 1
else:
FOR LOOPS
PYTHON - August - 2024 BATCH (MGM)
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a
set, or a string).
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set
etc.
The for loop does not require an indexing variable to set beforehand.
Code :
for x in "grapes":
print(x)
The Break Statement : With the break statement we can stop the loop before it has looped
through all the items
Code :
for x in fruits:
print(x)
if x == "cherry":
break
With the continue statement we can stop the current iteration of the loop, and continue
with the next
Range Function : o loop through a set of code a specified number of times, we can use
the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
Code :
for x in range(20):
print(x)
print(x)
Else in For Loop : The else keyword in a for loop specifies a block of code to be executed
when the loop is finished
PYTHON - August - 2024 BATCH (MGM)
for x in range(6):
print(x)
else:
print("Finally finished!")
Nested Loop :
A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each
iteration of the "outer loop"
for x in adj:
for y in fruits:
print(x, y)
DICTIONARY
PYTHON - August - 2024 BATCH (MGM)