Docsity

Python Coding Q&A: ENGR 102 Exam Prep, Exams of Nursing

Toggle sidebar
bg1
TAMU ENGR 102 (python
codi ng) – actual solution – A+
x += c - ANS-x = x + c
a -= 3 - ANS-a = a - 3
x *= 4 - ANS-x = x * 4
y /= 3 - ANS-y = y / 3
what are some different types of variables? - ANS-Integers, Floating-Point,
Strings, Booleans
what is an integer? - ANS-whole numbers, no decimals/fractions
what are floating-point numbers? - ANS-numbers with a decimal point
what is boolean? - ANS-single value either true or false
what are strings? - ANS-way of describing text (you can use either single
quotes or double quotes)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Coding Q&A: ENGR 102 Exam Prep and more Exams Nursing in PDF only on Docsity!

TAMU ENGR 102 (python

coding) – actual solution – A+

x += c - ANS-x = x + c a -= 3 - ANS-a = a - 3 x *= 4 - ANS-x = x * 4 y /= 3 - ANS-y = y / 3 what are some different types of variables? - ANS-Integers, Floating-Point, Strings, Booleans what is an integer? - ANS-whole numbers, no decimals/fractions what are floating-point numbers? - ANS-numbers with a decimal point what is boolean? - ANS-single value either true or false what are strings? - ANS-way of describing text (you can use either single quotes or double quotes)

float(3) - ANS-value becomes 3. int(4.9) - ANS-has the value 4 str(10*1.0) - ANS-has the value '10.0' what is the boolean value of true/false? - ANS-true is assumed to have the value 1 false is assumed to have the value 0 x = 3 y = 4 print(str(x) + ':' + str(y)) - ANS-3: how to ask the user for input? - ANS-x = float(input()) == - ANS-equal to != - ANS-inequality a = 10 b = 10 c = 20 d = ((a>b) and (b<=c)) or (not((((c<=a+b) and (a==10)) or ((b==10) and c!=10)))) print(d) - ANS-false temperature = 100 is_hot = (temperature > 85)

sum = 0 for i in range(10): sum += i+ print(sum) - ANS- what are nesting loops? - ANS-that is when you nest one loop inside another for i in range(3): print("i is", i) for j in range(3): print(" j is", j) break - ANS-i is 0 j is 0 i is 1 j is 0 i is 2 j is 0 what does the "break" statement do? - ANS-it immediately stops that iteration in the loop! for i in range(10): if i%2 == 1: continue print("i is", i) - ANS-i is 0 i is 2 i is 4

i is 6 i is 8 grades = [87, 93, 75, 100, 82, 91, 85] grades[1] = 10 grades[2] -= 90 grades[3] = grades[1]+grades[2] print(grades[3]) - ANS-- how to find the length of a list? - ANS-print(len(x)) how do you add an element to a list? - ANS-.append() grades = [87, 93, 75, 100, 82, 91, 85] print(grades[0], grades[6]) - ANS-87 85 what is list slicing? - ANS-this means to take a list and pull out a subpart of the list how would you list slice? - ANS-[a:b] grades = [87, 93, 75, 100, 82, 91, 85] print(grades[0:3]) print(grades[4:5]) print(grades[-3:-1]) - ANS-[87, 93, 75] [82] [82, 91]

from turtle import * forward(100) left(90) forward(100) left(90) forward(100) left(90) forward(100) left(90) #To return to starting orientation input() #So that the window doesn't disappear - ANS-(program draws a square) t.right(90) - ANS-turtle turns right 90 degrees t.penup() - ANS-lifts turtle pen up from canvas how is turtle motion similar in behavior to robotic controls? - ANS-just like we combine turtle motions to make more complex shapes, we can combine robot motions to create more complex actions file basics - ANS-•first "open" the file (specify how we will work with the file) •then, work with the file contents (reading/writing) •finally, "close" the file format for opening files - ANS- = open("", "") what do the different file designators mean: r:

w: a: r+: - ANS-•reading (we will read data from the existing file) •writing (we will write data to a new file) •appending (we will append data to an existing file) •will read from AND write to the file how would you open a file named Results.out so that it can be written to, and assigning the designator to a variable called output_file - ANS-output_file = open('Results.out', 'w') what is the alternative way to opening a file with an assignment and later closing? - ANS-with as : (when you finish with the indented portion, then the file is automatically closed) format for writing files - ANS-.write() different formats for reading files - ANS- = .readline() = .read() difference btwn = .readline() and = .read() - ANS-.read() will read the entire file into one single string which could be problematic bc it can be a REALLY large string. .readline() will convert all the lines in the file into a list of strings and each element of the list is a string, giving one line of the program. what is the split method? - ANS-it will convert a string into a list of strings, based on a separator that is specified.

is assignment allowed in tuples? - ANS-no, it does not support item assignment. however, you can assign values from a tuple. len(x) - ANS-this function returns the character length of a string x how to import individual functions from a module - ANS-from import how to import all the functions from a module - ANS-from import * (not the safest import method, you could bring in a new function that conflicts/overrides an existing function) format for defining a function - ANS-def (): what is the difference between a local variable and a global variable? - ANS-- local variable is defined in the context of just a function (not shared between functions) -global variable is defined throughout the program (anything in the main code) defF(a): a += 1 return a a += 10 return a x = F(4) print(x) - ANS-

what are examples of a mutable data type? - ANS-lists, dictionaries what are examples of immutable data type? - ANS-boolean, integer, float, tuple, string types of errors - ANS-syntax errors, run-time errors, logic errors syntax error - ANS-an error that results when an instruction does not follow the syntax rules or grammar of the programming language. run-time errors - ANS-error that occur when the program is being executed logic errors - ANS-occurs when the program runs and executes, but the output is not what you wanted/expected how to handle a run-time error - ANS-try: except: types of built in exceptions - ANS-TypeError, OSError, ZeroDivisionError