Changes

Jump to navigation Jump to search

Workshop: Introduction to programming (with Python)

2,210 bytes added, 23:13, 12 September 2019
Basic Operators
*int(2.9999) → 2
*int("1964") → 1964
 
 
'''Lists'''
 
In Python a list is indicated by square brackets and commas. Define a list like any other variable, except enclose all the list elements in square brackets and separate them by commas. '''For example: myList = [var1,var2,var3].''' The elements of a given list can be recalled by typing the name of the list followed with the index of the item enclosed in square brackets. '''For example: myListLists[0] ⟶ var1.''' Lists can be modified during runtime with methods such as these:
 
*myList'''.append('''x''')''' - Adds a new element (x) to the end of the list
*myList'''.remove('''x''')''' - Removes the first item with the specified value (x)
*myList'''.index('''x''')''' - Returns the index of the first element with the specified value (x)
*myList'''.insert('''x''')''' - Adds an element at the specified position (x)
*myList'''.clear()''' - Removes all the elements from the list
<br />
==Basic Operators==
'''Arithmetic Operators:'''
 
*Addition ('''+''') - Adds values
*Subtraction ('''-''') - Substracts values
*Division ('''/''') - Divides values
*Modulus ('''%''') - Divides values and returns the remainder
*Multiplication ('''*''') - Multiplies values
*Exponent ('''**''') - Performs an exponent
*Equal ('''=''') - Sets a variable equal to a value
 
 
'''Comparison Operators:'''
 
*Is equal? ('''==''') - Checks if two variables have an equivalent value
*Is not equal ('''!=''') - Checks if two variables have different values
*('''>''', '''<''') - Greater than and lesser than
*('''>=''', '''<=''') - Greater than or equal to and lesser than and equal to
*'''IS''' - Checks if two variables are the exact same (share the same memory location)
 
 
'''Logical Operators:'''
 
*'''AND''' or '''&''' - If both the statements are true then condition becomes true
*'''OR''' or '''|''' - If any of the statements are true then condition becomes true
*'''NOT''' - Used to reverse the logical state
*'''IN''' - Checks if an element exists inside of a sequence (or list)
 
<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:
==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 loopsyou can choose from, 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]]'''<code>while ('''statement'''):''' </code>'''''*code to repeat*'''''<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!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]]
==Other Educational Resources for Python==
118
edits

Navigation menu