Introduction To HTML Django Girls Tutorial
Introduction To HTML Django Girls Tutorial
Introduction To HTML Django Girls Tutorial
org/en/html/
tutorial.djangogirls.org
What is HTML?
1 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
blog
└───templates
└───blog
(You might wonder why we need two directories both called blog
– as you will discover later, this is simply a useful naming
convention that makes life easier when things start to get more
complicated.)
2 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
blog/templates/blog/post_list.html
<html>
<p>Hi there!</p>
<p>It works!</p>
</html>
The most basic tag, <html>, is always the beginning of any web
page and </html> is always the end. As you can see, the whole
content of the website goes between the beginning tag <html>
and closing tag </html>
Each HTML page is also divided into two elements: head and
body.
3 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
For example, you can put a web page title element inside the
<head>, like this:
blog/templates/blog/post_list.html
<html>
<head>
<title>Ola's blog</title>
</head>
<body>
<p>Hi there!</p>
<p>It works!</p>
</body>
</html>
Notice how the browser has understood that "Ola's blog" is the
title of your page? It has interpreted <title>Ola's
blog</title> and placed the text in the title bar of your
browser (it will also be used for bookmarks and so on).
4 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
Probably you have also noticed that each opening tag is matched
by a closing tag, with a /, and that elements are nested (i.e. you
can't close a particular tag until all the ones that were inside it
have been closed too).
It's like putting things into boxes. You have one big box, <html>
</html>; inside it there is <body></body>, and that contains
still smaller boxes: <p></p>.
You can now have a little fun and try to customize your template!
Here are a few useful tags for that:
<br /> goes to another line (you can't put anything inside br)
5 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
blog/templates/blog/post_list.html
<html>
<head>
<title>Django Girls blog</title>
</head>
<body>
<div>
<h1><a href="/">Django Girls
Blog</a></h1>
</div>
<div>
<p>published: 14.06.2014, 12:14</p>
<h2><a href="">My first post</a>
</h2>
<p>Aenean eu leo quam. Pellentesque
ornare sem lacinia quam venenatis vestibulum.
Donec id elit non mi porta gravida at eget
metus. Fusce dapibus, tellus ac cursus commodo,
tortor mauris condimentum nibh, ut fermentum
massa justo sit amet risus.</p>
</div>
<div>
<p>published: 14.06.2014, 12:14</p>
<h2><a href="">My second post</a>
</h2>
<p>Aenean eu leo quam. Pellentesque
ornare sem lacinia quam venenatis vestibulum.
6 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
The first div element contains the title of our blog – it's a
heading and a link
Yaaay! But so far, our template only ever displays exactly the
same information – whereas earlier we were talking about
templates as allowing us to display different information in the
7 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
same format.
It'd be good to see all this out and live on the Internet, right? Let's
do another PythonAnywhere deploy:
First off, let's see what files have changed since we last deployed
(run these commands locally, not on PythonAnywhere):
command-line
$ git status
Make sure you're in the djangogirls directory and let's tell git
to include all the changes within this directory:
command-line
--all means that git will also recognize if you've deleted files
(by default, it only recognizes new/modified files). Also remember
(from chapter 3) that . means the current directory.
Before we upload all the files, let's check what git will be
uploading (all the files that git will upload should now appear in
green):
command-line
$ git status
We're almost there, now it's time to tell it to save this change in
its history. We're going to give it a "commit message" where we
8 di 9 13/11/2017, 20:01
Introduction to HTML · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/html/
describe what we've changed. You can type anything you'd like at
this stage, but it's helpful to type something descriptive so that
you can remember what you've done in the future.
command-line
Make sure you use double quotes around the commit message.
command-line
$ git push
command-line
$ cd ~/my-first-blog
$ git pull
[...]
And watch your code get downloaded. If you want to check that
it's arrived, you can hop over to the Files tab and view your code
on PythonAnywhere.
Finally, hop on over to the Web tab and hit Reload on your web
app.
9 di 9 13/11/2017, 20:01
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
tutorial.djangogirls.org
Python prompt
command-line
$ python3
Python 3.6.1 (...)
Type "help", "copyright", "credits" or "license"
for more information.
>>>
1 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
If you want to exit the Python console at any point, just type
exit() or use the shortcut Ctrl + Z for Windows and Ctrl +
D for Mac/Linux. Then you won't see >>> any longer.
command-line
>>> 2 + 3
5
Nice! See how the answer popped out? Python knows math! You
could try other commands like:
4 * 5
5 - 1
40 / 2
command-line
>>> 2 ** 3
8
Have fun with this for a little while and then get back here. :)
2 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
Strings
How about your name? Type your first name in quotes like this:
command-line
>>> "Ola"
'Ola'
command-line
command-line
>>> "Ola" * 3
'OlaOlaOla'
If you need to put an apostrophe inside your string, you have two
ways to do it.
command-line
3 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
command-line
command-line
>>> "Ola".upper()
'OLA'
You just used the upper method on your string! A method (like
upper()) is a sequence of instructions that Python has to
perform on a given object ("Ola") once you call it.
command-line
>>> len("Ola")
3
Summary
4 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
Errors
Let's try something new. Can we get the length of a number the
same way we could find out the length of our name? Type in
len(304023) and hit enter:
command-line
>>> len(304023)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
We got our first error! It says that objects of type "int" (integers,
whole numbers) have no length. So what can we do now? Maybe
we can write our number as a string? Strings have a length,
right?
command-line
>>> len(str(304023))
6
5 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
Variables
command-line
command-line
>>> name
'Ola'
command-line
6 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
'Sonja'
command-line
>>> len(name)
5
command-line
>>> a = 4
>>> b = 6
>>> a * b
24
But what if we used the wrong name? Can you guess what would
happen? Let's try!
command-line
Play with this for a while and see what you can do!
7 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
Try this:
command-line
When you just type name, the Python interpreter responds with
the string representation of the variable 'name', which is the
letters M-a-r-i-a, surrounded by single quotes, ''. When you say
print(name), Python will "print" the contents of the variable to
the screen, without the quotes, which is neater.
Lists
command-line
>>> []
[]
Yes, this list is empty. Not very useful, right? Let's create a list of
lottery numbers. We don't want to repeat ourselves all the time,
so we will put it in a variable, too:
8 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
command-line
All right, we have a list! What can we do with it? Let's see how
many lottery numbers there are in a list. Do you have any idea
which function you should use for that? You know this already!
command-line
>>> len(lottery)
6
command-line
>>> lottery.sort()
command-line
>>> print(lottery)
[3, 12, 19, 30, 42, 59]
As you can see, the numbers in your list are now sorted from the
lowest to highest value. Congrats!
command-line
>>> lottery.reverse()
>>> print(lottery)
[59, 42, 30, 19, 12, 3]
Easy, right? If you want to add something to your list, you can do
this by typing this command:
9 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
command-line
>>> lottery.append(199)
>>> print(lottery)
[59, 42, 30, 19, 12, 3, 199]
If you want to show only the first number, you can do this by
using indexes. An index is the number that says where in a list
an item occurs. Programmers prefer to start counting at 0, so the
first object in your list is at index 0, the next one is at 1, and so
on. Try this:
command-line
>>> print(lottery[0])
59
>>> print(lottery[1])
42
As you can see, you can access different objects in your list by
using the list's name and the object's index inside of square
brackets.
To delete something from your list you will need to use indexes
as we learned above and the pop() method. Let's try an
example and reinforce what we learned previously; we will be
deleting the first number of our list.
command-line
>>> print(lottery)
[59, 42, 30, 19, 12, 3, 199]
>>> print(lottery[0])
59
>>> lottery.pop(0)
59
>>> print(lottery)
10 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
For extra fun, try some other indexes: 6, 7, 1000, -1, -6 or -1000.
See if you can predict the result before trying the command. Do
the results make sense?
You can find a list of all available list methods in this chapter of
the Python documentation: https://docs.python.org/3/tutorial
/datastructures.html
Dictionaries
command-line
>>> {}
{}
Now, try writing the following command (try substituting your own
information, too):
command-line
11 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
You can check the content of individual keys with this syntax:
command-line
>>> print(participant['name'])
Ola
See, it's similar to a list. But you don't need to remember the
index – just the name.
command-line
>>> participant['age']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'age'
Do you need to associate values with keys, so you can look them
up efficiently (by key) later on? Use a dictionary.
12 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
command-line
Like lists, using the len() method on the dictionaries returns the
number of key–value pairs in the dictionary. Go ahead and type
in this command:
command-line
>>> len(participant)
4
command-line
>>> participant.pop('favorite_numbers')
[7, 42, 92]
>>> participant
{'country': 'Poland', 'favorite_language':
'Python', 'name': 'Ola'}
command-line
13 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
As you can see, the value of the key 'country' has been
altered from 'Poland' to 'Germany'. :) Exciting? Hurrah! You
just learned another amazing thing.
Summary
errors – you now know how to read and understand errors that
show up if Python doesn't understand a command you've given it
variables – names for objects that allow you to code more easily
and to make your code more readable
Compare things
command-line
>>> 5 > 2
True
>>> 3 < 1
False
>>> 5 > 2 * 2
14 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
True
>>> 1 == 1
True
>>> 5 != 2
True
Do you wonder why we put two equal signs == next to each other
to compare if numbers are equal? We use a single = for
assigning values to variables. You always, always need to put
two of them – == – if you want to check if things are equal to
each other. We can also state that things are unequal to each
other. For that, we use the symbol !=, as shown in the example
above.
command-line
>>> 6 >= 12 / 2
True
>>> 3 <= 2
False
> and < are easy, but what do >= and <= mean? Read them like
this:
15 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
command-line
command-line
Here you see that just like in the expression, Python is not able to
compare a number (int) and a string (str). Instead, it shows a
TypeError and tells us the two types can't be compared together.
Boolean
16 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
there is.
True
False
command-line
>>> a = True
>>> a
True
command-line
>>> a = 2 > 5
>>> a
False
Practice and have fun with Booleans by trying to run the following
commands:
True or 1 == 1
1 != 2
17 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
So far we've been writing all our python code in the interpreter,
which limits us to entering one line of code at a time. Normal
programs are saved in files and executed by our programming
language interpreter or compiler. So far we've been running our
programs one line at a time in the Python interpreter. We're
going to need more than one line of code for the next few tasks,
so we'll quickly need to:
Run it!
To exit from the Python interpreter that we've been using, simply
type the exit() function
command-line
>>> exit()
$
Earlier, we picked out a code editor from the code editor section.
We'll need to open the editor now and write some code into a
new file:
editor
Now we need to save the file and give it a descriptive name. Let's
18 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
Note You should notice one of the coolest thing about code
editors: colors! In the Python console, everything was the same
color; now you should see that the print function is a different
color from the string. This is called "syntax highlighting", and it's
a really useful feature when coding. The color of things will give
you hints, such as unclosed strings or a typo in a keyword name
(like the def in a function, which we'll see below). This is one of
the reasons we use a code editor. :)
With the file saved, it's time to run it! Using the skills you've
learned in the command line section, use the terminal to change
directories to the desktop.
command-line
$ cd ~/Desktop
command-line
$ cd ~/Desktop
command-line
> cd %HomePath%\Desktop
19 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
command-line
> cd $Home\Desktop
Now use Python to execute the code in the file like this:
command-line
$ python3 python_intro.py
Hello, Django girls!
command-line
> python python_intro.py
Alright! You just ran your first Python program that was saved to
a file. Feel awesome?
If … elif … else
python_intro.py
if 3 > 2:
If we were to save and run this, we'd see an error like this:
command-line
$ python3 python_intro.py
20 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
python_intro.py
if 3 > 2:
print('It works!')
command-line
$ python3 python_intro.py
It works!
python_intro.py
21 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
if 5 > 2:
print('5 is indeed greater than 2')
else:
print('5 is not greater than 2')
command-line
$ python3 python_intro.py
5 is indeed greater than 2
python_intro.py
name = 'Sonja'
if name == 'Ola':
print('Hey Ola!')
elif name == 'Sonja':
print('Hey Sonja!')
else:
print('Hey anonymous!')
and executed:
command-line
$ python3 python_intro.py
Hey Sonja!
See what happened there? elif lets you add extra conditions
that run if the previous conditions fail.
You can add as many elif statements as you like after your
initial if statement. For example:
python_intro.py
22 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
volume = 57
if volume < 20:
print("It's kinda quiet.")
elif 20 <= volume < 40:
print("It's nice for background music")
elif 40 <= volume < 60:
print("Perfect, I can hear all the details")
elif 60 <= volume < 80:
print("Nice for parties")
elif 80 <= volume < 100:
print("A bit loud!")
else:
print("My ears are hurting! :(")
command-line
$ python3 python_intro.py
Perfect, I can hear all the details
python_intro.py
You don't need to write a comment for every line of code, but
they are useful for explaining why your code is doing something,
or providing a summary when it's doing something complex.
23 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
Summary
Boolean – a type of object that can only have one of two values:
True or False
comments - lines that Python won't run which let you document
your code
python_intro.py
def hi():
24 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
print('Hi there!')
print('How are you?')
hi()
You may wonder why we've written the name of the function at
the bottom of the file. This is because Python reads the file and
executes it from top to bottom. So in order to use our function, we
have to re-write it at the bottom.
command-line
$ python3 python_intro.py
Hi there!
How are you?
Note: if it didn't work, don't panic! The output will help you to
figure why:
If there's no output at all, check that the last hi() isn't indented -
if it is, that line will become part of the function too, and it will
never get run.
Let's build our first function with parameters. We will use the
previous example – a function that says 'hi' to the person running
25 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
it – with a name:
python_intro.py
def hi(name):
python_intro.py
def hi(name):
if name == 'Ola':
print('Hi Ola!')
elif name == 'Sonja':
print('Hi Sonja!')
else:
print('Hi anonymous!')
hi()
command-line
$ python3 python_intro.py
Traceback (most recent call last):
File "python_intro.py", line 10, in <module>
hi()
TypeError: hi() missing 1 required positional
argument: 'name'
26 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
pass it when calling the function. Let's fix it at the bottom of the
file:
python_intro.py
hi("Ola")
command-line
$ python3 python_intro.py
Hi Ola!
python_intro.py
hi("Sonja")
command-line
$ python3 python_intro.py
Hi Sonja!
Now, what do you think will happen if you write another name in
there? (Not Ola or Sonja.) Give it a try and see if you're right. It
should print out this:
command-line
Hi anonymous!
27 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
python_intro.py
def hi(name):
print('Hi ' + name + '!')
hi("Rachel")
command-line
$ python3 python_intro.py
Hi Rachel!
Loops
python_intro.py
python_intro.py
28 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
python_intro.py
def hi(name):
print('Hi ' + name + '!')
command-line
$ python3 python_intro.py
Hi Rachel!
Next girl
Hi Monica!
Next girl
Hi Phoebe!
Next girl
Hi Ola!
Next girl
Hi You!
Next girl
As you can see, everything you put inside a for statement with
an indent will be repeated for every element of the list girls.
You can also use for on numbers using the range function:
python_intro.py
29 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
command-line
1
2
3
4
5
Note that the second of these two numbers is not included in the
list that is output by Python (meaning range(1, 6) counts from
1 to 5, but does not include the number 6). That is because
"range" is half-open, and by that we mean it includes the first
value, but not the last.
Summary
That's it. You totally rock! This was a tricky chapter, so you
should feel proud of yourself. We're definitely proud of you for
making it this far!
30 di 31 13/11/2017, 19:53
Introduction to Python · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_introduction/
31 di 31 13/11/2017, 19:53
Python installation · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_installation/
tutorial.djangogirls.org
But first, let us tell you what Python is. Python is a very popular
programming language that can be used for creating websites,
games, scientific software, graphics, and much, much more.
1 di 5 13/11/2017, 19:52
Python installation · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_installation/
One thing to watch out for: During the installation you will notice a
window marked "Setup". Make sure you tick the "Add Python 3.6
to PATH" checkbox and click on "Install Now", as shown here:
2 di 5 13/11/2017, 19:52
Python installation · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_installation/
1. install all Windows Updates and try to install Python 3.6 again; or
3 di 5 13/11/2017, 19:52
Python installation · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_installation/
It is very likely that you already have Python installed out of the
box. To check if you have it installed (and which version it is),
open a console and type the following command:
command-line
$ python3 --version
Python 3.6.1
4 di 5 13/11/2017, 19:52
Python installation · Django Girls Tutorial about:reader?url=https://tutorial.djangogirls.org/en/python_installation/
command-line
command-line
If you're on older Fedora versions you might get an error that the
command dnf is not found. In that case you need to use yum
instead.
command-line
command-line
$ python3 --version
Python 3.6.1
If you have any doubts, or if something went wrong and you have
no idea what to do next, please ask your coach! Sometimes
things don't go smoothly and it's better to ask for help from
someone with more experience.
5 di 5 13/11/2017, 19:52
# Compara due interi tra di loro e stamp il ris.
def ritorna_il_piu_grande(x,y):
if x > y:
#print(str(x) + " è più grande di " + str(y))
return x
# elif x == y:
# print(str(x) + " è uguale a " + str(y))
else:
#print(str(x) + " è più piccolo di " + str(y))
return y
#stampa_numeri(10)
def add_students():
# finché gli alunni sono meno di 10, aggiungi nuovo
while len(alunni) < 10:
alunni.append('Alunno')
#add_students()
# Saluta tutti
def salutatore():
for nome in alunni:
print("Ciao " + nome)
return None
#salutatore()
y = f(2)
print(y)