CMPSC132 Quiz 1

Ace your homework & exams now with Quizwiz!

Interpreters

-A kind of program to process high-level languages into low-level languages source code ---> interpreter ---> output

Identifiers

-A name used to identify a variable, function, class, module or other object -Starts with a letter A to Z or a to z or an underscore followed by more letters, underscores and digits (0 to 9) -Python doesn't allow punctuations characters such as @, $, and % within identifiers -Python is a case sensitive programming language so employee_id does not equal Employee_id

Interpreting Python Programs - What happens when we enter python hello.py at the command shell prompt?

-Python tells the OS to load the python interpreter into memory and run it -We invoke python with a command line argument which python reads after it starts running -Since the command line argument was the name of the file(hello.py), the python loads the file named by the argument and executes the Python code in it

Str Exs: 1. "hello world!".find("1") 2. "banana".find("nan") 3. "banana".replace("ba", "choco") 4. "banana".split("nan") 5. "We are Penn State!".split() 6. "co".join(["cho", "late"]) 7. "".join(["cho", "co", "late"]) 8. " spaces. ".strip() 9. "banana".upper() 10. Input: "banana".isupper() Output: False Input: "BANANA".isupper() Output: True 11. Input: "1523".isdigit() Output: true Input: "3 pieces of cake".isdigit() Output: False 12. Input: "banana".startswith("ba") Output: True Input: "banana".startswith("ra") Output: False 13. Input: "banana".endswith("na") Output: True Input: "banana".endswith("nas" Output: False

A class with many methods that can be invoked using the dot opertator Exs: 1. str.find(substr) returns the index of the first occurrence of substr in str. 2 2. str.find(substr) returns the index of the first occurrence of substr in str. 2 3. str.replace(old, new) returns a copy of str with all occurrences of old replaces with new. "choconana" 4. str.split(delimiter) returns a list of substrings from str delimited by delimiter. ["ba", "a"] 5. ["We", "are", "Penn", "State!"] 6. str.join(utterable) returns a string that is the concatenation of all the elements of iterable with str in between each element "chocolate" 7. str.join(utterable) returns a string that is the concatenation of all the elements of iterable with str in between each element "chocolate" 8. str.strip() returns a copy of str with leadings and trailing whitespace removed "spaces" 9. str.upper() returns a copy of str with each character converted to upper case "BANANA" 10. str.upper() returns True if str is all upper case 11. str.isdigit() returns True if str is all digits 12. str.startswith(substr) returns True if str starts with substr 13. str.endswith(substr) returns True if str ends with substr

What is Python?

A high-level, general-purpose, interpreted, dynamic programming language.

Compilers

A kind of program to process high-level languages into low-level languages source code ---> compiler ---> object code ---> executor ---> output

Aliases Ex: a = [56, 8, 72, 150] b = a

Occurs when 2 or more variables reference the same object a = [56, 8, 72, 150] b = [56, 8, 72, 150]

Shell Mode

One way to use Python interpreter Type Python expressions into the Python shell, and the interpreter immediately shows the result

Program Mode

One way to use Python interpreter Write an entire program by placing lines of Python instructions in a file and then use the interpreter to execute the contents of the file as a whole.

Python Language

Python is interpreted, meaning you can run programs directly after you write them It is a high-level language.

Lines and Indentations

Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced

Keywords

Reserved words and you cannot use them as identifier names like break or print

Slicing 1. Ex: [:end] gets the first characters up to but not including end Input: s = "Hello world!" s[:7] 2. Ex: [begin:end] gets the character from begin up to but not including end Input: s = "Hello world!" s[2:7] 3. Ex: [begin:] gets the characters from begin to the end of the string Input: s = "Hello world!" s[8:]

Substrings/sublists can be created with this. 1. Output: "Hello w" 2. Output: "llo w" 3. Output: "rld!"

Mutable Objects Give an example

They can be altered in place Ex: list, dict

Immutable Objects Give an example

They cannot be changed in place Ex: Int, Boolean, array

Ex: Input: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] day.remove("Wednesday") days Output: ["Monday", "Tuesday", "Thursday", "Friday"] Input: days.remove("Sunday") Ouput: error

s.remove(x) removes the first occurrence of x in s, or raises a ValueError if x is not in s


Related study sets

Series 66: Investment Vehicles (Derivatives)

View Set

BIOLOGY - UNIT 4: CELL DIVISION AND REPRODUCTION MEIOSIS

View Set

Chapter 6: Frequent Pattern Mining

View Set

Honda Civic Sedan Touring (Upgrades from EX) Trim Differences

View Set