Photo by Thom Milkovic on Unsplash
WTH is a Glyphicon?! Bootstrap, flask and still chugging away at this Python 2 course. Today I learned: Tuesday 14 June 2022
Continueing with Codecademy's 'Python 2' course:
I can confirm that functions in py are defined using the def keyword.
I just spotted another placeholder: %f
- I'll figure out what this one means soon.
Instead of importing a whole library, which takes up unnecessary space. You can import just a single function instead. This is called functional import.
For example you can import just the sqrt function from the math library by doing this:
from math import sqrt
I also saw this from the flask tutorial:
- from flask import Flask
Supposedly there is a 'Universal import' that allows you to import everything froma library.
But what is the difference between:
import math
&
from math import *
?
Ah, I see now. If you use the latter option, you cannot define you own variable names if the name matches a function within the library.
For example
- math.sqrt from the first option is different from sqrt in the second option.
So the tutorial suggests that you either stick to either 'import module' and type 'module.name'
or
Just 'import' specific variables and functions from various modules as needed.
You can use the 'print dir(module)' to print out all the functions within the module.
max(), min() and abs() are all built in methods that do not require importing a module.
- abs() returns the absolute value, which means its distance from zero.
The type() function returns the type of the data passed into it.
Eg type(1.1)
Returns 'float'
You can add items to a list in python using the .append() method.
suitcase.append("lunch")
- This will add a string with the text "lunch" to the end of the "suitcase" list.
For loops in python are different from for loops in Javascript.
My_list = [1,2,3,4,5]
for number in my_list:
#do stuff
- The above code will perform an action for every number in that list.
Dictionaries are like lists, but their variables are accessed by key value pairs:
d = {'key1' : 1, 'key2' : 2, 'key3' : 3}
In the above, 'key1' points to the value 1, etc.
It looks like dictionaries are defined by using curly braces{}, and lists are defined using square braces [].
Dictionaries and lists haved different ways of removing items.
Dictionaries:
- Del dict_name[key_name]
Lists:
- list_name.remove("item_name")
The python 'range()' function is a shortcut to create a list.
The range function has three different versions:
range(stop)
range(start, stop)
range(start, stop, step)
In all cases, the range() function returns a list of number from start up to (but not including) stop.
Each item increases by step. So I guess if step is 3, then it will start with 1, 4, 7, etc.
If 'start' is omitted, it will default to 0.
If 'step' is omitted, it will default to 1.
Codecademy is very buggy. Sometimes I have the perfect code, and it still gives me an error. And when I request to see the solution, it presents either the exact same code, or an incomplete solution. Sometimes I need to reset the workspace and start again.
Started to learn about Bootstrap from this Toptal article from Tomislav Bacinger: https://www.toptal.com/front-end/what-is-bootstrap-a-short-tutorial-on-the-what-why-and-how
Is it possible to use bootstrap with Python instead of Javascript?
There are a number of free bootstrap themes from sources such as:
Bootswatch: https://bootswatch.com/
WrapBootstrap: https://wrapbootstrap.com/
I just came across a new word: Glyphicon
According to this tutorial: https://www.tutorialspoint.com/bootstrap/bootstrap_glyphicons.htm#:~:text=What%20are%20Glyphicons%3F,Bootstrap%20projects%20free%20of%20cost.
- "Glyphicons are icon fonts which you can use in your web projects."
The Bootstrap grid system has for tiers of classes:
Xs for phones (<768px)
Sm for tablets (>= 768px)
Md for desktops (>=992px)
Lg for larger desktops (>=1200px)