-
-
Save JettIsOnTheNet/298d189559e0d134c5941e6742bc599f to your computer and use it in GitHub Desktop.
Syntax example scan sheet for Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" python up to speed | |
""" | |
# variables | |
integer_var = 10 | |
float_var = 10.5 | |
string_var = "Hello, Python!" | |
boolean_var = True | |
# functions | |
def greet(name): | |
return f"Hello {name}!" | |
# for loop | |
for i in range(5): | |
print("For loop iteration:", i) | |
# while loop | |
count = 0 | |
while count < 5: | |
print("While loop iteration:", count) | |
count += 1 | |
# conditional statements | |
if integer_var > 5: | |
print("Integer is greater than 5") | |
elif integer_var == 5: | |
print("Integer is 5") | |
else: | |
print("Integer is less than 5") | |
# data structures | |
my_list = [1, 2, 3, 4, 5] | |
my_dict = {'a': 1, 'b': 2} | |
my_set = {1, 2, 3} | |
my_tuple = (1, 2, 3) | |
# comprehensions | |
list_comprehension = [x * 2 for x in my_list] | |
dict_comprehension = {k: v * 2 for k, v in my_dict.items()} | |
# modules | |
# from module.sub import foo as bar | |
import math | |
print("Pi is:", math.pi) | |
# exception handling | |
try: | |
result = 10 / 0 | |
except ZeroDivisionError: | |
print("Caught a division by zero error!") | |
# write to file | |
with open('example.txt', 'w') as f: | |
f.write("Writing to a file in Python!") | |
# read from file | |
with open('example.txt', 'r') as f: | |
content = f.read() | |
print("File content:", content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment