Skip to content

Instantly share code, notes, and snippets.

View marius-test's full-sized avatar
🌊
Programming

Marius Briscut marius-test

🌊
Programming
View GitHub Profile
list.sort()
  • Type: A method of the list object.
  • Behavior: Sorts the list in place.
  • Return Value: Returns None.
  • Example: my_list.sort()

@marius-test
marius-test / decorator_example.py
Last active August 18, 2025 21:22
Simple decorator example in Python
# decorator example in Python
# decorator function
def decorator(func):
def wrapper():
print("Runs before") # do something before
func() # call the original function
print("Runs after") # do something after
return wrapper
@marius-test
marius-test / time_format_conversion.py
Last active July 24, 2025 13:58
Time conversion program from 24-hour format to 12-hour format
try:
hour_24 = int(input("Enter hour in 24h format (0–23): "))
if 0 <= hour_24 <= 23:
hour_12 = hour_24 % 12 or 12
print(f"The hour in 12h format is: {hour_12}")
else:
print("Error: Please enter a valid hour between 0 and 23.")
except ValueError:
@marius-test
marius-test / self_explained.py
Last active August 18, 2025 21:24
The best explanation of self in Python
class Dog:
# the __init__ method is a special method called the constructor
# it initializes the attributes of a new instance
# 'self' refers to the new Dog object being created
def __init__(self, name, breed):
self.name = name # 'self.name' assigns the 'name' argument to the 'name' attribute of *this* dog
self.breed = breed # 'self.breed' assigns the 'breed' argument to the 'breed' attribute of *this* dog
self.is_hungry = True # every new dog starts hungry. This is an instance attribute
# this is an instance method, 'self' refers to the specific dog instance
@marius-test
marius-test / read_write_delete_file.py
Last active May 27, 2025 12:02
How to read/write/delete files in Python
# read text file
with open("log.txt", "r") as f:
content = f.read()
# write (overwrite) text
with open("log.txt", "w") as f:
f.write("New log start\n")
# append to file
with open("log.txt", "a") as f:
@marius-test
marius-test / access_modifiers.py
Last active August 18, 2025 21:22
Understanding access modifiers in Python
class Demo:
def __init__(self):
self.public_var = "Public"
self._protected_var = "Protected"
self.__private_var = "Private"
def show_vars(self):
print(self.public_var)
print(self._protected_var)
print(self.__private_var)
@marius-test
marius-test / python_slicing_demo.py
Last active May 27, 2025 12:02
A small demo about slicing in Python
name = "Marius"
text = f"MyNameIs{name}" # MyNameIsMarius
print(text[0:6]) # 'MyName' - from start (0) up to 6 (exclusive)
print(text[:6]) # 'MyName' - same as above, starts by default to 0
print(text[6:]) # 'IsMarius' - from index 6 to end
print(text[::2]) # 'MNmIMru' - starts with index 0 then every 2nd character
print(text[::-1]) # 'suiraMsIemaNyM' - reversed string
print(text[-7:-1]) # 'sMarius' - slice from index -7 to -1
print(text[-1:-8:-1]) # 'suiraMs' - slice backward from end (negative step), stops at index -8 (exclusive)
@marius-test
marius-test / zen.py
Created May 16, 2025 18:49
The Zen of Python
import this
# run the .py file
# and have a great coding day!
@marius-test
marius-test / pip_upgrade_all_packages.md
Last active August 18, 2025 21:21
PIP Upgrade All Packages

To view all installed packages, you can utilize either of the following commands in the Terminal:

pip list

or

pip freeze
@marius-test
marius-test / py_to_exe.md
Last active August 18, 2025 21:19
Convert .py to .exe

To convert a .py file to .exe you can use PyInstaller

  1. Open the terminal and type the following command to install the PyInstaller:
pip install pyinstaller
  1. While in the terminal, navigate to the {file_name}.py file location using the cd command.