Taula de continguts

Curso python udemy

interesante

jupyter notebook

Cheatsheet: Data Types

Tip: Converting Between Datatypes

Sometimes you might need to convert between different data types in Python for one reason or another. That is very easy to do:

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.

Cheatsheet: Operations with Data Types

Cheatsheet: Functions and Conditionals

Cheatsheet: Loops

A for-loop is useful to repeatedly execute a block of code.

Cheatsheet: List Comprehensions

A list comprehension is an expression that creates a list by iterating over another container.

Cheatsheet: More on Functions

Cheatsheet: File Processing

Cheatsheet: Imported Modules

Flask

pip install Flask
script1.py
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) 
templates/menu.html
<!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>
templates/home.html
{%extends "menu.html"%}
{%block content%}
    <h2>HOME PAGE</h1>
{%endblock%}
templates/about.html
{%extends "menu.html"%}
{%block content%}
    <h2>ABOUT PAGE</h1>
{%endblock%}
static/css/main.css
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;
}