Thumbnail: jekyll

TCM PEH Python Notes C

on under Certs
2 minute read

Importing Modules in Python3

#!/bin/python3
import moduleName
import sys #system funct & parameters such as print(sys.version)
import argv
import datetime

Import part of a module

from datetime import datetime # to import part of a module

Import with alias

So you can obfuscate with this, it looks like

from datetime import datetime as dt # now print(datetime.now()) == print(dt.now())

Advanced Strings

my_name = “Heath”
print(my_name[0]) # = H
print(my_name[-1]) # = h
sentence = “This is a sentence.”
print(sentence[:4]) # prints This
print(sentence[-9:-4])
print(sentence.split()) # breaks out as individual words
sent_split = sentence.split()
sent_join = ‘ ’.join(sent_split)
print(sent_join) # now it puts it all back together
quote = “He said, ‘I quote myself’” # single quotes inside double quotes
quote = “He said, \”I quote myself"" # escaped characters with
2_mch_space = “ Hello”
print(2_mch_space.strip()) # outputs only Hello
print (“A” in “Apple”) # looks for boolean truth, True or False, is True
print(“a” in “Apple”) # outputs False
letter = “A”
word =“Apple”
print(letter.lower() in word.lower()) # normalizes the chars for above boolean truths, YOU WILL USE THIS
movie = “The Hangover”
print(“My favorite movie is ().”.format(movie) # will insert movie var into brackets, much better than string concatenation

Dictionaries

drinks = (“wr”: 7, :of": 10, “ld”: 8) # dictionary == key/value pairs {}
key: value, key: value, key: value over and over
employees = (“Fin”: ["Bob, “Linda, ”Tina], “IT”: ["Gene, “Lou”, “Ted”])
print(employees) # so far just prints above as if a char str

Updating Dictionaries

employees[“Legal”] = [“Feng”] # adds to the dictionary
employees.update({“Sales”: [“Sifl”, “Ollie”]}) # just another way to add to dictionary
print(employees) # now fin, it, legal, and sales print out
drinks[‘wr’} = 8
print(drinks) # now drinks(wr: 8) instead of drinks(wr: 7)
print(drinks.get(“wr”)) # prints wr and value

Certs, Python
comments powered by Disqus