Changes

Jump to navigation Jump to search

Workshop: Introduction to programming (with Python)

814 bytes added, 19:00, 7 November 2019
update workshop checklist to conform to standard pedagogy.
{{Workshop header}}
 
===Python===
Some Beginner Questions:
 
*What do you know about Python?
*Do you have any previous experience with Python or other programming languages?
 
 
Python Basics:
 
*Python is a flexible, high-level, procedural scripting language.
*It offers support for object-oriented programming and integrating libraries.
*It's super widely adopted!
 
<br />
 
==Welcome to Introduction to Programming!==
===Setting up your work station===
#'''Download Python -''' You can download it [https://www.python.org/downloads/ here]
#'''Check that your installation was successful -''' We will do this by checking the version of python we just downloaded. We can do this by opening up our command prompt and simply typing "python --version". If you did everything right you should see a message that looks something like this: "Python 3.7.3"
#'''Let's test our distribution''' - Let's get our first introduction into Python programming by making a hello world file.
==Basic Hello World Program==
A hello world program is the most basic program you can write to test that your distribution is working smoothly. [[File:Hello world example.png|thumb|The print statement will simply print out "Hello world!" to the console when ran.]]A hello world program is the most basic program you can write to test that your distribution is working smoothly.
#'''Open a text editor''' - This is where we will be writing all of our code. There are many different editors you can use, so feel free to choose the one you are most comfortable with. The only requirement is that the text editor should be able to product plain text files. Windows users already have Notepad installed. Mac users have TextEdit (other text editors include TextWrangler, Atom, and much more).
#'''Write your code''' - To make this program as simple straightforward as possible we will simply write a print statement. After writing your print statement save the file to your Desktop (or whatever other directory you want) as "hello'''.py'''". All python files need to have the extension .py#'''Execute the code''' - Now is the fun part, running the code! Open up the command prompt (Terminal for Mac Users, Powershell for Windows). Navigate to the directory where you saved the file (ex: cd Desktop). Now execute your python code by typing "python [filename]"[[File:Hello World Executed.png|thumb|Execute the code!]]<br />
==Interactive Python==
According to [[wikibooks:Python_Programming/Interactive_mode|wikipedia]], "Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole". Essentially what this means is Interactive Python is good for quick testing and debugging in the command line. Enter it simply by typing "python" and then hit enter.
==Variables==
#Floats - Numbers w/ a decimal (ex: 1.0, 3.14, 7.77)
#Boolean - True or False
#Lists - An A sequential list of other variables (floats, integers, strings, more lists, or a mix) (ex: ['cat', 'dog', 999, 3.14, variable1])
===='''Defining Variables'''====
Answer: 1.5 + 3 = 4.5 but 4.5 is a float value. So for code #1 the answer is 4.5, but the "sum" variable in code #2 is an integer so the answer will be rounded down to 4.
It because of cases like these that sometimes we implement a technique known as casting. Casting simply means changing converting the type of information a variable holds. With casting we can turn a an int to a float, float to an int, str to an int, etc. We cast by writing the variable type we want ("float", "int", "str", etc.) followed by the variable/value we want to cast enclosed by parenthesis.
Examples:
*myList'''.insert('''x''')''' - Adds an element at the specified position (x)
*myList'''.clear()''' - Removes all the elements from the list
 
(We will discuss what methods are in a later section.)
<br />
*String - capitalize(), lower(), upper(), find()
*Int/Float - abs(), long(), pow(), round()
*List - append(), count(), pop(), sort(), reverse()
Okay, now we know how to run code if our statement is true. Now how do we do tell the program what to do if the statement is false? What if we have multiple criteria to check? This is where else and elif statements come into play. Multiple elif statements can be placed directly under the if statement. They allow you to write additional statements that will be evaluated only if the if statement isn't fulfilled. The else statement goes at the end of your if statement (and elif statements if you have them). Simply put, the else statement will run a code block only if the elif and if statements aren't fulfilled.
<br />
 
<br />
##To iterate a certain number of times - Use Python's range function to repeat the code block a certain number of times. The syntax for range is: range (start, stop[, step]). Just like in the other type of for loop, you can reference each element of the range function but naming it. The syntax for this for loop goes: '''for ('''element '''in range('''start, stop[, step]''')):''' '''''*code to repeat*''''' <br />
#'''Nested Loops''' - Loops can be nested inside of each other! For each iteration of the outer loop, the entire contents of the loop will be ran to completion (including other nested loops).
#'''Breaks and Continues''' - "Break" and "continue" are statements which can be placed inside of a loop to modify the loop for special cases. Break is used to break out of (or exit) a loop. Continue is used to stop the current iteration of the loop and start again from the beginning.   [[File:Break Flowchart.jpg|frameless]][[File:Continue Flow Chart.jpg|frameless]]
<br />
==Magic Conch Demo==
[[File:Magic Conch Logic Flowchart.png|thumb|Flowchart logic of the Magic Conch Demo]]
Together we will build your first real program, a [https://www.youtube.com/watch?v=H-IUE0idp7k magic conch shell ] demo (Magic 8-Ball). This project will combine all of the information we just learned.
The picture to the right outlines the basic logic of our program.
The link to the example code is [https://pastebin.com/Tm5HLTzV here].
==Other Educational Resources for Python==
<br />
==Workshop checklist==
===Learning Objectives===
By the end of this Workshop, you should:
 
#be able to explain the purpose of programming languages.
#understand the difference between a script and the interactive shell.
#know the basic functionality of programming languages: sequencing, selecting, and iterating.
#have a basic understanding of computational thinking.
==Skills Checklist=Measurable Outcomes===By the end of this Workshop, you should be able to:
*#run Python Installationscripts on your system*How to write/save #use the interactive shell on your system.py files*How to enter Interactive Python*Basic Understanding #utilize the basic parts of Variables (how to definea program: variables, different types, casting)*Basic Understanding of Methods*Basic Understanding of If Statements (orobjects, and, else, elif)functions.*Basic Understanding of Loops (for loop, while loop, nested loop, break #code and continue)*Make test a program which accomplishes a Magic Conch Shell Demo!small task.

Navigation menu