Professional Documents
Culture Documents
● Start with function name, after there is a pair of parentheses: ( ), followed by a parameter inside the parentheses.
Then Mr. Aitonean said to them “Put your phone away or I will read your messages out loud to everyone!”
\t tab
\n new line
\\ backslash
2.2 - Variables & Strings
Variables act as "storage locations" for data in a program. Just like math, it is a letter or a word that is used to represent a piece of data.
Operators are symbols used to perform operations or processes. Although the = symbol is what
we use for an equal sign in math class, this symbol is an the assignment operator in Python, not an equality operator (we'll see what the
equality operator is later this unit).
Rules:
● Variable names can only contain letters, number, and underscores
● Variable names cannot start with a number
● No space, use underscores to separate words
● Avoid Python Keyword (we’ll see these later on) and stick to lowercase
● Variable names should be short but descriptive. E.g. name is better than n, student_number is better than s_n.
In python you can redefine a variable and it will only keep track of your most recent variables.
● The main reason to write comments is to explain what your code is supposed to do and how you are making it work.
● If you leave a project and come back after a long time, the comments can be there as a summary to help understand what is going on.
● Do not OVER COMMENT unless it’s a “to-do” list
2.4 - Numbers I
● Strings are sets of characters joined to form words or sentences
● Integers are positive and negative whole numbers
● Floats are decimal numbers
● Booleans are true or false values
● In Python, we do this by wrapping the variable in parentheses and placing the name of the new data type in front.
Note that when int() takes a decimal number,
it doesn't round it to the nearest integer –– it cuts off the all decimal digits.
Float Imprecision: Sometimes we get inaccurate answers when we are working with decimal numbers.
round( ): We can use the round( ) function to round numbers to any number of decimal places.
● Operation goes inside the bracket with a comma and the number of decimals you want it rounded too.
Multiple Assignment: You can use one line to assign multiple values
to multiple variables.
import random: It might be beneficial to you at some point to create a random number (Think dice games or times
when you don’t want to enter test data from the keyboard).
You need to import the random library and then use the some of the different functions inside it.
2.6 - Intro To Lists: What are Lists? A list is a collection of items in a particular
order. You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family.
● It’s a good idea to make the name of your list plural, such as letters, digits, or names
● In Python, square brackets [ ] indicate a list, and individual elements in the list are separated by commas
Accessing Elements in a List: Item in a list called an element or
a value
Python numbers the positions of elements in a list from left to right. This is referred to as the index
● Using this counting system, you can get any element you want from a list by subtracting one from its position in the list.
● You can also get the last element from the list by asking for index -1
.append() method
➔ This allows you to add an element to the end of your list.
input( ) and .append( ): Try to come up with a way that we can use input ( ) and .append ( ) together to add an item to a list? NO GOOGLE,
just try.
Splitting Strings: .split() method. The split method will separate the string based on wherever whitespace occurs. It stores that information in a
list.
You could even force it to split a String that is separated by any character, such as a comma. That character is often called a delimiter in
programming.
3.1 - If Statements:
Basic Comparison:
If-elif-else Chain: Sometimes you’ll have to evaluate more than 2 possible situations.
● Python reads these test from top to bottom. As soon as it finds a condition that is True, it will skip the rest of the test.
If-else Statements: So far, If statements are only executed when the first condition is True. What happens when it is False?
We can use an if-else block, which allows you to define an action to take when a conditional test fails. Let's look at the voting example again.
Using And/Or:You can check multiple conditions by chaining together comparisons with and or operators.
● and - only passes if BOTH conditions are True
● or - passes when either condition is True
3.2 - For Loops & Loops in Lists: a loop is used to repeat a block of code until a specified
condition is satisfied.
People will often use the generic term “iterate” when referring to loops; iterate simply means “to repeat”.
User input and for Loops: This next example asks the user how many times to print using the input function we talked about last chapter in the
input function. Go ahead and try the program out.
Previous slide:
Python repeats the loop as long as the condition i < 10 is true. Because 0 is less than 10, Python prints 0 and then adds 1, making the current
number 1. Because 1 is less than 10, Python prints 1 and adds 1 again, making the current number 2, and so on. Once the value of i is no long
less than 10, the loop stops running and the program ends.
Letting the User quit loops:A very common operation is to loop until the user performs a request to quit. Give this code example a try.
3.5 - Random numbers
After this, random numbers can be created with the randrange function. For example, this code creates random numbers from 0 to 49. By
default the lower bound is 0.
At the very least, make sure you can write out the answers for the first eight problems. That will give you enough knowledge to continue on.
3.7 - Functions
Defining a function doesn’t cause the computer to do anything. It is like giving a recipe to the computer. Give someone a recipe for banana
bread and they know how to make it. They haven’t actually made it yet, they just know how. You have to tell them to make banana bread. That
is, after we define the function we must call the function before the code in it runs
To call a function, type the function name and follow it by parenthesis. Do not use def, that is only used when we define what the function
does, not when we tell the computer to run it.
You may see these terms used interchangeably, but there is a subtle difference between them.
Returning Values:Functions can not only take in values, functions can return values.
For example, here is a function that returns two numbers added together. See how it uses a return statement on line four to return the result out
of the function.