Python Notes
Python Notes
org/pep-0008/
def _internal_func():
return 42
The underscore prefix is meant as a hint to another programmer that a
variable or method starting with a single underscore is intended for internal
use. This convention is defined in PEP 8. This isn't enforced by Python.
Python does not have strong distinctions between “private” and “public”
variables like Java does.
https://dbader.org/blog/meaning-of-underscores-in-python
2. dir(list)
3. dir(dict)
2. help(str.replace)
3. help(dict.values)
3. Inputs
A Python program can get user input via the input function:
The input function halts the execution of the program and gets text
input from the user:
1. name = input("Enter your name: ")
The input function converts any input to a string, but you can
convert it back to int or float:
1. experience_months = input("Enter your experience in months: ")
2. experience_years = int(experience_months) / 12
4. String Formatting
You can format strings with (works both on Python 2 and 3):
1. name = "Sim"
2. experience_years = 1.5
2. experience_years = 1.5
5. Loops
For loops are useful for executing a command over a large number
of items.
2. print(letter.upper())
Output:
A
B
C
Output:
John Smith
Marry Simpsons
3. print(value)
Output:
+37682929928
+423998200919
3. print(key, value)
Output:
The loop above will print out the string inside print() over and over
again until the 20th of August, 2090.
6. List Comprehensions
A list comprehension is an expression that creates a list by iterating
over another container.
A basic list comprehension:
1. [i*2 for i in [1, 5, 10]]
7. Functions
Output: 3.0480370641306997
Output: 1001
Output: Sim
8. File Processing
9. Modules/Libraries
Builtin objects are all objects that are written inside the Python
interpreter in C language.
https://www.datacamp.com/tutorial/decorators-python
https://www.freecodecamp.org/news/python-decorators-explained-with-
examples/
https://www.google.com/url?
sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUK
EwjG9rfM5ez5AhX9SUEAHbErCqUQtwJ6BAg2EAI&url=https%3A%2F
%2Fwww.youtube.com%2Fwatch%3Fv
%3DZsvftkbbrR0&usg=AOvVaw0eAcqAY-cEp7xu23J0-owa
https://medium.com/analytics-vidhya/retry-decorator-in-python-
55d0729755c7
when installing a new library for Python always make sure you install for the
right version e.g.
this will install on the default version - sudo python setup.py
install
https://devcenter.heroku.com/articles/python-runtimes#supported-
python-runtimes
Troubleshooting
If you deployed your website on Heroku but when you visit the
website on the browser you see an error, you probably did something
wrong during the deployment.
No worries! You can see what you did wrong by looking at the server
logs. You can access the server logs by running the following in your
terminal:
heroku logs