loops/repetitious statement & string processing
Every program must have a header listing the following information:
#filename.py #programmer name #UMW Honor Pledge #Explanation of what the program does
which Using operator w/ string:
using the + allows to add strings for one output AND * to print string multiple times Ex of using addition for string: fullName = lastName + "," + firstName Ex of using multiplication for string: sillyGreeting = "Hello" * 3>> print sillyGreeting >>> HelloHelloHello
Line length: Maximum line length is ____ characters,
80
Using continue if loop
: allows to return to the beginning of the loop based on the result of a conditional test>>
When using the * with string variables, Python interprets this as ________. When using the + with string variables, Python interprets this as _________-
1. repetition 2. concatenation
Select the variable name that is NOT valid: 1stname _firstName First_Name name1st
1stname is NOT valid
concatenation
Attaching two things side-by-side for code
How to avoid infinite loops:
CTRL-C or just close the terminal window displaying your program's output. OR test every while loop and make sure the loop stops when you expect it to.
What is PEP-8?>>
Coding Guidelines for the Python Language
A loop that executes a specified number of times is called a/an_ _________ _ while a loop that executes until an expression changes value is called a/an __ _____.
Definite Loop Indefinite Loop
T/F A string variable can only include letters or special characters.
False
T/F In Python, comments can only be in a block format.
False
Common error in loops:
Infinite loop as common loop error: it's either always or never satisfied>> always satisfied BUT then the programmer can never change or break out of the loop
Spaces w/ colons:
No extra space before a comma, semicolon, or colon. Do use extra space after a comma, semicolon, or colon, except at the end of the line.
What is the purpose of the following Python code? lastName = input("Enter user's last Name: ") numOfChars = len(lastName) lastName[numOfChars] = "-"
Nothing, 'str' object does not support item assignment in Python.
what do variables need to be based on coding standards
Variable names shall be valid and descriptive
How to use modulus operator (%) to determine if even or odd:
When one number is divisible by another number, the remainder is 0, so the modulo operator always returns 0 SOO even BUT if not then odd SOOO use if-else statement
How a flag can end a program:
acts as a signal to the program to end the progarm,
Block comments
apply to some (or all) code that follows them and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).
Comments for coding standards
block comments generally apply to some (or all) code that follows them and are indented to the same level as that code Use # An inline comment is a comment on the same line as a statement
input for strings:
can store a string as an input statement for a variable and say print the variable
inline comment
comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.
How to use indents:
do 4 spaces NOT the tab
how to find string's length:
do answer = len(identifierOfStringVariable)>> ex is numberOfChar = len(lastName)
How to get/print only certain characters in a string:
do the substring = __________[__:__] >>> Ex: substring = lastName[0:3]>> where last name is smith it'd print smi cuz it's the first 3 characters
What are four items that must be included in a program header for CPSC 110, Section 5?
filename (*.py), programmer name, UMW Honor Pledge, program description
How to print each string character on new line:
for ch in stringName: put loop body here Ex for ch in lastName print(ch, "*")
initialization step
happens before loops & sets up correct starting vale
syntax/rule for event controlled/indef loop:
have initialization then while statement w/ indented part for body of the loop, anything indented = repeated, when loop ends w/ the False statement the rest of program continues
In order to set up steps for a iteration, all processing steps that are applied to each item must be WHAT 2 THINGS
identical & idented the same
To properly execute a short palindrome checker program, determine the missing line of code. my_str = 'alola' rev_str = reversed(my_str) <missing line of code> print("The string is a palindrome.") else: print("The string is not a palindrome.")
if list(my_str) == list(rev_str):
Characters can be WHAT
letters, #s, or special characters,
string is what
made up a list of characters.
Using break to exit a loop:
meant to stop loop @ that point, ex = for while-if-else & have break after the if the program stops @ break point if true
Ask the user to enter their name. Then use a loop to print each character in their name on a separate line of output.
name = input ("What is your name?") What is your name? Keegan print (char)
Write a loop that prints your name 10 times.
name = input ("What is your name?") count = 10 while (count < 10) print (name)
Using while loop w/ list & dictionary:
need to use WHILE statement w/ lists & dict cuz allow to collect, store, and organize things,
No extra space before the WHAT
open paren/bracket that starts an argument list, indexing or slicing.
Use _________________ for the input statement
parentheses
loop update step
part of body where loop = modified to tells when something = false
output for string & How to check string's value: using the ==, useful to make sure entered write input
password = input("Please enter your password") if (password == "password"): print("You entered the value password as your password." print("That's silly!")
input() function does what
pauses your program and waits for the user to enter some text.>> Input uses a prompt to tell what to do>> this is what is inside ()
CHARACTERS in a string: w/ the index position
starts out w/ input place 0, to get the character stored in the string at position X (identifierOfStringVaraible[X]),
Remember that Python treats all input as a WHAT by default.
string
Text is stored in the WHAT data type.
string
for loop
takes a collection of items and executes a block of code once for each item in the collection
body for loop
tells what to do for condition
condition/loop test
the boolean statement that says if do the set actions based on condition
How to address when have numerical value as answer to prompt & want to compare to other number:
the entered data (number) is treated like a string so have to change the compared value to int(___)
How to let user choose when to end the loop:
use the quit function to end the program by saying while _____________ != quit so you can enter info until want to end and then you enter quit AND then use if statement not have it output "quit
Tracing loops:
used to predict what specific code you look @
T/F In order to use a count controlled loop to iterate over all the characters in a string, you must first find the length of the string.
true
T/F Spacing is one item that can make code more readable.
true
In an event-controlled loop, be sure to do WHAT
update the variables used in the loop expression appropriately so that the condition will eventually evaluate as False and cause the loop to end.
While loops:
use a while loop to count up through a series of numbers, runs as long as, or while, a certain condition is true.
How to turn string into number:
use the int function to change the number character to be treated like a number and then can use + & * operators Ex: idNumber = input("Enter an id number: ") 12345 firstCharacter = idNumber[0] firstDigit = int(firstCharacter) print(firstCharacter*2)>> 11 print(firstDigit*2)>> 2
Event controlled / ndefinite loop:
when don't know times you'll repeat something Ex of event controlled/indef loop: Wash. Rinse. Repeat as needed., Keep dealing cards until the player says "stay"
The Python reserved word that indicates that a loop is being coded in the program.
while
how To have multiple prompts
write the first one ( prompt = ______) then enter for new line w/ 2nd prompt where it is += to add the prompts (prompt +=)