Python Exam 1 Flashcards
How do humans solve problems?
cognitive abilities and intuition
How do computers solve problems?
Rely on computational power and algorithmic precision
different types of programming language
machine language, assembly language, high-level language
machine language
the limited set of instructions that a CPU can understand directly
Each instruction sequence of 1s and 0s
Binary digits or bits for shorts
the CPU executes the instructions
Assembly language
Each instruction is defined by a short abbreviation
The CPU can not understand assembly language directly –> translated into machine language using a processor
High-Level Language
For more readability and portability high-level language:
uses English like language
machine-independent
portable
examples: python, Pascal, C, C++, java, Fortron
Must be translated: 2 primary ways of translation: compiler and interpreter
2 primary ways of translation
compiler and interpreter
compiler
is a program that reads SOUCE CODE and produces a STAND-ALONE EXECUTABLE that can then be run
interpreter
program that directly executes the instructions in the source code w/o requiring them to be compiled into an executable first
About Python
high level general purpose
interpreted language
code executed line by line
run code without compiler
Why python?
relatively simple
pre-written software
widely used
general purpose
Python file extension
.py
What is a terminal also called?
terminal, console, output
string
a sequence of text characters
starts and ends with a “quote” character or a ‘quote’ character
quotes do not appear in output
String rules
strings surrounded by “” or ‘’ may not span multiple lines
strings surrounded by “” may not contain a “ character (unless using esc. seq)
strings surrounded by ‘’ may not contain a ‘ character (unless using esc. seq)
Escape sequence
used to represent certain special characters in a string
\t
\n
"
'
\
\t
tab key
\n
new line character
\”
quotation mark character
'
quotation mark/apostrophe character
\
backslash character
string addition
use plus operator +
comments
are notes written in source code by the programmer to describe/clarify the code
not displayed
# or “”” “””
Data types
a category or set of data values
constrains the operations that can be performed on data
float
float
real numbers
have decimal point
int
integer
integer
complex
another type of data (ex: 3 + 4j) that I don’t need to know
+
addition
-
subtraction (or negotiation)
*
multiplication
/
division
%
Integer division (aka leave off any remainder)
//
integer
**
exponent
what happens if you divide by 0 in python?
error
uses of %
obtain the last digit of a number
obtain the last 4 digits
see when a number is odd
algorithms
a list of steps for solving a problem
functions
a named group of functions
procedural decomposition
dividing a problem into functions
making a new function syntax python
def name():
statement statement … statement
how to call a function in python
name()
control flow in python
when a function is called, the program’s execution jumps int that function, executes its statements, then jumps ack to the point where the function was called
place statements into a function if
The statements are related structurally, and/or
The statements are repeated
You should not create functions for
an individual print statement
Only blank lines
Unrelated or weakly related statements
precedence
Order in which operators are valued
Precedence in Python:
(**): Highest precedence
(*), (/), (//), (%): Same precedence are evaluated after exponentiation
(+),(-): Least precedence
What data type is printed in this case: Integer (+ , - , //, *,) integer
integer
What data type is printed in this case: Integer / integer
float
What data type is printed in this case: Integer (+ , - , //, *,) float
float
variable
a piece of computer’s memory that is given a name and type and can store a value
declaring a variable
Declaring /initiate it - state its name and type and store a value into it
Variable declaration and assignment
Variables must be declared before they can be used
The value can be an expression; the variable stores its result
Syntax: name = expression
what = means
store the value at right in variable at left
printing with multiple strings
add commas in between string and variable
types of loops
for and while loops
range (start, end)
start is inclusive
end is exclusive
import random
imports the random module
random.random()
returns a random float in the range (0.0, 1.0]
random function we know
random.random()
random.randint(min, max)
random.randint(min, max)
returns a random integer in the rand [min, max)