Changes

Jump to navigation Jump to search

Workshop: Introduction to programming (with Python)

3,848 bytes added, 03:35, 10 September 2019
Interactive Python
==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==
Now that we understand the basic workflow of python (create code in text editor → execute in command prompt), we can start discussing the different functions and capabilities of Python. We can start this discussion with variables. You can think of variables as the objects that store information for our program. A variable has two basic parts; '''A name''' (which we will use to refer to the variable) and '''A value''' (the info we care about). Besides those two parts, variables can hold different types of information.
 ===='''Types Of Variables:'''====
#Strings - A sequence of characters (ex: "Python is fun!")
#Integers - Whole numbers (ex: 3, 10, 999)
#Floats - Numbers w/ a decimal (ex: 1.0, 3.14, 7.77)
#Boolean - True or False
#Lists - An sequential list of other variables (floats, integers, strings, more lists, or a mix) (ex: ['cat', 'dog', 999, 3.14, variable1])
 ===='''Defining Variables:'''====
One of the great features of Python is that the built in compiler can intelligently figure out what type of variable your specified variables should be and takes care of this distinction '''automatically'''. This means defining variables is easier in Python than some other languages you may be familiar with. We define a variable by writing its name, followed by an equal sign, followed by the value of the variable (ex: pi=3.14, name="Dylan", eq="3+4"). We can also define variables without setting the values immediately. To do this we need to tell the compiler what type of variable we need (since it can't figure it out automatically) (ex: int pi, str myName)
Important Note: The standard naming convention of variables follows: The first word is always all lowercase then the proceeding words begin with an uppercase letter (ex: myVariable, reallyAwesomeInteger). You don't need to follow this convention, but it is worth knowing.
<br />===='''Updating Variables:'''====
To update a variable (change it's value later on in the code) we simply need to redefine it's value. You can do this by:
variableName = [new value]
'''Casting Variables:'''<br />
===='''Casting Variables'''====
When we get into more complex implementations our variable type will become very important. Ask yourself 'what is the difference between these two codes?'
*int("1964") → 1964
<br /> ==Methods==The next major thing to learn about Python is methods. Methods are functions that act upon objects and variables in your code. You can use methods to obtain information about variables/objects or update their values. And the syntax of a method goes as follows:[[File:Example of Append.png|thumb|An example of a very common method, append, which adds elements to the end of a list. ]][name of object/variable]'''.'''[method]'''('''value''')'''  You can learn about all the different methods for your variables and objects by exploring the documentation of python (or which ever library you are using).  Here are some methods you can use on different variables in Python: *String - capitalize(), lower(), upper(), find()*Int/Float - abs(), long(), pow()*List - append(), count(), pop(), sort(), reverse() <br /> ==If Statements==<br /> ==Loops==One of the most important concepts in procedural programming is the loop. Like the name suggests, a loop simply iterates through some selected lines of code a certain amount of times. In Python there are a few different types of loops, they are: #'''While Loop''' - Like the name suggests, a while loop repeats a block of code while a certain expression is true. This kind of loop is useful when we don't know how many times a loop will actually execute at runtime. The While Loop is composed of two parts; the loop statement and the code block to repeat (indented). The syntax looks like this:[[File:While Loop Example.png|thumb|An example of a while loop]]<nowiki><code></nowiki>'''while ('''statement'''):''' '''''*code to repeat*''''' <nowiki></code></nowiki><br />The while loop will loop over the code block until the statement in parentheses equates to false. The statement can be an algebraic equation or variable. (ex: (n+1)/4 == 1) <br />[[File:Example For loop.png|thumb|2 examples of for loops that yield the same result. ]]#'''For Loop''' - This loop will run once for each element in a list. This kind of loop is useful when iterating over loops and arrays or when we know exactly how many times the code block should run. There are two main ways to use the for loop.##To iterate over a list - Will run the code block once for each element in a list. You can reference the element data during each iteration by putting an element name after "for". Example of this syntax can be seen below: '''for''' element '''in''' list''':''' '''''*code to repeat*''''' <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! == Other Educational Resources for Python == *The Official Python Documentation - https://docs.python.org/3/*Tutorials - https://thepythonguru.com*Challenging Math/Computational Problems - https://projecteuler.net <br /> ==Skills Checklist==
* The official Python Documentation - https:Installation*How to write//docssave .python.org/3/py files*How to enter Interactive Python*Basic Understanding of Variables (how to define, different types, casting)*Basic Understanding of Methods* Tutorials - https://thepythonguru.comBasic Understanding of If Statements (or, and, else, elif)* Challenging Math/Computational Problems - https://projecteuler.netBasic Understanding of Loops (for loop, while loop)
118
edits

Navigation menu