<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture4</id>
	<title>I719 Fundamentals of Python/lecture4 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture4"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture4&amp;action=history"/>
	<updated>2026-05-07T04:34:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture4&amp;diff=118278&amp;oldid=prev</id>
		<title>Eroman: Add lecture 4</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture4&amp;diff=118278&amp;oldid=prev"/>
		<updated>2017-03-02T03:33:42Z</updated>

		<summary type="html">&lt;p&gt;Add lecture 4&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Exceptions ==&lt;br /&gt;
&lt;br /&gt;
this is a good overview:&lt;br /&gt;
&lt;br /&gt;
https://docs.python.org/3/tutorial/errors.html&lt;br /&gt;
&lt;br /&gt;
== Slices ==&lt;br /&gt;
&lt;br /&gt;
== TASK 1 ==&lt;br /&gt;
&lt;br /&gt;
print out bitcoin price in console for EUR.&lt;br /&gt;
&lt;br /&gt;
=== Example output ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;1064.4876&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== install requests ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;pip3 install requests&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== use requests ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import requests&lt;br /&gt;
&lt;br /&gt;
# make an HTTP GET request, and assign &amp;#039;r&amp;#039; to response object&lt;br /&gt;
r = requests.get(my_url)&lt;br /&gt;
&lt;br /&gt;
# convert response to python types, if response is JSON, and assign to &amp;#039;data&amp;#039;&lt;br /&gt;
data = r.json()&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;my_dict = {&amp;#039;bpi&amp;#039;: []}&lt;br /&gt;
# access value for key &amp;#039;bpi&amp;#039;&lt;br /&gt;
my_dict[&amp;#039;bpi&amp;#039;]&lt;br /&gt;
print(my_dict[&amp;#039;bpi&amp;#039;])&amp;lt;/source&amp;gt;&lt;br /&gt;
== MAKE A WEBSITE ==&lt;br /&gt;
&lt;br /&gt;
* Django&lt;br /&gt;
* Flask&lt;br /&gt;
* Bottle&lt;br /&gt;
* Falcon&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;shell&amp;quot;&amp;gt;pip3 install django&lt;br /&gt;
python3 -m django startproject first_website&lt;br /&gt;
cd first_website/first_website&lt;br /&gt;
touch views.py&lt;br /&gt;
&lt;br /&gt;
cd ../ # go back to the original first website dir&lt;br /&gt;
python3 manage.py runserver&amp;lt;/pre&amp;gt;&lt;br /&gt;
Add a view&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;first_website/first_website/view.py&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;from django.http import HttpResponse&lt;br /&gt;
&lt;br /&gt;
def my_view(request):&lt;br /&gt;
    return HttpResponse(&amp;#039;hi&amp;#039;)&amp;lt;/source&amp;gt;&lt;br /&gt;
Add the view to the urls&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;first_website/first_website/view.py&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;from first_website.views import my_view&lt;br /&gt;
&lt;br /&gt;
urlpatterns = [&lt;br /&gt;
    url(r&amp;#039;^admin/&amp;#039;, admin.site.urls),&lt;br /&gt;
    url(&amp;#039;$&amp;#039;, my_view)&lt;br /&gt;
]&amp;lt;/source&amp;gt;&lt;br /&gt;
=== Running an interactive shell with django ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python3 manage.py shell&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
get a nicer shell&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;pip3 install ipython&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Query string ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;http://google.com/?q=1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the query string is converted to a dictionary like object available as the property &amp;lt;code&amp;gt;GET&amp;lt;/code&amp;gt; of the &amp;lt;code&amp;gt;request&amp;lt;/code&amp;gt; object&lt;br /&gt;
&lt;br /&gt;
== Task 2 ==&lt;br /&gt;
&lt;br /&gt;
Make a page that gives the bitcoin price in euros&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import requests&lt;br /&gt;
&lt;br /&gt;
from django.http import HttpResponse&lt;br /&gt;
&lt;br /&gt;
def my_view(request):&lt;br /&gt;
    # NOTE: requests with an &amp;#039;s&amp;#039; is the library&lt;br /&gt;
    r = requests.get(&amp;#039;https://api.coindesk.com/v1/bpi/currentprice.json&amp;#039;)&lt;br /&gt;
    data = r.json()&lt;br /&gt;
    price = data[&amp;#039;bpi&amp;#039;][&amp;#039;EUR&amp;#039;][&amp;#039;rate&amp;#039;]&lt;br /&gt;
    response = HttpResponse(price)&lt;br /&gt;
    return response&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>