Docsity

Python Programming Fundamentals: Loops, Functions, and File I/O, Exams of Computer Science

Toggle sidebar
bg1
While loops - ANSWER -executes a series of steps while some condition is
true
-the test (part between the parentheses) returns either true or false
-loop continues until the test returns false
-while loops are used when we don't know how many times we need to repeat
the steps... it relies on an external condition
Ex: while (there are still lines in a file)
read the next line
print the line out
For Loops - ANSWER - executes a series steps of steps a finite amount of
times-text evaluates a counter, when it reaches the last value, the loop stops
repeating-we use a for loop when we know ahead of time how many times, we
need to repeat the stepsEx: for (count = 1 to 12)do a stepdo another stepincrease
the count by one
print() - ANSWER a built-in function to display on screen in Python
String - ANSWER set of characters represented in quotation marks
int(x) - ANSWER to convert x to a plain integer
float(x) - ANSWER to convert x to a floating-point number.
Variables - ANSWER are assigned values using the '=' operator, which is not
to be confused with the '==' sign used for testing equality. A variable can hold
almost any type of value such as lists, dictionaries, functions.
Ex: >> x = 12 12
Functions - ANSWER Python builds ___ using the syntax:
def function_name(variable): Functions can be stand-alone or can return values.
Functions can also contain other functions.
Ex: def add_two(a, b): c=a+b
pf3
pf4

Partial preview of the text

Download Python Programming Fundamentals: Loops, Functions, and File I/O and more Exams Computer Science in PDF only on Docsity!

While loops - ANSWER - executes a series of steps while some condition is true

  • the test (part between the parentheses) returns either true or false
  • loop continues until the test returns false
  • while loops are used when we don't know how many times we need to repeat the steps... it relies on an external condition Ex: while (there are still lines in a file) read the next line print the line out For Loops - ANSWER - executes a series steps of steps a finite amount of times-text evaluates a counter, when it reaches the last value, the loop stops repeating-we use a for loop when we know ahead of time how many times, we need to repeat the stepsEx: for (count = 1 to 12)do a stepdo another stepincrease the count by one print() - ANSWER a built-in function to display on screen in Python String - ANSWER set of characters represented in quotation marks int(x) - ANSWER to convert x to a plain integer float(x) - ANSWER to convert x to a floating-point number. Variables - ANSWER are assigned values using the '=' operator, which is not to be confused with the '==' sign used for testing equality. A variable can hold almost any type of value such as lists, dictionaries, functions. Ex: >> x = 12 12 Functions - ANSWER Python builds ___ using the syntax: def function_name(variable): Functions can be stand-alone or can return values. Functions can also contain other functions. Ex: def add_two(a, b): c=a+b

return c# or without the interim assignment to c def add_two(a, b): return a + b Sets - ANSWER collections of unique >> new_set = { 1, 2, 3, 4, 4, 4,'A', 'B', 'B', 'C'} but unordered items. Ex: >> new_set = { 1, 2, 3, 4, 4, 4,'A', 'B', 'B', 'C'} List - ANSWER A Python data type that holds an ordered collection of values, which can be of any kind. This is equivalent to an "array" in many other languages. Python lists are "mutable," implying that they can be changed once created. String - ANSWER Strings store characters and have many built-in convenience methods that let you modify their content. The len() Function: - ANSWER returns the number of items contained in a list. Example: >> mylist = [0,4,5,2,3,4,5] >> len(mylist) Single Line Comments: - ANSWER Augmenting code with human readable descriptions can help document design decisions. Example: # this is a single line comment Print - ANSWER A function to display the output of a program. Using the parenthesized version is arguably more consistent. Ex:>> # this will work in all modern versions of Python >> print("some text here")"some text here" The range() function: - ANSWER it returns a list of integers, the sequence of which is defined by the arguments passed to it. Argument variations:- range(terminal)- range(start,terminal)- range(start,terminal,step_size) Example: >> [ i for i in range(4)][ 0, 1, 2, 3]>> [ i for i in range(2, 8)] [ 2, 3, 4, 5, 6, 7] >> [ i for i in range(2, 13, 3)] [ 2, 5, 8, 11] For Loops: - ANSWER Python provides an iteration syntax. Note the colon and indentation.

File writing syntax: - ANSWER myFile.writelines("Data to be written") Ex: # Ask for the input and output files' names inputFileName = input("Input file name: ") outputFileName = input("Output file name: ")

Open the input and output files. infile = open(inputFileName, "r") outfile =

open(outputFileName, "w")

Read the input and write the output. total = 0.0count = 0line = infile.readline()

while line != "": outfile.write(line) value = float(line) total = total + value count = count + 1 line = infile.readline()

Output the total and average.

outfile.write("\n") outfile.write(str(total)) outfile.write("\n")avg = total / count outfile.write(str(avg))

Close the files. infile.close() outfile.close()