Python
Notion Link: https://wise-monitor-956.notion.site/Python-10c1d1e7e13c800cb943d06973f15a4c
String
Creation:
Example:
my_string = 'Hello, World!'ormy_string = "Hello, World!"
Accessing Characters:
You can access individual characters within a string using indexing, starting from 0.
Example:
print(my_string[0])would output 'H'.
String Concatenation:
You can concatenate (join) two or more strings using the
+operator.Example:
greeting = 'Hello' + ' ' + 'World!'would result in 'Hello World!'.
String Length:
The
len()function can be used to determine the length (number of characters) of a string.Example:
print(len(my_string))would output the length of the string.
String Slicing:
You can extract a substring from a string using slicing, specifying the start and end indices.
Example:
substring = my_string[7:12]would extract the substring 'World'.
print(x[:9]) # will print from 0 to 9
print(x[9:]) # print from 9 to the last char
print(x[0:5]) # print from 0 to 5String Methods:
Python provides various built-in methods to manipulate and transform strings. Examples include
upper(),lower(),strip(),split(),replace(), and more.Example:
print(my_string.upper())would output 'HELLO, WORLD!'.
String Formatting:
String formatting allows you to embed values within a string. This can be done using the
%operator or theformat()method.You can also use f strings as shown in the example below
To check if this char is found
Arithmetic Operations
Math Operators:
Addition (
+): Adds two numbers.Subtraction (``): Subtracts one number from another.
Multiplication (``): Multiplies two numbers.
Division (
/): Divides one number by another.Integer Division (
//): Performs division and returns the quotient as an integer (rounds down) .Modulo (
%): Returns the remainder of division.Exponentiation (
*): Raises a number to a power.
u can use (int(5/2)) to return the int value
Math Functions in the math Module:
math.sqrt(x): Calculates the square root ofx.math.pow(x, y): Raisesxto the power ofy.math.exp(x): Calculates the exponential value ofx(e^x).math.log(x): Calculates the natural logarithm ofx(base e).math.log10(x): Calculates the logarithm ofxto base 10.math.sin(x),math.cos(x),math.tan(x): Calculate the sine, cosine, and tangent ofx, respectively (wherexis in radians).math.degrees(x): Convertsxfrom radians to degrees.math.radians(x): Convertsxfrom degrees to radians.
but you must import math module first
Variables and Basic Methods
Functions
Some function for string upper() , lower() , title()
→ In python we can’t concatenate intwith string we can transfer it to string to use concatenation by this way str(var)
→ python don’t have Postfix and Prefix
User Input
We use input() to take value from user
Functions
To write Function def NameOfFunction():
→ num1, num2 in function called parameter
Condition
if condition have 3 type if, elif, else
Loop
Use For loop
range → we use it with int number and have (start,end,step)
Use While loop
→ we use while when we can’t know the number of steps we need it so use it with condition like check the correct password
List
Have brackets [] - everything inside is called an item - is called array in another programming language like cpp,php,etc
Some Function use with List
→ Combine list : concatenation with two list or more and store it in another list
Tuples
Immutable(can’t be changed) - ()
we can use it with color like red =(255,0,0)
Dictionary
Key/Value pairs { } -{key:value}
the value may be List but the key is unique
we can add new key to dictionary by this way
Dictionary functions
To print dictionary with loop
Socket
To use socket u must import this module import socket
Once you have a socket object, you can use various methods to establish connections, send data, and receive data. Here are some commonly used methods:
socket.connect(address): Establishes a connection to a remote address.socket.bind(address): Binds the socket to a specific address and port.socket.listen(backlog): Listens for incoming connections on a TCP socket.socket.accept(): Accepts an incoming connection and returns a new socket object for communication.socket.send(data): Sends data over the socket.socket.recv(buffer_size): Receives data from the socket.socket.settimeout(0.5): Set time for connection
Port Scanning For ip address
check if is port or domain
Security
we have many modules in python like os where it use to execute command on the system
requests send request to web page
GET Request
POST Request
Password brute force
Notes

Variable Best Practices
Best Practice for Variable Naming: snake_case
All letters are in lowercase.
Words are separated by underscores (
_).
Example of Snake Case:
Constants in All Uppercase:
For variables that represent constants (values that do not change), use ALL_UPPERCASE with underscores between words.
Example:
Private Variables with Leading Underscore:
If you intend for a variable to be used only within a module or class, prefix it with a single underscore (
_), indicating that it is meant to be "private."Example:
Summary of Best Practices:
**Use
snake_case**for variable names.Use meaningful and descriptive names.
Avoid reserved keywords.
Use ALL_UPPERCASE for constants.
Use a leading underscore (
_) for internal/private variables.Be consistent with naming conventions across your code.
Last updated
