CS101 Python

¡Supera tus tareas y exámenes ahora con Quizwiz!

For Loops

- 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

While loops

-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

File closing syntax:

.close() Ex: Closing a file is important! If you don't do this Python won't read or open your file because it holds this in memory buffer.

List

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.

While Loops:

A While loop permits code to execute repeatedly until a certain condition i s met. This is useful if the number of iterations required to complete a task is unknown prior to flow entering the loop.

Print

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"

Single Line Comments:

Augmenting code with human readable descriptions can help document design decisions. Example: # this is a single line comment

Functions

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

For Loops:

Python provides an iteration syntax. Note the colon and indentation. Ex: >> for i in range(0, 3): ... print(i*2)...0 2 4 >> m_list = [ "apple", "orange", "coconuts"] >> for item in m_list:>> print(item)apple orange coconuts >> w = "Swift">> for letter in w: ... print(letter) ... S w i f t

String

Strings store characters and have many built-in convenience methods that let you modify their content.

The str() function:

Using the _____ allows you to represent the content of a variable as a string, provided that the data type of the variable provides a neat way to do so. str() does not change the variable in place, it returns a 'stringified' version of it.

print()

a built-in function to display on screen in Python

Variables

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

Sets

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'}

File Input/Output Syntax:

f = open("output.txt", "w") Syntax explanation:This told Python to open output.txt in "w" mode ("w" stands for "write").We stored the result of this operation in a file object 'f'. Ex:You can open files in write-only mode ("w"),read-only mode ("r"),read and write mode ("r+"),and append mode ("a", which adds any new data you write to the file to the end of the file).

input():

function that enables user to insert their number Ex: >> number = input("Type in a number: ")

The range() function:

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]

File reading syntax:

myFile.readlines() Ex: infile = open("demo3.txt","r") line = infile.readline()while line != "": line = infile.readline() # Process the line. print(line)

File writing syntax:

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()

The len() Function:

returns the number of items contained in a list. Example: >> mylist = [0,4,5,2,3,4,5] >> len(mylist)7

String

set of characters represented in quotation marks

float(x)

to convert x to a floating-point number.

int(x)

to convert x to a plain integer


Conjuntos de estudio relacionados

Chapter 43. Sleep and the Nursing Process EAQ

View Set

Section 5: Quiz 53 - Network and Endpoint Devices

View Set

IB BUSINESS - OPERATIONS MANAGEMENT

View Set

Kappa Alpha Psi - Cumulative Test - 1

View Set

Respiration 5: Control of Respiratory System

View Set

BUSI-230 Probability & Statistics - Module 2

View Set