Diferències

Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.

Enllaç a la visualització de la comparació

Següent revisió
Revisió prèvia
info:cursos:udemy:python-mega-course [08/10/2024 06:24] – creat mateinfo:cursos:udemy:python-mega-course [18/10/2024 04:12] (actual) – [Curso python udemy] mate
Línia 1: Línia 1:
 = Curso python udemy = Curso python udemy
 +  * [[https://www.udemy.com/course/former-python-mega-course-build-10-real-world-applications/learn/lecture/34362798#overview]]
 +== interesante
 +  * Numpy: manejo de matrices
 +  * Web Mapping: creación mapas interactivos HTML
 +  * Manejo Webcam
 +  * Bokeh: libreria representación gráficos -> [[development:python:bokeh|]]
 +  * Pandas: libreria de analisis de datos -> [[development:python:pandas|]]
 +  * Flask: web development
 +  * openCV: image processing library
 +  * Mobile app: apk
 +  * Web Scraping
 +  * pyinstaller: creación de ejecutables
 +
 +== jupyter notebook
 +  * <code bash>sudo apt install libsqlite3-dev</code>
 +  * <code bash>pip3 install jupyter notebook</code>
 +  * <code bash>jupyter notebook</code>
 == Cheatsheet: Data Types == Cheatsheet: Data Types
   * Integers are used to represent whole numbers:<code python>rank = 10   * Integers are used to represent whole numbers:<code python>rank = 10
Línia 26: Línia 43:
 help(str.replace) help(str.replace)
 help(dict.values)</code> help(dict.values)</code>
 +
 +== 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:
 +
 +  * From tuple to list:<code python>cool_tuple = (1, 2, 3)
 +cool_list = list(cool_tuple)
 +cool_list # [1, 2, 3]
 +</code>
 +
 +  * From list to tuple:<code python>cool_list = [1, 2, 3]
 +cool_tuple = tuple(cool_list)
 +cool_tuple # (1, 2, 3)
 +</code>
 +
 +  * From string to list:<code python>cool_string = "Hello"
 +cool_list = list(cool_string)
 +cool_list # ['H', 'e', 'l', 'l', 'o']
 +</code>
 +
 +  * From list to string:<code python>cool_list = ['H', 'e', 'l', 'l', 'o']
 +cool_string = str.join("", cool_list)
 +cool_string # 'Hello'
 +</code>
 +
 +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
 +  * Lists, strings, and tuples have a positive index system:<code>
 +["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 +        1      2      3      4      5      6
 +</code>
 +  * And they have a negative index system as well:<code>
 +["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 +  -7     -6     -5     -4     -3     -2     -1
 +</code>
 +
 +  * In a list, the 2nd, 3rd, and 4th items can be accessed with:<code python>
 +days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 +days[1:4]
 +Output: ['Tue', 'Wed', 'Thu']
 +</code>
 +
 +  * First three items of a list:<code python>
 +days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 +days[:3]
 +Output:['Mon', 'Tue', 'Wed'
 +</code>
 +
 +  * Last three items of a list:<code python>
 +days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 +days[-3:]
 +Output: ['Fri', 'Sat', 'Sun']
 +</code>
 +
 +  * Everything but the last:<code python>
 +days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 +days[:-1] 
 +Output: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
 +</code>
 +
 +  * Everything but the last two:<code python>
 +days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 +days[:-2] 
 +Output: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'
 +</code>
 +
 +  * A dictionary value can be accessed using its corresponding dictionary key:<code python>
 +phone_numbers = {"John":"+37682929928","Marry":"+423998200919"}
 +phone_numbers["Marry"]
 +Output: '+423998200919'
 +</code>
 +
 +== Cheatsheet: Functions and Conditionals
 +  * Define functions:<code python>
 +def cube_volume(a):
 +    return a * a * a
 +</code>
 +
 +  * Write if-else conditionals:<code python>
 +message = "hello there"
 + 
 +if "hello" in message:
 +    print("hi")
 +else:
 +    print("I don't understand")
 +</code>
 + 
 +  * Write if-elif-else conditionals:<code python>
 +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")
 +</code>
 +
 +    * Use the and operator to check if both conditions are True at the same time:<code python>
 +x = 1
 +y = 1
 + 
 +if x == 1 and y==1:
 +    print("Yes")
 +else:
 +    print("No")
 +</code>
 +
 +  * Use the or operator to check if at least one condition is True:<code python>
 +x = 1
 +y = 2
 + 
 +if x == 1 or y==2:
 +    print("Yes")
 +else:
 +    print("No")
 +</code>
 +
 +  * Check if a value is of a particular type with isinstance:<code python>
 +isinstance("abc", str)
 +isinstance([1, 2, 3], list)
 +# or directly:
 +
 +type("abc") == str
 +type([1, 2, 3]) == lst
 +</code>
 +
 +    
 +== Cheatsheet: Loops
 +A for-loop is useful to repeatedly execute a block of code.
 +
 +  * You can create a for-loop like so:<code python>
 +for letter in 'abc':
 +    print(letter.upper())
 +</code><code ; output>
 +A
 +B
 +C
 +</code>
 +    * As you can see, the for-loop repeatedly converted all the items of 'abc' to uppercase.
 +    * The name after for (e.g. letter) is just a variable name
 +  * You can loop over dictionary keys as follows:<code python>
 +phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"}
 +for value in phone_numbers.keys():
 +    print(value)
 +</code><code ; output>
 +John Smith
 +Marry Simpsons
 +</code>
 +
 +  * You can loop over dictionary values:<code python>
 +phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"}
 +for value in phone_numbers.values():
 +    print(value)
 +</code><code ; output>
 ++37682929928
 ++423998200919
 +</code>
 +
 +  * You can loop over dictionary items:<code python>
 +phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"}
 +for key, value in phone_numbers.items():
 +    print(key, value)
 +</code><code ; output>
 +John Smith +37682929928
 +Marry Simpons +423998200919
 +</code>
 +
 +  * We also have while-loops. The code under a while-loop will run as long as the while-loop condition is true:<code python>
 +while datetime.datetime.now() < datetime.datetime(2090, 8, 20, 19, 30, 20):
 +    print("It's not yet 19:30:20 of 2090.8.20")
 +</code>
 +    * The loop above will print out the string inside print() over and over again until the 20th of August, 2090.
 +
 +== Cheatsheet: List Comprehensions
 +A list comprehension is an expression that creates a list by iterating over another container.
 +
 +  * A basic list comprehension:<code python>
 +[i*2 for i in [1, 5, 10]]</code><code ; output>
 +[2, 10, 20]</code>
 +
 +  * List comprehension with if condition:<code python>
 +[i*2 for i in [1, -2, 10] if i>0]</code><code ; output>
 +[2, 20]</code>
 +
 +  * List comprehension with an if and else condition:<code python>
 +[i*2 if i>0 else 0 for i in [1, -2, 10]]</code><code ; output>
 +[2, 0, 20]</code>
 +
 +== Cheatsheet: More on Functions
 +  * Functions can have more than one parameter:<code python>
 +def volume(a, b, c):
 +    return a * b * c</code>
 +
 +  * Functions can have default parameters (e.g. coefficient):<code python>
 +def converter(feet, coefficient = 3.2808):
 +    meters = feet / coefficient
 +    return meters
 + 
 +print(converter(10))
 +# Output: 3.0480370641306997
 +</code>
 +
 +  * Arguments can be passed as non-keyword (positional) arguments (e.g. a) or keyword arguments (e.g. b=2 and c=10):<code python>
 +def volume(a, b, c):
 +    return a * b * c
 + 
 +print(volume(1, b=2, c=10))
 +</code>
 +
 +  * An *args parameter allows the  function to be called with an arbitrary number of non-keyword arguments:<code python>
 +def find_max(*args):
 +    return max(args)
 +print(find_max(3, 99, 1001, 2, 8))
 +# Output: 1001
 +</code>
 +
 +  * A %%**%%kwargs parameter allows the function to be called with an arbitrary number of keyword arguments:<code python>
 +def find_winner(**kwargs):
 +    return max(kwargs, key = kwargs.get)
 + 
 +print(find_winner(Andy = 17, Marry = 19, Sim = 45, Kae = 34))
 +# Output: Sim
 +</code>
 +
 +  * Here's a summary of function elements:{{:info:cursos:udemy:pasted:20241009-022827.png?300}}
 +
 +== Cheatsheet: File Processing
 +  * You can read an existing file with Python:<code python>
 +with open("file.txt") as file:
 +    content = file.read()
 +</code>
 +
 +  * You can create a new file with Python and write some text on it:<code python>
 +with open("file.txt", "w") as file:
 +    content = file.write("Sample text")
 +</code>
 +  * You can append text to an existing file without overwriting it:<code python>
 +with open("file.txt", "a") as file:
 +    content = file.write("More sample text")</code>
 +
 +  * You can both append and read a file with:<code python>
 +with open("file.txt", "a+") as file:
 +    content = file.write("Even more sample text")
 +    file.seek(0)
 +    content = file.read()
 +</code>
 +    
 +== Cheatsheet: Imported Modules
 +  * Builtin objects are all objects that are written inside the Python interpreter in C language.
 +  * Builtin modules contain builtins objects.
 +  * Some builtin objects are not immediately available in the global namespace. They are parts of a builtin module. To use those objects the module needs to be imported first. E.g.:<code python>
 +import time
 +time.sleep(5)
 +</code>
 +  * A list of all builtin modules can be printed out with:<code python>
 +import sys
 +sys.builtin_module_names
 +</code>
 +  * Standard libraries is a jargon that includes both builtin modules written in C and also modules written in Python.
 +  * Standard libraries written in Python reside in the Python installation directory as .py files. You can find their directory path with ''sys.prefix''.
 +  * Packages are a collection of .py modules.
 +  * Third-party libraries are packages or modules written by third-party persons (not the Python core development team).
 +  * Third-party libraries can be installed from the terminal/command line:
 +    * Windows:<code python>
 +pip install pandas # or use 
 +python -m pip install pandas # if that doesn't work.
 +</code>
 +    * Mac and Linux:<code python>
 +pip3 install pandas # or use 
 +python3 -m pip install pandas # if that doesn't work.
 +</code>
 +
 +== Flask
 +<code bash>pip install Flask</code>
 +
 +<code python 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) 
 +</code>
 +
 +<code html 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>
 +</code>
 +<code html templates/home.html>
 +{%extends "menu.html"%}
 +{%block content%}
 +    <h2>HOME PAGE</h1>
 +{%endblock%}
 +</code>
 +
 +<code html templates/about.html>
 +{%extends "menu.html"%}
 +{%block content%}
 +    <h2>ABOUT PAGE</h1>
 +{%endblock%}
 +</code>
 +
 +<code css 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;
 +}
 +
 +</code>
  • info/cursos/udemy/python-mega-course.1728393898.txt.gz
  • Darrera modificació: 08/10/2024 06:24
  • per mate