UVU Info1200 Python
What are four ways to include quotation marks in a string?
"Type \"x\" to exit" # String is: Type "x" to exit.' Type \'x\' to exit' # String is: Type 'x' to exit. "Type 'x' to exit" # String is: Type 'x' to exit. 'Type "x" to exit' # String is: Type "x" to exit.
What are python's arithmetic operators?
+ (Addition) - (Subtraction) ** (Multiplication) / (Division) // (Integer division) % (Modulo/Remainder) *** (Exponentiation)
What are the most useful compound assignment operators?
+= Adds the results of the expression to the variable -= Subtracts the results of the expression from the variable **= Multiplies the variable value by the result of the expression (one **) (others include /=, //=, %=, and ***=)(last is 2**)
What can Python be used for?
1. As a scripting language to work with other software applications or to develop system administration programs that perform tasks like automatically backing up a server or rotating log files 2. For data analysis and visualization. Data analysis is the process of cleaning, preparing, analyzing, and modeling data to better understand and use it to make decisions. (Popular for this)
What are the two types of software that are stored on disk?
1. Systems software 2. application software
What are the 3 types of Python applications?
1. console application 2. GUI application 3. web application
What are the two type of errors likely to be found?
1. syntax error. 2. runtime error
What is a GUI application?
A GUI application is an application that uses a graphical user interface (GUI) to interact with the user.
What is a console application and where does it run?
A console application is a desktop application that uses the console to interact with the user. *It runs through a command prompt or shell
What is the float (or floating-point) data type?
A data type that holds numbers with decimal places like 41.4 or -25.78
What is an escape sequence in Python?
A sequence of characters with special meaning when used inside a string or a character. Example: \n, \t etc. The characters that we can't insert into a string are called Illegal characters, and these characters modify the string.
What is a statement?
A statement is an instruction that the Python interpreter can execute. We have seen two kinds of statements: print and assignment. Each statement must be indented properly.
What is a web application and where does it run?
A web application typically gets requests from a web browser, processes them on a web server, and returns the responses to the web browser. *It runs on a browser.
What is addition in python?
Adds two operands.(+)
What is important to remember with input() functions?
All entries are returned as strings, even if the user enters a number.
What is underscore notation?
All letters are lowercase with words separated by underscores. This can also be referred to as snake case. example: variable_name
What is a runtime error?
An error occurs when a Python statement can't be executed as the program is running This type occurs after all the code has compiled cleanly and the program is being executed. Then, an error message is displayed that helps you identify the cause of the error. This type of error is also known as an exception.
What is IDLE?
An integrated development environment (IDE) for Python. Simple Statements can be run in IDLE's shell IDLE's editor can be used to create a Python program The F5 key can be used to run a Python Program in IDLE
What is application software?
Application software consists of the applications that are available on the system. That includes applications such as web browsers, word processors, and spreadsheet programs. The applications software consists of the applications that are available on the system.
What are compound assignment operators?
Are used to simplify arithmetic expressions These operators are a shorthand way to code common assignment operations. (Left to right)
What is a block comment in Python?
Block Comments are coded on their own lines. Example: # initialize variable counter = 0 score_total = 0 test_score = 0
In a syntax summary, what do brackets mark?
Brackets mark the portions of code that are optional.
What does source code compiled into?
Bytecode All you need to take away from this figure is that Python source code is compiled into bytecode that's run by the computer's operating system.
What is python Bytecode?
Bytecode is a low-level representation of the instructions in a programming language. Bytecode can be run by any computer that has the Python virtual machine installed on it. This virtual machine translates the bytecode so it can be run by the operating system of the computer.
The storage capacity of main memory and disk storage is measured in....
Bytes Main memory and disk storage are commonly measured in megabytes (MB) and gigabytes (GB), and a byte is roughly equivalent to one character of data. KB, MB, GB - A kilobyte (KB) is 1,024 bytes. A megabyte (MB) is 1,024 kilobytes. A gigabyte (GB) is 1,024 megabytes.
What is RAM?
Central Processing Unit (CPU) and main memory, which is often referred to as RAM (Random Access Memory) A computer consists of a CPU (Central Processing Unit) and main memory, also known as RAM (Random Access Memory).
How Python compiles and runs source code
Code editor -> source code -> *python interpreter -> bytecode -> *python virtual machine -> operating system
What is an application?
Computer software that performs a task or related set of tasks. (applications can also be referred to as programs)
What is a variable in Python?
Containers that hold data. These can change, or vary, as the code executes. Variables are case-sensitive
What is the int (or integer) data type?
Data type that holds whole numbers like 21 or -25
What is the str (or string) data type?
Data type that holds words and characters like "Mike" or "40"
What is a data type?
Defines the type of data for a value
What is order of precedence?
Determines which operators are applied first. (To override the default order of precedence, you can use parentheses.)
What is division in python?
Divides the right operand into the left. The result is always a floating-point number. ( / )
What are arithmetic expressions?
Expressions that consist of two or more operands that are operated upon by arithmetic operators. The operands in an expression can be either numeric variables or numeric literals. You don't have to code spaces before and after arithmetic operators. Arithmetic expressions are evaluated using the order of precedence - Parenthesis, Exponentiation (^), multiplication (*), division (/), addition (+), and subtraction (-)
What is Python?
High-level programming language It was intended to be a simple, intuitive language that is as powerful as traditional languages, and it succeeded at that.
When do numbers have to be converted to strings? (print function)
If you pass just one string as the argument for a print() function, numbers have to be converted to strings within string argument.
What is a Inline comment in Python?
Inline Comments are coded after statements to describe what they do. Example: while test_score != 999: test_score = int(input("Enter test score: ") ) #User input
What is a function in python?
It is a reusable unit of code that performs a specific task. A block of code that can be called by other statements. Functions are useful because they provide a way to divide code for a program into manageable chunks. This makes it easier to maintain, test, and debug your code.
What is a logic error in Python?
It is when the code runs without any syntax or runtime errors but produces incorrect results due to flawed logic in the code. These types of errors are often caused by incorrect assumptions, an incomplete understanding of the problem, or the incorrect use of algorithms or formulas.
What does it mean when they say Python is opened sourced?
It means that its source code is available to the entire Python community.
shebang line defintion
It specifies the interpreter that should be used to run this program. the code begins with a line that starts with a hash (#) and a bang (!)
What is multiplication in python?
Multiplies two operands. (*)
How many errors can IDLE detect at a time?
One at a time When you try to run a program, IDLE detects one syntax error at a time. For each error, a dialog box is displayed and the point at which the error was detected is highlighted. Thterm-142en, you close the dialog box, fix the error, and try again.
What can a string consist of?
One or more characters, including letters, numbers, and special characters like *, &, #.
What is integer division in python?
Operator rounds the quotient down to an integer value Divides the right operand into the left operand and drops the decimal portion of the result. ( // ) example: 50 // 33 = 1
What is a program (according to our textbook)
Program refers to the short applications that it presents
Why is Python said to be platform-independent?
Python bytecode can be run on any operating system that has a Python virtual machine
Why does indentation matter in Python coding?
Python relies on proper indentation. Incorrect Indentation causes an error. The standard indentation is four spaces whenever it is required.
What is exponentiation in python?
Raises the left operand to the power of the right operand. (**)
What is Modulo/Remainder in python?
Returns the remainder after division. Divides the right operand into the left and returns the remainder. The results are always an integer. (%) example: 25 % 6 = 1
What does Command + P (MacOS) or Alt + P (Windows) do?
Retype the previous entry at the prompt.
Why are escape sequences needed?
So that the Python compiler can interpret the code correctly.
What is subtraction in python?
Subtracts the right operand from the left. (-)
What is systems software?
Systems software consists of the programs that control the operation of the system. That includes the operating system for the computer, like Windows for a PC or macOS for a Mac. The systems software for a computer provides all of the software that is needed for running the applications, including the operating system.
What does it mean that Python 3 isn't backward compatible? What does it mean that Python 2 isn't forward compatible?
That means that Python 3 can't run most programs written for Python 2. That means that Python 2 can't run most programs written for Python 3.
If you are using IDLE to develop python, what don't you need to include?
The Shebang line. (It's considered good practice to include it since it clearly indicates what version of Python should be used to run the program. It also can make it easier to run the program on Unix-like operating systems too)
When is data in the main memory lost?
The data in the main memory is lost when an application ends. Disk storage is used for persistent data storage, which remains after an application ends.
What's the difference between += and + in python?
The difference between both the concatenation operators is that the + creates a new list and the += modifies an existing list in place
When an exception occurs while a program is running....
The program crashes and an error message is displayed
What is syntax in Python?
The set of rules that defines how a Python program will be written and interpreted.
What can escape characters be used for?
To format strings. \n will create a new line \ is used to escape a character (it will be interpreted literally or used to preform some action like tabbing or adding a new line)
What does it mean to concatenate strings in python and how do you do it?
To join strings. To do it you can use either the + operator or an f-string. F-string is more concise than + operator and they get the same results.
What is a string literal?
To specify the value of a string, you can enclose the text in either double or single quotations marks.
What is persistent data storage?
To store the data of an application, disk storage is commonly used. This type of storage remains after the application ends, so it is called persistent data storage
What is a assignment statement?
Uses the equal sign to assign a value to a variable. Example: first_name = "Joe"
What is an argument in python?
Values that are passed into a function. (Functions can also be arguments if they are passed into other functions) (Make sure to separate multiple arguments with commas within the function)
When can a named argument be used
When passing in arguments (age = 22)
What is chain functions?
When you code one function as the argument of another function. This is a common coding practice.
What is implicit continuation in python coding?
Whenever Python gets to the end of a line of code and sees that there is more to come because a parenthesis ( ( ), square bracket ( [ ), or curly brace ( { ) has been left open. You can divide statements after parentheses, brackets, and braces, and before or after operators like plus and minus signs. When you divide a statement, it is a good practice to indent its continuation lines. example: print("Total Score: " + str(score_total) + "\nAverage Score: " + str(average_score))
Can the print () function accept one or more data arguments?
Yes • The sep argument can be used in the print() function to separate words by different characters instead of the default space. • The end argument can also be used to change the ending character from the default \n character.
What is explicit continuation?
You can use the backslash ( \ ) character to divide statements anywhere on a line. example: print("Total Score: " + str(score_total) \ + "\nAverage Score: " + str(average_score)) THIS IS DISCOURAGED AND ISN'T USUALLY REQUIRED!
How to fix the exception that occurs when you try to perform an arithmetic operation on a string?
You can use the int() and float() functions.
What is a comment in Python?
You code a # at the start of each line and they are ignored by the compiler.
How do you assign an empty string to a variable?
You code a set of quotation marks with nothing between them. This means that the string doesn't contain any characters.
After you write the code for a program, what is your goal when you test the program?
Your goal is to find all of the errors (or bugs) in the program. Whenever you find a bug, you need to debug it by fixing it
What are the common escape sequences?
\n (new line) \t (tab) \r (return) \" (Quotation mark in a double quoted string) \' (Quotation mark in a single quoted string) \\ (backslash)
A runtime error is also known as:
an exception
What are Python's keywords? (can't be used as a variable name)
and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
You can use escape sequences to include....
certain types of special characters such as new lines and tabs. You can also use escape characters to include special characters such as quotation marks and backslashes.
How do you call a function?
functionName() you code the name of the function followed by a pair of parentheses. Within the parentheses, you code any arguments that the function requires, and you separate multiple arguments with commas.
What is the syntax for calling any function?
function_name( [ arguments ] )
What is python source code?
human-readable computer instructions written by programmers. It's written in plain text. To create the file that contains this source code, the programmer uses a text editor or integrated development environment (IDE),
When don't numbers have to be converted to strings? (print function)
if more than one string is used
What is the input function for getting data from the console?
input([prompt]) This pauses the program and waits for the user to enter data at the console. When the user presses Enter, this function returns the data entered by the user as a str value.
Python is considered a good first language to learn because:
it has a simple syntax it has most of the features of traditional programming languages it is open source
What is an example of a f-string?
message = f"{name} is {age} years old" string() function is not needed
What is an example of chaining to get the int value in one statement?
quantity = int ( input("Enter the quantity: "))
The round() function can be used to ....
round a floating-point number down to a number of specified digits. example: number = 2.123456 round(number, 2) #this will round down to 2.12
When working with floating-point numbers, be aware....
that they are approximations, not exact values. This can cause inaccurate results.
What is camel case?
the first word is lowercase and the subsequent words start with a capital letter. example: variableName
The float() function can be used to ....
to retrieve a floating-point number from an input
Python interpreter is used...
to translate (or compile) the source code into bytecode.
What is a syntax error?
violates one of the rules of Python coding so the source code can't be compiled. These are errors that occur because the Python coding rules have been violated. As a result, the program can't be compiled.
How to code literal values
• To code a literal value for a string, enclose the characters of the string in single or double quotation marks. This is called a string literal. • To code a literal value for a number, code the number without quotation marks. This is called a numeric literal.
How to use the python shell
• To test a statement, type it at the prompt and press the Enter key. You can also type the name of a variable at the prompt to see what its value is. • Any variables that you create remain active for the current session. As a result, you can use them in statements that you enter later in the same session. • To retype the previous statement on Windows, press Alt+p. To cycle through all of the previous statements, continue pressing Alt+p. On OS X, use Command+p.
What are the guideline for using Comments in Python?
• Use comments to describe portions of code that are hard to understand, but don't overdo them. • Use comments to comment out (or disable) statements that you don't want to test. • If you change the code that's described by comments, change the comments too.
Rules for naming variables
• Use meaningful names that are easy to remember. • A variable name must begin with a letter or underscore. • A variable name can't contain spaces, punctuation, or special characters other than the underscore. • A variable name can't begin with a number but can use numbers later in the name. • A variable name can't be the same as a keyword that's reserved by Python. • Use underscore notation or camel case.
How do disk storage and main memory work together?
• When you start the computer, it loads the operating system into main memory. Then, you use the features of the operating system to start an application. • When you start an application, the operating system loads it into main memory. Then, it runs the application. • As the application runs, it may read data from disk storage into main memory or write data from main memory to disk storage.
What are three functions for working with numbers in python?
• int (data) --- converts the data argument to the int type and returns the int value • float (data) --- Converts the data argument to the float type and returns float value. • round (number, [, digits] --- Rounds the number argument to the number or decimal digits in the digits argument. If no digits are specified, it round the number to the nearest integer.