<?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%2Flecture1v2</id>
	<title>I719 Fundamentals of Python/lecture1v2 - 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%2Flecture1v2"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture1v2&amp;action=history"/>
	<updated>2026-06-16T01:00:15Z</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/lecture1v2&amp;diff=119940&amp;oldid=prev</id>
		<title>Eroman: Created page with &quot;= Lesson 1 =  == Running Python ==  There are executables &lt;code&gt;python&lt;/code&gt;, &lt;code&gt;python2&lt;/code&gt;, and &lt;code&gt;python3&lt;/code&gt; By default on ubuntu and most systems, &lt;code&gt;pyth...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture1v2&amp;diff=119940&amp;oldid=prev"/>
		<updated>2017-04-07T13:38:35Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Lesson 1 =  == Running Python ==  There are executables &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;python2&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;python3&amp;lt;/code&amp;gt; By default on ubuntu and most systems, &amp;lt;code&amp;gt;pyth...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Lesson 1 =&lt;br /&gt;
&lt;br /&gt;
== Running Python ==&lt;br /&gt;
&lt;br /&gt;
There are executables &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;python2&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;python3&amp;lt;/code&amp;gt; By default on ubuntu and most systems, &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; is symlinked to &amp;lt;code&amp;gt;python2&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== python 2 or 3? ===&lt;br /&gt;
&lt;br /&gt;
* v2 is more commonly used&lt;br /&gt;
* v3 is the official standard&lt;br /&gt;
* all the code we write today will run on both versions&lt;br /&gt;
&lt;br /&gt;
=== interactive ===&lt;br /&gt;
&lt;br /&gt;
many interactive shells&lt;br /&gt;
&lt;br /&gt;
* default repl, &amp;lt;code&amp;gt;python3&amp;lt;/code&amp;gt;&lt;br /&gt;
* IPython (recommended), &amp;lt;code&amp;gt;ipython3&amp;lt;/code&amp;gt;&lt;br /&gt;
* bpython, &amp;lt;code&amp;gt;bpython3&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run a python script ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python3 my_script.py&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Writing python ===&lt;br /&gt;
&lt;br /&gt;
The editor must recognize that python requires a 4 space indent.&lt;br /&gt;
&lt;br /&gt;
* Use what ever editor you want&lt;br /&gt;
* If you do not know what to do, use Spyder&lt;br /&gt;
* If you want to learn something new, I recommend learning vim or emacs&lt;br /&gt;
* PyCharm is a popular IDE&lt;br /&gt;
&lt;br /&gt;
== Language basics ==&lt;br /&gt;
&lt;br /&gt;
=== Whitespace dependent ===&lt;br /&gt;
&lt;br /&gt;
* 4 space indent&lt;br /&gt;
* no brackets &amp;lt;code&amp;gt;{&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt; like in java&lt;br /&gt;
* no semicolons&lt;br /&gt;
* indent level replaces brackets&lt;br /&gt;
&lt;br /&gt;
For example in javascript:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;function hi (name) {  // uses &amp;#039;{&amp;#039; to determine what is in function body&lt;br /&gt;
    return &amp;quot;Hi &amp;quot; + name;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
would become in python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def hi(name):  # code indented after the function definition is the function body, and is executed when the function is called&lt;br /&gt;
    return &amp;#039;Hi {}&amp;#039;.format(name)&amp;lt;/source&amp;gt;&lt;br /&gt;
=== Comments and Documentation ===&lt;br /&gt;
&lt;br /&gt;
* use &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt; for comments&lt;br /&gt;
* use &amp;lt;code&amp;gt;&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;#039;&amp;#039;&amp;#039;&amp;lt;/code&amp;gt; to start and close multiline strings that will become documentation&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def hi(name):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    Prepend &amp;#039;Hi &amp;#039; before name. This is documentation&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    result = &amp;#039;Hi {}&amp;#039;.format(name)  # this is a comment&lt;br /&gt;
    return result&amp;lt;/source&amp;gt;&lt;br /&gt;
=== Types ===&lt;br /&gt;
&lt;br /&gt;
* to find the type of a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;type(a)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Strings ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;hello = &amp;#039;Hello World!&amp;#039;&lt;br /&gt;
print(hello)&amp;lt;/source&amp;gt;&lt;br /&gt;
* double or single quotes are fine&lt;br /&gt;
* length of string determined by function &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;&lt;br /&gt;
* concatenate strings with &amp;lt;code&amp;gt;+&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;hello = &amp;#039;Hello&amp;#039;&lt;br /&gt;
world = &amp;#039;World&amp;#039;&lt;br /&gt;
hello_world = hello + &amp;#039; &amp;#039; + world  # &amp;#039;Hello World&amp;#039;&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;substitute variable in a template string with &amp;lt;code&amp;gt;format&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;template_string = &amp;#039;Hi {}&amp;#039;&lt;br /&gt;
hi_bob = template_string.format(&amp;#039;Bob&amp;#039;)  # &amp;#039;Hi Bob&amp;#039;&amp;lt;/source&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Arithmetic ===&lt;br /&gt;
&lt;br /&gt;
* uses operators like most other languages&lt;br /&gt;
* similar to java, &amp;lt;code&amp;gt;1 + 1&amp;lt;/code&amp;gt; returns &amp;lt;code&amp;gt;2&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== functions ===&lt;br /&gt;
&lt;br /&gt;
* to define a function, start with &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;&lt;br /&gt;
* then the function name&lt;br /&gt;
* functions are always in snake case. always lower case and words are separated by underscores&lt;br /&gt;
* add the end of the name, list of arguments in parethesis&lt;br /&gt;
* to document function, use triple quoted string after function definition&lt;br /&gt;
* functions can use named arguments, which allow a default value for the argument. They are called &amp;#039;Keyword Arguments&amp;#039;&lt;br /&gt;
* Arguments can be made in order they are in the function definition, or be named specifically&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
&lt;br /&gt;
lists in Python are like arrays in Java. They do not have predetermined length, and elements can be any type&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;a = [1, 2, 3]  # defines a list&amp;lt;/source&amp;gt;&lt;br /&gt;
=== For loops ===&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; is different from other languages. It is a &amp;amp;quot;foreach&amp;amp;quot; loop. It goes through each element of an iterator.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
0&lt;br /&gt;
1&lt;br /&gt;
2&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
for i in range(3):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; in this example becomes an element of the iterator being iterated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
H&lt;br /&gt;
e&lt;br /&gt;
l&lt;br /&gt;
l&lt;br /&gt;
o&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
for character in &amp;quot;Hello&amp;quot;:&lt;br /&gt;
    print(character)&amp;lt;/source&amp;gt;&lt;br /&gt;
= Tasks =&lt;br /&gt;
&lt;br /&gt;
== Task 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;amp;quot;Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Example Solution ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for x in range(1,101):&lt;br /&gt;
    if x % 15 == 0:&lt;br /&gt;
        print(&amp;quot;FizzBuzz&amp;quot;)  # Catch multiples of both first.&lt;br /&gt;
    elif x % 3 == 0:&lt;br /&gt;
        print(&amp;quot;Fizz&amp;quot;)&lt;br /&gt;
    elif x % 5 == 0:&lt;br /&gt;
        print(&amp;quot;Buzz&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(x)&amp;lt;/source&amp;gt;&lt;br /&gt;
== Task 2 ==&lt;br /&gt;
&lt;br /&gt;
Write a function that meets these requirements:&lt;br /&gt;
&lt;br /&gt;
Args: name: a string exclamation: optional, defaults to &amp;#039;Hi&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Returns: string with name prefixed by the exclamation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt;&amp;amp;gt;&amp;amp;gt; test_function(&amp;#039;Bob&amp;#039;)&lt;br /&gt;
&amp;#039;Hi Bob!&amp;#039;&lt;br /&gt;
&amp;amp;gt;&amp;amp;gt;&amp;amp;gt; test_function(&amp;#039;Sally&amp;#039;, exclamation=&amp;#039;Bye&amp;#039;)&lt;br /&gt;
&amp;#039;Bye Sally!&amp;#039;&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Example solution ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def hi(name, exclamation=&amp;#039;Hi&amp;#039;):&lt;br /&gt;
    return &amp;#039;{e} {n}&amp;#039;.format(n=name, e=exclamation)&amp;lt;/source&amp;gt;&lt;br /&gt;
== Task 2 ==&lt;br /&gt;
&lt;br /&gt;
Write a function that meets these requirements:&lt;br /&gt;
&lt;br /&gt;
Args: numbers - a list of numbers&lt;br /&gt;
&lt;br /&gt;
Returns: Boolean True if an odd number exists in the list of numbers False if only even numbers exist in numbers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt;&amp;amp;gt;&amp;amp;gt; test_function([2,1])&lt;br /&gt;
# 1 is an odd number&lt;br /&gt;
True&lt;br /&gt;
&amp;amp;gt;&amp;amp;gt;&amp;amp;gt; test_function([2,4])&lt;br /&gt;
# Both 2 and 4 are even numbers&lt;br /&gt;
False&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Example solution ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def has_odd(l):&lt;br /&gt;
    for i in l:&lt;br /&gt;
        if i % 2 != 0:&lt;br /&gt;
            return True&lt;br /&gt;
    return False&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>