Skip to content

Instantly share code, notes, and snippets.

@daviddahl
Created May 22, 2022 20:12
Show Gist options
  • Save daviddahl/63870e35ed1a3666c9dfa480ebca6e19 to your computer and use it in GitHub Desktop.
Save daviddahl/63870e35ed1a3666c9dfa480ebca6e19 to your computer and use it in GitHub Desktop.
basics.py
#!/usr/bin/env python3
### TYPES ... see: https://www.w3schools.com/python/python_datatypes.asp
# Integer:
my_number = 33
# Float
my_float = 33.33
# String
my_name = "Doctor Who"
# list
my_list = [1, 2, 2, 3, 4, 4, "a", "a", "b", "c",] # mutable
# tuple
my_tuple = (1, 2, 3, 4, 5, 6, "a", "b", "c",) # immutable
# dict
my_dict = {"first_name": "Doctor", "last_name": "Who",}
# set
my_set = set(my_list) # removes all duplicates
# Boolean
is_program = True
is_elephant = False
# null or None
my_money = None
# Binary types: bytes, bytearray, memoryview TBD
# function:
def print_stuff(name, fav_number):
"""
This is a `docstring` comment that explains the function's usage
"""
print(f"Your Name is {name} and favorite number is {fav_number}")
# Put all of the variables in a List
my_data = [
my_number, my_float, my_name, my_list, my_tuple,
my_dict, my_set, is_program, is_elephant, my_money,
]
print("Print out all of the data that was created...")
# Iterate through the data
for item in my_data:
_type = type(item)
print(f"item: {item} is type {_type}")
def print_dict_data():
keys = my_dict.keys()
# Get the keys in the dict as a list
print(keys)
print(my_dict["first_name"])
print(my_dict.get("last_name")) # Gets the value or returns None
class DataStuff(object):
"""
a basic class with a constructor (__init__) and method
"""
def __init__(self):
"""
__init__ is the function that runs when you call the class
"""
print("class is instanciating...")
self.my_name = f"{my_name}, Esq."
self.my_number = my_number + 1
def print_name(self):
print(self.my_name)
def print_my_number(self):
print(self.my_number)
if __name__ == "__main__":
print("This python file was run via python ./python_basics.py")
# Run the function "print_stuff"
print_stuff(my_name, my_number)
print_dict_data()
my_obj = DataStuff()
my_obj.print_name()
my_obj.print_my_number()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment