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