Fix formatting

This commit is contained in:
CSnap 2023-03-22 16:19:42 +00:00
parent 865dc972ad
commit 1b01d76e4b
1 changed files with 111 additions and 33 deletions

144
python.md
View File

@ -3,82 +3,147 @@
## Variables and Data Types
### Variables
# Declaring a variable
x = 5
# Reassigning a variable
x = 10
### Data Types
# Integer
#### Declaring a variable
```
x = 5
# Float
```
#### Reassigning a variable
```
x = 10
```
### Data Types
#### Integer
```
x = 5
```
#### Float
```
x = 5.0
# Boolean
```
#### Boolean
```
x = True
# String
```
#### String
```
x = "Hello, World!"
```
## Operators
# Arithmetic Operators
#### Arithmetic Operators
```
+, -, *, /, %, **
# Comparison Operators
```
#### Comparison Operators
```
==, !=, >, <, >=, <=
# Logical Operators
```
#### Logical Operators
```
and, or, not
```
## Control Structures
### Conditional Statements
# If statement
#### If statement
```
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
```
### Loops
# For loop
#### For loop
```
for i in range(0, 5):
print(i)
# While loop
```
#### While loop
```
i = 0
while i < 5:
print(i)
i += 1
```
## Functions
# Defining a function
#### Defining a function
```
def my_function(param1, param2):
print(param1 + param2)
# Calling a function
```
#### Calling a function
```
my_function(5, 10)
```
## Lists
# Declaring a list
#### Declaring a list
```
my_list = [1, 2, 3, 4, 5]
# Accessing elements of a list
```
#### Accessing elements of a list
```
my_list[0] # returns 1
my_list[-1] # returns 5
# Slicing a list
```
#### Slicing a list
```
my_list[1:3] # returns [2, 3]
# Appending to a list
```
#### Appending to a list
```
my_list.append(6)
# Removing from a list
```
#### Removing from a list
```
my_list.remove(2)
```
## Dictionaries
# Declaring a dictionary
#### Declaring a dictionary
```
my_dict = {"key1": "value1", "key2": "value2"}
# Accessing elements of a dictionary
```
#### Accessing elements of a dictionary
```
my_dict["key1"] # returns "value1"
# Adding to a dictionary
```
#### Adding to a dictionary
```
my_dict["key3"] = "value3"
# Removing from a dictionary
```
#### Removing from a dictionary
```
del my_dict["key2"]
```
## Classes
# Declaring a class
#### Declaring a class
```
class MyClass:
def __init__(self, param1, param2):
self.param1 = param1
@ -86,17 +151,30 @@ class MyClass:
def my_method(self):
print(self.param1 + self.param2)
# Creating an instance of a class
```
#### Creating an instance of a class
```
my_instance = MyClass(5, 10)
# Calling a method of a class
```
#### Calling a method of a class
```
my_instance.my_method()
```
## Exceptions
# Catching an exception
#### Catching an exception
```
try:
# some code that may raise an exception
except ExceptionType:
# do something to handle the exception
# Raising an exception
raise ExceptionType("Error message")
```
#### Raising an exception
```
raise ExceptionType("Error message")
```