Programming for Everybody - Python - Coursera (Module 1)
Input Function
We can instruct Python to pause and read data from the user using the input() function. The input () function returns a string.
Type Conversions (int and float)
When you put an integer and floating point in an expression, the integer is implicitly converted to a float. You can control this with the built in functions int() and float() to convert types
Reserved Words
Words that have predefined meanings that cannot be changed. You cannot use reserved words as variable names/identifiers
The try / except Structure
You surround dangerous section of code with try and except. If the code in the try works - the except is skipped If the code in the try fails - it jumps to the except section5
def (reserved word)
used to create a new user defined function def poem (): print ("there once was a man from nantucket") Now you can use the function poem later to mean print "there once was a man from nantucket"
if / else (reserved words) - Two Way Decisions
x = 4 if x > 2 : print ('Bigger') else : print ('Smaller') print 'All Done' x = 4 will produce Bigger x = 1 will produce Smaller
+ in Python
Python knows the difference btwn an integer and a string It knows it means addition with numbers e.g. 2 + 4 It knows it means "concatenate" with strings e.g. 'Hello ' + 'there'
Remainder Operator
The percent sign; when it is used with two integers, the result is an integer with the value of the remainder after division takes place
Boolean (Variable)
A variable that has a value of True or False
Operators
+ Addition - Subtraction * Multiplication / Division ** Power % Remainder
Comparison Operators
< Less than <= Less than or Equal to == Equal to >= Greater than or equal to > Greater than != Not equal Note* = is used for variable assignment == is used to say if this is equal to then .....
Nesting
An indent within an indent.
Boolean Expressions / Comparison Operators
Boolean Expressions ask a question and produce a Yes or No result which we use to control program flow. Boolean expressions using comparison operators evaluate to True / False or Yes / No Comparison operators look at variables but do not change variables.
Assignment Statement
Consists of an expression on the right-hand side and a variable to store the result. x = x - 3.9 * x * (1 - x )
max
Finds the largest in a set of values.
Constants
Fixed values such as numbers, letters, and strings are called constants bc their value does not change. Numeric constants are numbers. String constants are sing quotes (') and double quotes (")
if (reserved word)
If x > 5 : print ('Hello') Python will print hello if x is greater than 5 and won't print hello is x 5 or less. Note* The indent (4 spaces) of the second line of the if statement matters. Some languages like Java and C++ it doesn't but in Python it does. Everything in the indent will happen if the condition is met. Un-indent to start new if statement.
Integer Division
Integer division produces a floating point result
while (reserved word)
Like the if reserved word, but allows you to perform a repeating loop (Indefinite Loop) of an if statement n = 5 while n > 0 print (n) n = n - 1 print ('Blastoff!') print (n) This will print 5 4 3 2 1 Blastoff! 0. The indentations after the while will repeat until n > 0 and then will run the remaining of the code, printing Blastoff! and n.
Repeated Loops
Loops (repeated steps) have iteration variables that change each time through a loop. Often these iteration variables go through a sequence of numbers.
Variable Name Rules (Python)
Must start with a letter or underscore_ Must consist of letters, numbers, and underscores Case Sensitive
Secondary Memory
Slower large permanent storage - lasts until deleted - disk drive / memory stick. Where your codes live when not in direct use by the Main Memory and CPU
Function
Sort of like a variable that holds code A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions using def
None (Variable)
The None keyword is used to define a null variable or an object. In Python, None keyword is an object, and it is a data type of the class NoneType. None is not the same as False. None is not 0. None is not an empty string. Comparing None to anything will always return False except None itself.
Indenting
The indent (4 spaces) of the second line of the if statement matters. Some languages like Java and C++ it doesn't but in Python it does. Everything in the indent will happen if the condition is met. Un-indent to start new if statement. Using Tab is dangerous as some editors add 5 spaces and some add a tab. Python doesn't like tabs it likes spaces.
Comments in Python
Use the # character. Anything after # is ignored.
break (reserved word)
break statement ends the current loop and jumps to the statement immediately following the loop. while True: line = input ('>') if line == 'done' : break print (line) print ('Done!') The program will ask for an input, if you right something "dogs are cool" the program with skip over the break and print "dogs are cool" it will then loop back to the input line and you can write something else. It will keep doing this until you write "done" at which point the break code line will be triggers and you will exit the while indent block and the program will print ('Done!')
continue (reserved word)
continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration. break skips out of the loop continue skips to the beginning of the loop = abandons the current loop and goes to the next iteration of the loop
Parameter
def grad (letter): if letter >= 90 'A': elif letter >= 80 'B': elif letter >= 70 'C': the parameter. grad is the function letter is the parameter Later when we are using the grad function we will always put something in the () next to it to give it the relevant parameter print grad (90) for instance will simply print an A
return (reserved word)
def greet ()": return "Hello" print (greet (), "Jarrod") This will print Hello Jarrod
Main Memory
fast small temporary storage - lost on reboot - aka RAM Where Secondary Memory (aka Storage) sends program to be read, which then sends to CPU.
in (as part of definite loops)
for i in [4, 2, 10] used with for in making code for definite loops
try / except code (reserved words)
greeting = 'Hello Jarrod' try: hello = int (greeting) except: hello = -2 print ('First' , greeting) Because this program will fail (b/c greeting is a string that can't be converted to an integer) it will skip that and go to the except clause and will put -2 into the variable hello.
Multi-way and elif
if x < 2 : print ('Small') elif x < 10 : print ('Medium') elif x < 20 : print ('Big') elif x < 40 : print ('Large') else: print ('Huge') elif is a combination of else and if. The program goes sequentially down the line. If < 2 print small, if >2 and < 10 print Medium, if >2 >10 and <20 print Big etc...
Finding the Largest so Far
largest_so_far = -1 print ('Before' , largest_so_far) for the_num in [9, 41, 12, 3, 74, 15] if the_num > largest_so_far : largest_so_far = the_num print (largest_so_far, the_num) print ('After' , the largest_so_far)
for (reserved word)
like the while reserved word but allows for definite loops (as opposed to indefinite loops) for i in [5, 3, 8, 10] : print (i) print (Blastoff!) the iteration variable (i) iterates though the sequence. the indented block of code is executed once for each value in the set. Then the program moves out of the code block and prints Blastoff after each item in the set has been printed once.
Variable
named place in memory where a programmer can store data and later retrieve their data using variable "name" Programmers choose names of variables. Can change contents of variable in later statement.
Infinite Loops
programming loop that runs forever because there is no way to make the while or for statement false. Therefore you need iteration variables or variables that chance each time through a loop.