sudo apt install libsqlite3-dev
pip3 install jupyter notebook
jupyter notebook
rank = 10 eggs = 12 people = 3
temperature = 10.2 rainfall = 5.98 elevation = 1031.88
message = "Welcome to our online shop!" name = "John" serial = "R001991981SW"
members = ["Sim Soony", "Marry Roundknee", "Jack Corridor"] pixel_values = [252, 251, 251, 253, 250, 248, 247]
phone_numbers = {"John Smith": "+37682929928", "Marry Simpons": "+423998200919"} volcano_elevations = {"Glacier Peak": 3213.9, "Rainer": 4392.1}
phone_numbers.keys()
phone_numbers.values()
vowels = ('a', 'e', 'i', 'o', 'u') one_digits = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
dir(str) dir(list) dir(dict)
dir(__builtins__)
help(str) help(str.replace) help(dict.values)
Sometimes you might need to convert between different data types in Python for one reason or another. That is very easy to do:
cool_tuple = (1, 2, 3) cool_list = list(cool_tuple) cool_list # [1, 2, 3]
cool_list = [1, 2, 3] cool_tuple = tuple(cool_list) cool_tuple # (1, 2, 3)
cool_string = "Hello" cool_list = list(cool_string) cool_list # ['H', 'e', 'l', 'l', 'o']
cool_list = ['H', 'e', 'l', 'l', 'o'] cool_string = str.join("", cool_list) cool_string # 'Hello'
As can be seen above, converting a list into a string is more complex. Here str() is not sufficient. We need str.join(). Try running the code above again, but this time using str.join(«—», cool_list) in the second line. You will understand how str.join() works.
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] 0 1 2 3 4 5 6
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] -7 -6 -5 -4 -3 -2 -1
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] days[1:4] Output: ['Tue', 'Wed', 'Thu']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] days[:3] Output:['Mon', 'Tue', 'Wed']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] days[-3:] Output: ['Fri', 'Sat', 'Sun']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] days[:-1] Output: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] days[:-2] Output: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
phone_numbers = {"John":"+37682929928","Marry":"+423998200919"} phone_numbers["Marry"] Output: '+423998200919'
def cube_volume(a): return a * a * a
message = "hello there" if "hello" in message: print("hi") else: print("I don't understand")
message = "hello there" if "hello" in message: print("hi") elif "hi" in message: print("hi") elif "hey" in message: print("hi") else: print("I don't understand")
x = 1 y = 1 if x == 1 and y==1: print("Yes") else: print("No")
x = 1 y = 2 if x == 1 or y==2: print("Yes") else: print("No")
isinstance("abc", str) isinstance([1, 2, 3], list) # or directly: type("abc") == str type([1, 2, 3]) == lst
A for-loop is useful to repeatedly execute a block of code.
for letter in 'abc': print(letter.upper())
A B C
phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"} for value in phone_numbers.keys(): print(value)
John Smith Marry Simpsons
phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"} for value in phone_numbers.values(): print(value)
+37682929928 +423998200919
phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"} for key, value in phone_numbers.items(): print(key, value)
John Smith +37682929928 Marry Simpons +423998200919
while datetime.datetime.now() < datetime.datetime(2090, 8, 20, 19, 30, 20): print("It's not yet 19:30:20 of 2090.8.20")
A list comprehension is an expression that creates a list by iterating over another container.
[i*2 for i in [1, 5, 10]]
[2, 10, 20]
[i*2 for i in [1, -2, 10] if i>0]
[2, 20]
[i*2 if i>0 else 0 for i in [1, -2, 10]]
[2, 0, 20]
def volume(a, b, c): return a * b * c
def converter(feet, coefficient = 3.2808): meters = feet / coefficient return meters print(converter(10)) # Output: 3.0480370641306997
def volume(a, b, c): return a * b * c print(volume(1, b=2, c=10))
def find_max(*args): return max(args) print(find_max(3, 99, 1001, 2, 8)) # Output: 1001
def find_winner(**kwargs): return max(kwargs, key = kwargs.get) print(find_winner(Andy = 17, Marry = 19, Sim = 45, Kae = 34)) # Output: Sim
with open("file.txt") as file: content = file.read()
with open("file.txt", "w") as file: content = file.write("Sample text")
with open("file.txt", "a") as file: content = file.write("More sample text")
with open("file.txt", "a+") as file: content = file.write("Even more sample text") file.seek(0) content = file.read()
import time time.sleep(5)
import sys sys.builtin_module_names
sys.prefix
.pip install pandas # or use python -m pip install pandas # if that doesn't work.
pip3 install pandas # or use python3 -m pip install pandas # if that doesn't work.
pip install Flask
from flask import Flask, render_template app=Flask(__name__) @app.route("/") def home(): return render_template("home.html") @app.route("/about") def about(): return render_template("about.html") if __name__=="__main__": app.run(debug=True)
<!DOCTYPE html> <html> <head> <title>Flask app </title> <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}"> </head> <body> <header> <div class="container"> <h1 class="logo">Python Flask test page</h1> <ul class="menu"> <li><a href="{{ url_for('home') }}">HOME</a></li> <li><a href="{{ url_for('about') }}">ABOUT</a></li> </u1> </div> </header> <div class="container"> {%block content%} {%endblock%} </div> </body> </html>
{%extends "menu.html"%} {%block content%} <h2>HOME PAGE</h1> {%endblock%}
{%extends "menu.html"%} {%block content%} <h2>ABOUT PAGE</h1> {%endblock%}
body { margin: 0; padding: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; color: #060; } /* * Formatting the header area */ header { background-color: #DFB887; height: 35px; width: 100%; opacity: .9; margin-bottom: 10px; } header h1.logo { margin: 0; font-size: 1.7em; color: #fff; text-transform: uppercase; float: left; } header h1.logo:hover { color: #fff; text-decoration: none; } /* * Center the body content */ .container { width: 1200px; margin: 0 auto; } div.home { padding: 10px 0 30px 0; background-color: #E6E6FA; -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; } div.about { padding: 10px 0 30px 0; background-color: #E6E6FA; -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; } h2 { font-size: 3em; margin-top: 40px; text-align: center; letter-spacing: -2px; } h3 { font-size: 1.7em; font-weight: 100; margin-top: 30px; text-align: center; letter-spacing: -1px; color: #999; } .menu { float: right; margin-top: 8px; } .menu li { display: inline; } .menu li + li { margin-left: 35px; } .menu li a { color: #444; text-decoration: none; }