initial commit
This commit is contained in:
151
dev/programming/Python Cheat Sheet(1).md
Normal file
151
dev/programming/Python Cheat Sheet(1).md
Normal file
@@ -0,0 +1,151 @@
|
||||
# Python Cheat Sheet
|
||||
|
||||
## 1. Variables, Data Types, and Basic Operations
|
||||
|
||||
Python has several fundamental data types, including integers (int), floating point numbers (float), and strings (str). Python is a dynamically typed language, which means you don't need to declare the data type of a variable when you define it.
|
||||
|
||||
```python
|
||||
a = 10 # Integer
|
||||
b = 3.14 # Float
|
||||
c = "Hello, World!" # String
|
||||
```
|
||||
|
||||
Operators allow you to perform operations on variables. Arithmetic, comparison, assignment, logical, and identity operators are some of the main types in Python.
|
||||
|
||||
```python
|
||||
a = 10
|
||||
b = 20
|
||||
sum = a + b # Addition
|
||||
difference = a - b # Subtraction
|
||||
#... remaining code ...
|
||||
```
|
||||
|
||||
## 2. Control Structures (Conditionals and Loops)
|
||||
|
||||
Python uses `if`, `elif`, and `else` for conditional statements. Loops in Python can be programmed using a `for` or `while` loop.
|
||||
|
||||
```python
|
||||
# If-else statement
|
||||
if a > b:
|
||||
print("a is greater than b")
|
||||
else:
|
||||
print("a is not greater than b")
|
||||
|
||||
# For loop
|
||||
for i in range(5):
|
||||
print(i)
|
||||
```
|
||||
|
||||
## 3. Functions
|
||||
|
||||
Functions in Python are defined using the `def` keyword. They are used to encapsulate a piece of code that performs a specific task.
|
||||
|
||||
```python
|
||||
def greet(name):
|
||||
print("Hello, " + name)
|
||||
|
||||
greet("Alice")
|
||||
```
|
||||
|
||||
## 4. Lists, Tuples, Sets, and Dictionaries
|
||||
|
||||
Python has several types of compound data structures that can hold multiple values, including lists, tuples, sets, and dictionaries.
|
||||
|
||||
```python
|
||||
# Lists
|
||||
my_list = [1, 2, 3, 4, 5]
|
||||
|
||||
# Dictionaries
|
||||
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
|
||||
```
|
||||
|
||||
## 5. File Handling
|
||||
|
||||
Python has built-in functions for reading and writing files. `open()` function is used to open a file. It returns a file object and is most commonly used with two arguments: `open(filename, mode)`.
|
||||
|
||||
```python
|
||||
# Reading a file
|
||||
file = open('file.txt', 'r')
|
||||
content = file.read()
|
||||
file.close()
|
||||
|
||||
# Writing to a file
|
||||
file = open('file.txt', 'w')
|
||||
file.write("Hello, World!")
|
||||
file.close()
|
||||
```
|
||||
|
||||
## 6. Exception Handling
|
||||
|
||||
Python uses `try` and `except` blocks to catch and handle exceptions. Python executes code following the `try` statement as a "normal" part of the program. The code that follows the `except` statement is the program's response to any exceptions in the preceding try clause.
|
||||
|
||||
```python
|
||||
try:
|
||||
# Code that may raise an exception
|
||||
x = 1 / 0
|
||||
except ZeroDivisionError:
|
||||
print("You can't divide by zero!")
|
||||
```
|
||||
|
||||
## 7. Classes and Objects
|
||||
|
||||
Python is an object-oriented programming language. You define classes to create your own types.
|
||||
|
||||
```python
|
||||
class MyClass:
|
||||
x = 5
|
||||
|
||||
p1 = MyClass() # Create an object of MyClass
|
||||
print(p1.x) # Access the properties of MyClass using the object
|
||||
```
|
||||
|
||||
## 8. Modules and Packages
|
||||
|
||||
Modules in Python are simply Python files with a .py extension, which implement a set of functions. A package is a way of organizing related modules into a single directory hierarchy.
|
||||
|
||||
```python
|
||||
# Importing a module
|
||||
import math
|
||||
print(math.sqrt(16)) # Use a function from the math module
|
||||
|
||||
# Importing
|
||||
ChatGPT
|
||||
a package
|
||||
import os.path
|
||||
print(os.path.join('/a', 'b', 'c')) # Use a function from the os.path package
|
||||
```
|
||||
|
||||
## 9. Working with JSON
|
||||
|
||||
Python has a built-in package called `json`, which can be used to work with JSON data.
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
# Some JSON:
|
||||
x = '{"name": "John", "age": 30, "city": "New York"}'
|
||||
|
||||
# Parse x:
|
||||
y = json.loads(x)
|
||||
|
||||
# The result is a Python dictionary:
|
||||
print(y["age"])
|
||||
```
|
||||
|
||||
## 10. Making HTTP requests
|
||||
|
||||
The `requests` library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.
|
||||
|
||||
```python
|
||||
import requests
|
||||
|
||||
response = requests.get('https://www.example.com')
|
||||
|
||||
# Print the status code
|
||||
print(response.status_code)
|
||||
|
||||
# Print the content
|
||||
print(response.text)
|
||||
```
|
||||
|
||||
These are the basics to get you started with Python! Each of these topics has more depth to explore as you become more comfortable with the language.
|
||||
151
dev/programming/Python Cheat Sheet.md
Normal file
151
dev/programming/Python Cheat Sheet.md
Normal file
@@ -0,0 +1,151 @@
|
||||
# Python Cheat Sheet
|
||||
|
||||
## 1. Variables, Data Types, and Basic Operations
|
||||
|
||||
Python has several fundamental data types, including integers (int), floating point numbers (float), and strings (str). Python is a dynamically typed language, which means you don't need to declare the data type of a variable when you define it.
|
||||
|
||||
```python
|
||||
a = 10 # Integer
|
||||
b = 3.14 # Float
|
||||
c = "Hello, World!" # String
|
||||
```
|
||||
|
||||
Operators allow you to perform operations on variables. Arithmetic, comparison, assignment, logical, and identity operators are some of the main types in Python.
|
||||
|
||||
```python
|
||||
a = 10
|
||||
b = 20
|
||||
sum = a + b # Addition
|
||||
difference = a - b # Subtraction
|
||||
#... remaining code ...
|
||||
```
|
||||
|
||||
## 2. Control Structures (Conditionals and Loops)
|
||||
|
||||
Python uses `if`, `elif`, and `else` for conditional statements. Loops in Python can be programmed using a `for` or `while` loop.
|
||||
|
||||
```python
|
||||
# If-else statement
|
||||
if a > b:
|
||||
print("a is greater than b")
|
||||
else:
|
||||
print("a is not greater than b")
|
||||
|
||||
# For loop
|
||||
for i in range(5):
|
||||
print(i)
|
||||
```
|
||||
|
||||
## 3. Functions
|
||||
|
||||
Functions in Python are defined using the `def` keyword. They are used to encapsulate a piece of code that performs a specific task.
|
||||
|
||||
```python
|
||||
def greet(name):
|
||||
print("Hello, " + name)
|
||||
|
||||
greet("Alice")
|
||||
```
|
||||
|
||||
## 4. Lists, Tuples, Sets, and Dictionaries
|
||||
|
||||
Python has several types of compound data structures that can hold multiple values, including lists, tuples, sets, and dictionaries.
|
||||
|
||||
```python
|
||||
# Lists
|
||||
my_list = [1, 2, 3, 4, 5]
|
||||
|
||||
# Dictionaries
|
||||
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
|
||||
```
|
||||
|
||||
## 5. File Handling
|
||||
|
||||
Python has built-in functions for reading and writing files. `open()` function is used to open a file. It returns a file object and is most commonly used with two arguments: `open(filename, mode)`.
|
||||
|
||||
```python
|
||||
# Reading a file
|
||||
file = open('file.txt', 'r')
|
||||
content = file.read()
|
||||
file.close()
|
||||
|
||||
# Writing to a file
|
||||
file = open('file.txt', 'w')
|
||||
file.write("Hello, World!")
|
||||
file.close()
|
||||
```
|
||||
|
||||
## 6. Exception Handling
|
||||
|
||||
Python uses `try` and `except` blocks to catch and handle exceptions. Python executes code following the `try` statement as a "normal" part of the program. The code that follows the `except` statement is the program's response to any exceptions in the preceding try clause.
|
||||
|
||||
```python
|
||||
try:
|
||||
# Code that may raise an exception
|
||||
x = 1 / 0
|
||||
except ZeroDivisionError:
|
||||
print("You can't divide by zero!")
|
||||
```
|
||||
|
||||
## 7. Classes and Objects
|
||||
|
||||
Python is an object-oriented programming language. You define classes to create your own types.
|
||||
|
||||
```python
|
||||
class MyClass:
|
||||
x = 5
|
||||
|
||||
p1 = MyClass() # Create an object of MyClass
|
||||
print(p1.x) # Access the properties of MyClass using the object
|
||||
```
|
||||
|
||||
## 8. Modules and Packages
|
||||
|
||||
Modules in Python are simply Python files with a .py extension, which implement a set of functions. A package is a way of organizing related modules into a single directory hierarchy.
|
||||
|
||||
```python
|
||||
# Importing a module
|
||||
import math
|
||||
print(math.sqrt(16)) # Use a function from the math module
|
||||
|
||||
# Importing
|
||||
ChatGPT
|
||||
a package
|
||||
import os.path
|
||||
print(os.path.join('/a', 'b', 'c')) # Use a function from the os.path package
|
||||
```
|
||||
|
||||
## 9. Working with JSON
|
||||
|
||||
Python has a built-in package called `json`, which can be used to work with JSON data.
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
# Some JSON:
|
||||
x = '{"name": "John", "age": 30, "city": "New York"}'
|
||||
|
||||
# Parse x:
|
||||
y = json.loads(x)
|
||||
|
||||
# The result is a Python dictionary:
|
||||
print(y["age"])
|
||||
```
|
||||
|
||||
## 10. Making HTTP requests
|
||||
|
||||
The `requests` library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.
|
||||
|
||||
```python
|
||||
import requests
|
||||
|
||||
response = requests.get('https://www.example.com')
|
||||
|
||||
# Print the status code
|
||||
print(response.status_code)
|
||||
|
||||
# Print the content
|
||||
print(response.text)
|
||||
```
|
||||
|
||||
These are the basics to get you started with Python! Each of these topics has more depth to explore as you become more comfortable with the language.
|
||||
92
dev/programming/pseudocode.md
Normal file
92
dev/programming/pseudocode.md
Normal file
@@ -0,0 +1,92 @@
|
||||
## Pseudocode Cheat Sheet
|
||||
|
||||
**Pseudocode** is a simple way of expressing programming logic in plain language. It's a valuable tool to outline algorithms, aiding in understanding and problem-solving without using specific programming language syntax.
|
||||
|
||||
**Why Use Pseudocode?**
|
||||
|
||||
Pseudocode can be employed in various situations, especially when planning and brainstorming programming solutions. Its uses extend to:
|
||||
|
||||
- **Problem Understanding:** It simplifies problem comprehension by breaking it down into manageable steps.
|
||||
- **Solution Design:** It aids in creating efficient solutions by enabling more precise visualization of the logical flow.
|
||||
- **Coding:** It provides a high-level overview of the code to be written, simplifying the coding process.
|
||||
- **Debugging:** It helps identify logic errors before actual code is written, simplifying debugging.
|
||||
- **Collaboration:** It aids in explaining the logic to others, especially those unfamiliar with specific programming languages.
|
||||
- **Maintainability:** It serves as a blueprint of the logic for future reference, enhancing code maintainability.
|
||||
|
||||
**Guidelines for Writing Pseudocode**
|
||||
|
||||
- **Natural Language:** Use plain English or your preferred natural language. If a part of the pseudocode is complex, consider adding comments for better clarity.
|
||||
- **Avoid Specific Syntax:** Avoid language-specific syntax to keep the pseudocode universally understandable.
|
||||
- **Logic Emphasis:** Center your focus on the algorithm's logic.
|
||||
- **Flow of Control:** Clearly illustrate the flow of control using sequences (steps following each other), selections (decisions based on conditions), and iterations (loops).
|
||||
- **Consistency:** Ensure the terminology is consistent and clear to enhance understandability.
|
||||
|
||||
**Pseudocode: A Step-by-step Approach**
|
||||
|
||||
1. **Understand the Problem:** Clearly define what the problem is asking for.
|
||||
2. **Plan Your Approach:** Brainstorm possible ways to solve the problem.
|
||||
3. **Write the Pseudocode:** Describe the steps to solve the problem in plain language.
|
||||
4. **Utilize Control Structures:** Incorporate sequences, selections, and iterations to control the flow of the pseudocode.
|
||||
5. **Indent for Clarity:** Use indentation to show hierarchy and structure in your pseudocode.
|
||||
6. **Avoid Language-Specific Syntax:** Ensure your pseudocode is language-agnostic.
|
||||
7. **Review and Refine:** Go through your pseudocode, making sure it makes sense and is complete. Revise as necessary.
|
||||
8. **Translate to Code:** Convert your pseudocode into the chosen programming language.
|
||||
9. **Test and Revise:** Ensure your program behaves as expected by testing the code. If problems arise, revising the pseudocode may be necessary.
|
||||
|
||||
**Key Words and Phrases in Pseudocode**
|
||||
|
||||
| Category | Keywords |
|
||||
| --------------------- | -------------------------------------------------------------------- |
|
||||
| Sequence | First, Second, Then, After, Next, Finally |
|
||||
| Selection | If, Else, Then, Otherwise, Choose, Depending on |
|
||||
| Iteration | While, For, Do Until, Repeat Until, Loop, Repeat, Until, Each, Times |
|
||||
| Input/Output | Read, Print, Display, Input, Output, Get, Show |
|
||||
| Arithmetic Operations | Add, Subtract, Multiply, Divide, Calculate, Compute |
|
||||
| Comparisons | Equals, Greater than, Less than, Not equal to |
|
||||
| Data Manipulation | Set, Reset, Increment, Update, Append, Remove |
|
||||
|
||||
**Remember to adjust these according to the conventions used in your team or organization.**
|
||||
|
||||
**Examples of Pseudocode**
|
||||
|
||||
**Tip Calculator**
|
||||
|
||||
```pseudocode
|
||||
# Print instructions to user
|
||||
Print "Enter the total bill:"
|
||||
Read totalBill
|
||||
Print "Enter the desired tip percentage:"
|
||||
Read tipPercentage
|
||||
|
||||
# Calculate the tip amount
|
||||
Set tipAmount to (totalBill \* (tipPercentage / 100))
|
||||
|
||||
# Calculate the total with tip
|
||||
Set totalWithTip to totalBill + tipAmount
|
||||
|
||||
# Round the total down to the nearest whole dollar
|
||||
Set finalTotal to Floor(totalWithTip)
|
||||
|
||||
# Print the final total to the user
|
||||
Print "The total bill including tip is", finalTotal
|
||||
```
|
||||
|
||||
**Currency Converter**
|
||||
|
||||
```pseudocode
|
||||
# Print instructions to user
|
||||
Print "Enter the amount in your source currency:"
|
||||
Read sourceAmount
|
||||
Print "Enter the conversion rate to the target currency:"
|
||||
Read conversionRate
|
||||
|
||||
# Calculate the equivalent amount in the target currency
|
||||
Set targetAmount to sourceAmount \* conversionRate
|
||||
|
||||
# Print the amount in the target currency to the user
|
||||
Print "The amount in the target currency is", targetAmount
|
||||
```
|
||||
|
||||
**Conclusion**
|
||||
|
||||
Pseudocode is a valuable tool that can help you to write more efficient, maintainable, and bug-free code. By following the guidelines and best practices outlined in this cheat sheet, you can improve your pseudocode writing skills and become a better programmer.
|
||||
Reference in New Issue
Block a user