Python Midterm
repetition operator
* used to generate a string made up of multiple copies of another string
Principles embraced by the Python programming language
* beautiful is better than ugly *explicit is better than implicit *simple is better than complex *complex is better than complicated *readability counts
print function
*a built in function that writes text output to the console window *arguments can be of any data type
function
*a collection of programming statements that are given a name *when you call a function the statements of the function are executed *modularity and reusability *ends with a return statement if the function returns a value
variable
*a name used to refer to a value stored in the computer's memory *in computing holds a particular value at any moment in time
character string data type
*a sequence of characters enclosed in single or double quotes *sequence type *strings are objects *enclosed in single or double quotes
what is the convention for naming Python variables?
*a variable name should use lowercase letters and an underscore to separate words
Python Shell
*an interactive interpreter that runs each line of code as soon as you enter it *bottom part of thonny *shell prompt: >>>
what is the python shell?
*an interactive window in which Python statements and expressions are executed immediately *great for experimenting with Python code without having to save the code in a separate program file
identifier
*any name you make up when writing a program
for loop
*can be used to iterate through the elements of any Python sequence *one of Python's repetition statements which allows you to execute a group of statements multiple times * for iteration variable in sequence
rules of an identifier
*can only be composed of letters (A-Z or a-z) *digits (0-9) *and the underscore character (_) *they CAN NOT begin with a digit
concatenation
*combining two or more strings into one longer string *done using the + operator
str function
*converts a numeric value to a string *string constructor *strings are immutable
turtle.setheading(angle)
*explicitly set the heading to a particular angle *the current heading is irrelevant *angles measured from right and counterclockwise *floating points can be used in this *can be negative as well aka clockwise
methods
*functions associated with objects *called using the . operator *functions that are part of classes
modules
*have to be imported to be used in a program *examples: math, random, string, turtle, datetime, os, urllib
compiler
*programs like Java and C++ *code is first translated by a compiler into an executable form which can then be run *compiled first into a different format before being executed
default turtle direction
*right/east *centered in the window *blacks line *one pixel wide
sep argument in the print function
*specifies the separator that should be used between each argument value printed *default = a single space *sep=',' = separates each argument by a comma
end argument in the print function
*specifies what to print after displaying its other outputs *default = a newline character (next output starts on a new line) *end='space' = single space be printed after the string *end='empty' = output of each print statement is followed immediately by the next
assignment statement
*stores a value in a variable using the assignment operator *an expression is evaluated and the result is stored in the variable *left hand side = variable *right hand side = simple value or complex expression
pythonic
*used to describe Python code that embraces the spirit and purpose of the language *code that exploits features of the language tat may not be found in other programming languages
What operator is used to perform string concatenation in Python?
+
python codes are saved with a .what at the end?
.py
string indexes start where?
0
Which of the following statements is true? 1. The print statement is a call to a function 2. The print statement sends text output to the editor 3. A print statement must be the first statement in a Python program 4. The print statement sends text output to a printer
1. The print statement is a call to a function
If a variable called pioneer refers to the string 'Grace Murray Hopper', what is the result of the expression len(pioneer)?
19
What issue must be addressed when printing a long character string? 1. The print method cannot print character strings 2. The print method has a maximum number of characters it can accept 3. A regular string literal cannot span across multiple lines 4. The string must be subdivided into sections of 50 characters or less
3. A regular string literal cannot span across multiple lines *either have to concatenate or use triple-quoted string (''' ''' ,""" """)
assignment operator
=
equality operator
==
What is the Python Standard Library?
A collection of programming components that can be used in any Python program *installed automatically
Python embodies elements of which programing paradigm? 1. Procedural programming 2. object-oriented programming 3. functional programming
All three!
True or False - A Python program must be compiled into an executable form before it can be run
False
True or False - A Python program must be enclosed in curly braces {} to be executed
False
True or False - In dynamically typed languages, variables are declared to store a specific type of data
False
True or False - The effect of the setheading depends on the current heading of the turtle
False
True or False - The goto command moves the turtle without drawing a line
False
True or False - The value assigned to a variable must be numeric
False
True or False - Using components from the Python Standard Library is a rare occurrence
False
True or False - You must use a print statement to display the result of an expression in the Python shell
False
True or False -Python variables that represent integers are NOT object reference variables
False *ALL values in python are object references
True or False - Python 3 is backwards compatible to Python 2.
False *Python 2 code will not necessarily run in a Python 3 environment *support for python 2 officially ended in 2020
True or False - The origin point (0,0) of the turtle coordinate system is in the upper left corner of the graphic screen
False *its in the center of the screen
True or False - In Python, the assignment statement is also an expression
False *the assignment operator does not return a value
True or False - The assignment operator has a higher precedence than the arithmetic operators
False *the assignment operator has a lower precedence than the arithmetic operators
True or False - A turtle draws a filled circle using the fill_circle command
False *this command doesn't exist
True or False - The Python programing language was so named after a snake discovered in the creator's office?
False - it was named after the Monty Python comedy group
True or False - Running a program that contains errors will cause the Thonny development environment to terminate
False - the program will terminate but Thonny will not
Who developed the Python programming language? in what year?
Guido van Rossum in 1989
Thonny is considered a what?
Integrated Development Environment (IDE)
What does the term case sensitive mean?
The difference between uppercase and lowercase letters matter (An uppercase A and a lowercase a are different in Python)
True or False - According to the TIOBE index Python is one of the most used programming languages
True
True or False - Built-in functions of the Python Standard Library can be used without being imported
True
True or False - Full documentation about the Python Standard Library can be found online
True
True or False - Python is a general-purpose programming language, appropriate for solving problems in many areas of computing
True
True or False - The Python shell is great for exploring Python language features
True
True or False - The Python turtle graphics module is based on the programming language Logo developed in the 1960s
True
True or False - The output of a Python program run in Thonny appears in the shell window
True
True or False - The pen color and size determines the color and width of the lines drawn
True
True or False - The turtle circle command can be used to draw a regular polygon
True
True or False - The value assigned to a variable could be a floating point number
True
True or False - Thonny displays code using syntax highlighting
True
True or False - You can store a value in a variable in the Python shell
True
True or False - the value of a variable can change throughout a program
True
True or False - The position of a circle depends on the current heading of the turtle
True *circle is drawn to the left of the turtle
True or False - The Python shell has an integrated help system
True *end the help system by typing quit
True or False - You can not change the contents of Python character string once it has been created
True *strings are immutable
True or False - Python variables are created using an assignment statement
True * a variable is created the first time it is assigned a value
True or False - Thonny has a package manager that lets you install and update external Python packages
True * you can also install packages by using the pip command
index operator
[ ] used to refer to a particular character in the string
object references
a Python variable is a name that refers to an object somewhere in memory
program
a sequence of instructions that can be interpreted and executed by a computer
What is an IDE?
a software that combines the tools that a programmer uses to write, save, edit, and run code
integer data type
a whole number
default argument
an argument that has a default value, making it optional
console window
another name for the output window
keyword arguments
arguments that you specify by name when the function is called *examples are end and sep
turtle.forward(n)
causes the turtle to move forward n pixels along its current heading
turtle.bgcolor('color')
changes the graphics background color *default is white
tutrle.title('title')
changes the title in the title bar of the graphics window
turtle.setup(w,h)
changes the width and height of the turtle graphics window
in operator
checks to see if one string is contained in another string
floating point number data type
decimal point and a fractional part
data type
determines the nature of the data and the kinds of operations that can be performed on it
assistant window
in thonny a window displayed whenever needed to provide additional information about errors
shell window
in thonny where the output of a saved program is displayed when the program is run
editor window
in thonny where you write and revise saved Python programs
Python is a ___ language
interpreted language *means it is executed one line of code at a time using a software tool called a Python interpreter
Print statement
prints a line of output to the system output window
len function
returns the length/number of characters in a in a string *one higher than the index of a string
not in operator
returns true if the first string is not contained in the second
What is syntax coloring?
showing certain elements of program code in different colors
scripts
small, saved programs
'' and "" are called what?
string literals
if more than on argument is passed to be printed
they are printed on the same line separated by a space *example print('hi', 'katie') = hi katie
docstrings
triple-quoted strings used to add multi-line comments to Python functions
turtle.left(angle)/ turtle.right(angle)
turns the turtle a certain degree RELATIVE to the turtle's current heading
Which way would the turtle be facing after executing the command turtle.setheading(135)
up and left (northwest)
If the turtle is currently facing up (north), which way would it be facing after executing the command turtle.right(45)
up and right (northeast)