










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This study guide provides accurate questions and detailed answers with rationales for an introduction to programming in python course. It covers topics such as floating-point precision, string manipulation (replace, slice notation, find), field width, alignment, fill characters, and various string methods (isalnum, isdigit, islower, isupper, isspace, startswith, endswith, capitalize, lower, upper, strip, title, split, join). It also includes information on lists, list methods (append, extend, insert, remove, pop, sort, reverse, index, count), list comprehensions, and dictionaries, along with fundamental programming concepts like input, process, output, variables, and algorithms.
Typology: Exams
1 / 18
This page cannot be seen from the preview
Don't miss anything!











Floating-point Precision ......ANSWER........format specification which indicates how many digits to the right of the decimal should be included in the output of floating types '{:.1f}'.format(1.725) produces 1. Replace (old and new parameters) ......ANSWER........returns a copy of the string with all occurrences of the substring old replaced by the string new; old and new arguments may be string variables or string literals
phrase.replace('one', 'two') will replace any occurrence of the word one with two Replace (old, new and count parameters) ......ANSWER........returns a copy of the string with all occurences of the substring old replaced by the new except only replaces the first count occurrences of old Slice Notation ......ANSWER........my_str[start:end] will create a substring from my_str index start through to end – 1 Slice Notation with Stride ......ANSWER........my_str[start:end:stride] Field Width ......ANSWER........minimum number of characters that must be inserted into a string
my_str.find('!') will return the index of the '!' in the string Find (with x, and start parameter) ......ANSWER........string method that will return the index of the first occurrence of item x in the string but will begin looking at index start my_str.find('!', 3) will produce the index of the first! on or after index 3 Find (with x, start and end parameter) ......ANSWER........string method that will return the index of the first occurrence of item x in the string but will begin looking at the index start and will stop at index end - 1 my_str.find('!', 3, 8) will produce the index of the first! but will start looking at index 3 and stop looking at index 8 - 1
Rfind ......ANSWER........string method that works the same as str.find(x) but looks for the parameter in reverse, returning the last occurrence in the string my_str.rfind('!') will give the index of the last! in the string Count ......ANSWER........string method that returns the number of times x occurs in the string my_str.count('oo') will return the number of times 'oo' occurs in the string isalnum() ......ANSWER........string method that returns true if all characters in the string are lowercase or uppercase letters or the numbers 0- 9 isdigit() ......ANSWER........string method that returns true if all characters are the numbers 0 - 9
lower() ......ANSWER........string method that returns a copy of the string with all characters lowercased upper() ......ANSWER........string method that returns a copy of the string with all characters uppercased strip() ......ANSWER........string method that returns a copy of the string with leading and trailing whitespace removed title() ......ANSWER........returns a copy of the string as a title, with the first letters of words capitalized split() ......ANSWER........string method that splits a string into a list of tokens each token is a substring that forms a part of a larger string parameter is a separator which indicates where to split the string into tokens (blank defaults to space)
join() ......ANSWER........string method that joins a list of strings together with the parameter separating them my_str = '@'.join(['billgates', 'microsoft']) = 'billgates@microsoft' mylist = [] ......ANSWER........creates an empty list mylist = ['apple', 'banana', 'orange'] ......ANSWER........creates a list with the items apple, banana, and orange in it mylist[#] ......ANSWER........will return the list item at the index of
mylist = list('abcd') ......ANSWER........create a list of items a, b, c, and d mylist[start:end] ......ANSWER........create a new list containing only those items at index start and end - 1
list.remove(x) ......ANSWER........list method that Remove first item from list with value x list.pop() ......ANSWER........list method that remove and return element at end of the list list.pop(i) ......ANSWER........list method that removes and returns item at position i in list list.sort() ......ANSWER........list method that sort the items of list in- place list.reverse() ......ANSWER........list method that reverse the elements of the list, in place list.index(x) ......ANSWER........list method that return index of first item in list with value x
list.count(x) ......ANSWER........list method that return the number of times x appears in the list enumerate() ......ANSWER........list function that iterates over a list and provides and iteration counter all(list) ......ANSWER........True if every element in list is True (!= 0), or if the list is empty any(list) ......ANSWER........True if any element in the list is True max(list) ......ANSWER........Find the element in list with the largest value min(list) ......ANSWER........Get the minimum element in the list sum(list) ......ANSWER........Get the sum of all elements in the list
List Comprehension new_list=[expression for item in list if condition] ......ANSWER........do the expression for each item in the list if a condition is true Conditional List Comprehension mylist.sort() ......ANSWER........sort items of a list in ascending order; in-place modification mynewlist = sorted(mylist) ......ANSWER........sorts items of a list in ascending order and creates a new list mynewlist = sorted(mylist, key=str.lower) ......ANSWER........this will put all items in a list to lowercase before sorting them to ensure that a will come before B
mynewlist = sorted(mylist, reverse=True) ......ANSWER........create a new list sorted in reverse dict() ......ANSWER........implements a dictionary dict(Bobby='805- 555 - 2232', Johnny='951- 555 - 0055') dict([('Bobby', '805- 555 - 2232'), ('Johnny', '951- 555 - 0055')]) my_dict[key] ......ANSWER........Indexing operation - retrieves the value associated with key jose_grade = my_dict['Jose'] my_dict[key] = value ......ANSWER........adds an entry if the entry does not exit, else modifies the existing entry my_dict['Jose'] = 'B+'
Variable ......ANSWER........A piece of data that can vary depending on the input. Algorithm ......ANSWER........A sequence of instructions that solves a problem. Python Interpreter ......ANSWER........A computer program that executes code written in the Python programming language. Interactive Interpreter ......ANSWER........A program that allows the user to execute one line of code at a time Statement ......ANSWER........A program instruction, typically appearing on its own line. Expressions ......ANSWER........Code that returns a value when evaluated.
Assignment ......ANSWER........A new variable is created by performing an assignment using the '=' symbol (eg. salary = wage * hours * weeks). Print() ......ANSWER........Displays variables or expression values. Comments ......ANSWER........Used to explain portions of code and are denoted by the '#' character in Python. String Literal ......ANSWER........Text enclosed in quotes. The text can have letters, numbers, spaces, or symbols.