Skip to content

Instantly share code, notes, and snippets.

@LaurentiuGabriel
Created April 2, 2023 11:50
Show Gist options
  • Save LaurentiuGabriel/993c7368acabee937aee73efaa1177bc to your computer and use it in GitHub Desktop.
Save LaurentiuGabriel/993c7368acabee937aee73efaa1177bc to your computer and use it in GitHub Desktop.
Python in 99 seconds
# Variables
x = 5
y = 1.5
name = "John"
is_active = True
# Conditionals
if x > y:
print("x is greater than y")
elif x < y:
print("x is less than y")
else:
print("x is equal to y")
# For loop
for i in range(5):
print(i)
# While loop
counter = 0
while counter < 5:
print(counter)
counter += 1
# Functions
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
# Lists
my_list = [1, 2, 3]
my_list.append(4)
my_list.remove(1)
print(my_list)
# Tuples
my_tuple = (1, 2, 3)
# Dictionaries
my_dict = {'a': 1, 'b': 2}
print(my_dict['a'])
# Modules
import math
print(math.sqrt(9))
# Error handling
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero!")
# Classes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print("My name is " + self.name + " and I am " + str(self.age) + " years old.")
person = Person("Bob", 30)
person.introduce()
# File I/O
with open("example.txt", "w") as file:
file.write("This is an example.")
with open("example.txt", "r") as file:
print(file.read())
# Working with os module
import os
print(os.listdir())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment