Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

BASIC OPERATORS CONVERTING BETWEEN TYPES CONDITIONAL STATEMENTS

int(value)​ converts value to integer if <condition>:


+​ ​addition Ex: int(“2”) = 2 <do this>
-​ ​subtraction int(4.9) = 4 decimal portion drops off
*​ ​multiplication Code indented inside happens if the condition
/ ​ ​division float(value)​ converts value to a float is True
**​ ​power/exponent Ex: float(5) = 5.0
float(“3.14”) = 3.14 If-else
//​(​integer division​; division without remainder) Use when there are 2 possibilities
Ex: 7//3 = 2 str(value)​ converts value to a string
Ex: str(2.5) = “2.5” if (condition):
%​ (​modulus​; remainder from division) str(1) = “1” <do this if condition is True>
Ex: 7%3 = 1 else:
<do this when condition is
False>
from math import * input()
if-elif-else
Allows us to use more math functions: All input comes in as a string, so if we want to
Use when there are more than 2 alternative
use the inputted value in calculations, we need
paths
sqrt(x) to convert to either an integer or float.
cos(x), sin(x), tan(x) if (condition1):
acos(x), asin(x), atan(x) Ex: ​age=int(input(“Enter your age:”)) <do this if condition1 is True>
log(x), log10(x) elif (condition2):
exp(x) ​ this is ex <do this if condition2 is True>
COMPARISON OPERATORS …
NOTE​: trig functions take in radian values, not It allows us to compare two values; results in a else:
degrees, so you might need to convert Boolean value. <do this is ​all​ above conditions
are False>
==​ ​Equality
VARIABLES !=​ ​Inequality
<variable name> = <value to <, <=​ ​less than, less than or equal to NOTE​: you can nest conditional statements
assign> >,​ ​>=​ ​greater than, greater than or equal to Ex: ​num = int(input(“Enter a #:”))
If num > 5:
TYPES BOOLEAN OPERATORS print(“Greater than 5”)
Integers (Ex: 1, 2, 100, 0, -20) and, or, not If num <= 47:
Floats (Ex: 1.01, 2.0, 3.14, -20.03) print(“Between 5 and 47”)
Booleans (True or False) A ​and​ B (True if both A and B are true) else:
Strings (stuff in quotes; Ex: ‘Howdy!’, “72.3”) A ​or​ B (True if either A or B is true, or if both print(“Greater than 47”)
are true)
Do not include (-) or (‘’) in a variable name not​ A (“reverses” A; True if A is False, False if
A is True)
WHILE LOOP TESTS LISTS
Repeats the indented code until the condition Tests should be thought of before you write We can use lists to store several values of the
is False. any code same type.

while <condition>: Comments <list name> = [ ]


<do this> Include comments within your code to clarify
your code , computation, and purpose. [ ] indicates a list, and individual elements are
If the condition is True, all the indented code is separated by commas.
run. After the code is run, the condition is Typical cases
checked again; if a condition is still​ true​, the You use this case to test for common possible ACCESSING ELEMENTS IN A LIST
code is run again, and so on. If False the code errors within your code. This is what the user <list name>[<element number>]
is skipped. should give you.
The first item in a list is at element 0, the
print(“guess number between 1 and Edge or Corner cases second is at element 1, and so on.
0.”) You use this case to counter special error
Secret_number = 7 cases that would be less common to occur Ex: ​grades = [92, 84, 89, 96]
User = int(input(“guess number”)) within your code. What happens if the user print(grades[0]) outputs 92
While user != secret_number: messes up. (invalid inputs) print(grades[2]) outputs 89
print (“no.try again”)
User = int(input(“guess Logical errors OTHER LIST COMMANDS
number”)) Referring to compiler or syntax errors within len(x)​ length, returns the number of
print(“correct”) the code. elements in list x

<list name>.append(<element to
add>) ​adds an element to the end of the list

del <list name>[<element number>]


removes the item at that position

<list name>.pop()​ removes the last item


in the list

Ex: ​grades = [92, 84, 89, 96]


grades.append(79)
adds 79 to end of list
del grades[1] removes 84
​grades.pop()​ removes 79
FOR LOOP More list Commands
Repeats the indented code a specific number Used to manipulate or interact with lists.
of times whether the condition is true or false. Sequence type commands
len(list)​ length of the list.
i = 0 min(list)​ minimum value in the list.
For i in range < condition >: max(list)​ maximum in the list.
< do this > sum(list)​ sum of all variables in the list.

The for loop is used when you have a known list.index(val) ​finds the index of the
number of iterations or when you want to first element in the list whose value matches
iterate through a specific, known set of items. val.

List = [a,b,c,d] list.count(val) ​ finds the number of


For i in range List: occurrences of the val in the list.
< do somthing >
List[start: end]​ allows you to collect
certain values in a list.

You might also like