This markdown file serves as a guide and quick reference to Python's language syntax, key features, and functionalities.
- Introduction to Python
- Variables and Data Types
- Control Flow
- Functions
- Classes (Object-oriented programming)
- Error Handling
- File Input/Output
- Python Libraries
- Tips & Tricks
- Resources for Further Learning
Python is an interpreted, high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation.
print("Hello, World!")
In Python, variables are containers for storing data values. Python has the following data types built-in by default, in these categories:
- Text Type:
str
- Numeric Types:
int
,float
,complex
- Sequence Types:
list
,tuple
,range
- Mapping Type:
dict
- Set Types:
set
,frozenset
- Boolean Type:
bool
- Binary Types:
bytes
,bytearray
,memoryview
x = "Hello World" #str
x = 20 #int
x = 20.5 #float
x = 1j #complex
x = ["apple", "banana", "cherry"] #list
x = ("apple", "banana", "cherry") #tuple
x = range(6) #range
x = {"name" : "John", "age" : 36} #dict
x = {"apple", "banana", "cherry"} #set
x = frozenset({"apple", "banana", "cherry"}) #frozenset
x = True #bool
x = b"Hello" #bytes
x = bytearray(5) #bytearray
x = memoryview(bytes(5)) #memoryview
Control flow is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated.
- if ... Else
a = 33 b = 200 if b > a: print("b is greater than a")
- while Loops
i = 1 while i < 6: print(i) i += 1
- For Loops
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
def my_function():
print("Hello from a function")
my_function()
Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects.
class MyClass:
x = 5
p1 = MyClass()
print(p1.x)
Python has several built-in exceptions that force your program to output an error when something in the program goes wrong.
- Raising an Exception
x = -1 if x < 0: raise Exception("Sorry, no numbers below zero")
- The try ... except Block
try: print(variable) except: print("An exception occurred")
Python allows you to work with files (read and write files).
- Writing to a File
f = open("demofile.txt", "w") f.write("Now the file has content!") f.close()
- Reading a File
f = open("demofile.txt", "r") print(f.read()) f.close()
Python has a rich collection of libraries. These are some of them:
- NumPy
- SciPy
- Matplotlib
- Pandas
Python is filled with subtle coding techniques that can make your code more efficient and easier to understand. Here are a few:
List comprehensions offer a concise way to create lists based on existing lists. Here is an example:
# Traditional method
numbers = [1, 2, 3, 4, 5]
squares = []
for n in numbers:
squares.append(n**2)
# Using List Comprehensions
squares = [n**2 for n in numbers]
When you need to iterate over a list and use both its elements and their indexes, enumerate()
becomes invaluable. It allows us to loop over something and have an automatic counter.
list = ["apple", "banana", "mango"]
for i, item in enumerate(list):
print(f"Item {i} is {item}")
Python allows a single line of code to assign a value to a variable based on a condition.
condition = True
x = 1 if condition else 0 # If condition is True, x is 1. Else, x is 0
Here are some resources to further enhance your Python knowledge and skills:
1. Python's Official Documentation: https://docs.python.org/3/
2. Real Python: https://realpython.com/
3. Codecademy's Python Course: https://www.codecademy.com/learn/learn-python-3
4. r/Python Subreddit: https://www.reddit.com/r/Python/
If you've discovered new resources, tips, or information that could benefit Python enthusiasts in their learning journey, feel free to contribute to this Markdown file!
- Fork this repository.
- Make your changes or additions to the Markdown file.
- Create a pull request describing your changes and their purpose.
- Your contribution will be reviewed and merged to help Python enthusiasts!
Let's make this resource better together! 🐍💻