
























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
A wide range of python programming concepts, including variable scope, function definitions, exception handling, and the use of the python interpreter. It provides insights into the strengths and weaknesses of the python language, as well as the benefits of using tools like jupyter notebook and anaconda. The document also delves into common syntax errors, exception types, and the role of logging in python development. Overall, this resource offers a comprehensive understanding of python's core features, best practices, and practical applications, making it a valuable reference for both beginner and experienced python programmers.
Typology: Exams
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Why are functions helpful? - correct answer ✔✔Functions facilitate code reusability, readability, and maintenance What is "scope" in terms of programming language python? - correct answer ✔✔Scope is the term used to describe the points at which in code variables and functions are defined. Why is there an error when trying to run this code? a=int(input()) b=int(input()) if c<a+b: print("Less than a +b:) a = b= c= print (a,b,c) - correct answer ✔✔The variable c is not in scope or defined in line 3. Which of the following lines of code defines a function named "my_func" that has three parameters (a, b, and c) where b defaults to 0 and c defaults to "Yes"? a. my_func(a,b,c): b. def my_func(a, b(0), c("yes"): c. def my_func(a, b=0, c="Yes"): d. def my_func(a, b, c): - correct answer ✔✔c
Which of the following lines of code will not call a function named "my_func" that has three parameters (a, b, and c) where b defaults to 0 and c defaults to "Yes"? a.) my_func( ) b.) my_func(1, c="No") c.) my_func(1) d.) my_func(1, 2) - correct answer ✔✔a Which of the following will import a specific function "add_numbers" from a library "adding_library"? - correct answer ✔✔from adding_library import add_numbers import <file name without the .py extension> (How to import the full module/libray) from <file name without the .py extension> import < function name > There is a function, "my_function". Within that function, a variable called "my_var" is created. In which namespace does my_var exist? - correct answer ✔✔local Python's int( ) function will take an argument and convert it to an integer. In which namespace does int( ) exist? - correct answer ✔✔built-in Which best describes the difference between a comment and a docstring in Python? - correct answer ✔✔Comments do not have a functional purpose but docstrings tie into the Python documentation help library. Which of the following is not a Python library used frequently for data science applications? a.) NumPy b.)SciPy
(Python code is compiled into an executable, which is helpful for web development.) In which specializations is Python a primary language of choice for professionals and researchers? - correct answer ✔✔data science/artificial intelligence The primary function of the python interpreter is to: - correct answer ✔✔read code, execute it, and return the output One of the benefits of Jupyter Notebook is that it... - correct answer ✔✔allows developers to combine code, Markup, and raw text in one document JupyterLab uses IPython as... - correct answer ✔✔the backend interpreter for python code How do you represent a hexadecimal number in python? - correct answer ✔✔0x What is the result of this equation 80%25? - correct answer ✔✔ 5 If I have a string "my_string" with the value "Python is so powerful!" how do I substring to get just "is so"? - correct answer ✔✔my_string[7:12] remember, the string starts with 0, and the number 12, means that it will stop at 11 I want to open and write to a file called "thatfile.txt". If the file does not exist, I want to create it but if it does exist, I want to write at the end of the file, preserving what's already in the file. How do I do that? - correct answer ✔✔open("thatfile.txt", "a") What is the value of the variable c after the following statement? a=b=c=d=5 - correct answer ✔✔ 5 What is the output of the following code if the user enters the number 0? my_num = int(input())
if my_num < 0: print(1) elif my_num > 0: print(2) else: print(3) - correct answer ✔✔ 3 Why will the following code not run? my_num = 0 int(input()) if my_num < 0 print(1) elif my_num > 0 print(2) else print(3) - correct answer ✔✔The colons are missing at the end of the else, elif, and if lines. Which of the following will generate a range of numbers from 11 to 99? - correct answer ✔✔range[11,100] What does this Python expression evaluate to: 8==8 - correct answer ✔✔True I have a variable called my_var that contains the value 10. What does the statement my_var +=5 do? - correct answer ✔✔Adds 5 to the current value of my_var, so my_var will contain the value 15
-easy to prototype -readable -interpreted language (codes are processed at runtime without need of comping) -programs can run interactively -cross platform compatible -great for large data analysis -can be used for web development What are the weaknesses of python programming language? - correct answer ✔✔-slow compared to other programming languages such as Java and C++ -lacks support for mobile app development -no security/warranty -uses large amount of memory -lacks support for relational databases What are the benefits of Jupyter Lab? - correct answer ✔✔-There is more you can do in Jupyter lab compared to just using the python interpreter -uses the web browser instead of python terminal prompt environment -combines rich formatted text (markdown), inline code, math formulas, plots/graphs all in single document Which of the following is not a reason why Python is such a popular software development language? a) Python is relatively easy to read. b) Python code is compiled into an executable, which is helpful for web development. c) Python is very concise, meaning you can do more with less code. d) Python has a large community of active developers building libraries and tools for Python development. - correct answer ✔✔b
What is the primary function of the Python interpreter? - correct answer ✔✔A python interpreter is a computer program that converts each high-level program statement into machine code. An interpreter translates the command that you write out into code that the computer can understand List three reasons why Python is a popular language among educators and learners. - correct answer ✔✔-free with a variety of supporting tools -syntax is concise compared to other languages, meaning you can do more with less -easier to read -code resembles everyday english -active developer community that creates resources -interpreted language, can get immediate feedback without having to wait for an application to compile List three reasons why Python is a popular language among professionals. - correct answer ✔✔-concise syntax ( do more with less) -extremely robust and powerful -popular choice for quickly building working prototypes -language of choice for data science and AI -large amounts of libraries available for Python development in math, statistics, machine learning and deep learning -web development What is Anaconda? - correct answer ✔✔Anaconda is a distribution that includes Python and various other tools including SciPy, NumPy, and pandas. What is NumPy? - correct answer ✔✔An extremely efficient library used for computation on large data sets and multi-dimensional arrays. What is SciPy? - correct answer ✔✔a library used for scientific computing including linear algebra, interpolation, and signal and image processing
operations: .get("Hair") List: [ ] elements are ordered and changeable (mutable). Duplicates are allowed. How do you write to a file? - correct answer ✔✔my_file= open('my_file.txt', 'w') How do you create a file? - correct answer ✔✔my_file= open('my_file.txt', 'x') How do we open a file, write to it (at the end, not overwrite), and then close the file? - correct answer ✔✔my_file=open('my_file', 'a') <--- this file must already exist. my_file.write('I would love for this exam to be over') (<-- will not appear in the file until the file is closed. ) my_file.close() Why do we se variables in python? - correct answer ✔✔A variable in Python is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing. Declaring and re-declaring a variable in Python is as easy as writing names and assigning values to it. What is the result of 80%25? - correct answer ✔✔ 5 How would you put quotes into a string in Python? - correct answer ✔✔my_string = "this has "quotes"." Or you can use ' ' to open and use " " to add the quotation
If I h ave a string "my_string" with the value "Python is so powerful!" how do I substring to get just "is so"? - correct answer ✔✔my_string[7:12] I want to open and write to a file called "thatfile.txt". If the file does not exist, I want to create it. If it does exist, I want to write at the end of the file, preserving what's already in the file. How do I do that? - correct answer ✔✔that_file=open("that_file.txt", "x") that_file=open("that_file.txt", "a") that_file.write('This is a great day!') that_file.close() What are the syntax rules for creating a variable? - correct answer ✔✔Must start with either a letter or an _ May only include letters, numbers, and _ Should be all lowercase. (Only constants have all UPPERCASE, i.e. gravity.) -words should be separated by an underscore, however if they are not, Python will not mark it as an error. example of incorrect variable: &player_age (starts with something other than a lowercase letter or _) What type of value is 0o42? - correct answer ✔✔octal What type of value is 42? - correct answer ✔✔int What type of value is 4 + 2J? - correct answer ✔✔complex What type of value is 4.2? - correct answer ✔✔float What is the result of this equation: int(5.8 * 2)? - correct answer ✔✔ 11
What is an expression? - correct answer ✔✔An expression is a combination of literals, operators, variables, and parentheses used to calculate a value. What is a statement? - correct answer ✔✔An expression sits on the right-hand side of the = sign and is evaluated first, and then the result is assigned to a variable. statement--> (variable name = expression) What is a conditional statement? - correct answer ✔✔A conditional statement tells a program what to do if a certain condition is met (i.e., evaluates to True). Example: if a<=b: If a is less than or equal to be (condition, True or false) a*=2. (What to do if True) What numbers are displayed with range(3,9,2)? - correct answer ✔✔range(3,9,2) start at position 3, ending at the number preceding 9, and in steps of 2 3,5, What is range(7) equivalent to? - correct answer ✔✔range(0,7,1) 0,1,2,3,4,5, What is range(2,7) equivalent to? - correct answer ✔✔range(2,7,1) What is a loop statement for? - correct answer ✔✔iterates a code block until the end of a range syntax: for
for x in range (7): print(x) 0,1,2,3,4,5, What is a while loop? - correct answer ✔✔`the while loop iterates a code block until an expression is evaluated to False. example: while
my_new_tuple= tuple(n*1 for n in my_tuple) What is the code that will generate a range of numbers from 11 to 99 with increment of 2? - correct answer ✔✔range(11, 100, 2) Why will the following code not run? my_num = 0 int(input()) if my_num < 0 print('negative') elif my_num > 0 print('positive') else print('zero') - correct answer ✔✔no : after each if, elif, and else Which of the following lines of code will print the output of all four variables (a, b, c, and d)? - correct answer ✔✔print(a, b, c, d) I have a variable called my_int that holds the integer value 42. I want to convert that value to a string data type and assign that value to a variable called my_str. Which of the following will do that? - correct answer ✔✔my_str = str(my_int) What is the output of the following code when the user inputs "30" at the prompt? my_int = int(input()) if my_int * 2 < 10:
print("Less than 10!") elif my_int * 2 < 20: print("Between 10 and 19!") elif my_int * 2 < 50: print("Between 20 and 49!") else: print("Bigger than 50!") - correct answer ✔✔Bigger than 50! What is the difference between an expression and a statement in Python? - correct answer ✔✔A statement is a line of code the interpreter can execute, an expression is a section of code that the interpreter evaluates to a value. What is the output of the following code? my_str = "I love Python! It is so much fun!" if len(my_str) < 20: print("Python is awesome!") else: print("Python is amazing!") - correct answer ✔✔Python is amazing! my_variable = input() if int(my_variable) < 50 print("Less than 50!") else: print("50 or more!") - correct answer ✔✔There is a missing colon (:) after the if statement on line 2. What is the difference between break and continue? - correct answer ✔✔The break statement will move the execution outside and just after a loop. (Will discontinue the loop, stop the loop.) The continue statement will move the execution to the start of the loop. (skipping one, it will not continue further through the execution.)
print(1) elif my_num > 0 print(2) else print(3) - correct answer ✔✔The colons are missing at the end of the else, elif, and if lines. I have a list of values called "my_list". How do I loop through all of the values in "my_list"? - correct answer ✔✔for element in my_list:
What does global scope mean? - correct answer ✔✔global keyword inside a function will make a variable accessible outside a function when declaring the variable. (Makes the variable in scope inside the function and outside.) What is the difference between parameter and argument? - correct answer ✔✔A parameter is the variable within a function when it is being defined. An argument is what is passed through the function in place of the parameter. Example: def greetings (first_name): print('Hello, ', first_name,'!') greetings('Jane') first_name= parameter 'Jane' is the argument What is the difference between positional arguments and named arguments? - correct answer ✔✔Once a function is defined with parameters, you can use either positional arguments or named arguments to pass through the function. Positional arguments correspond with the order position of the defined parameters within the function. Named arguments do not necessarily have to correspond with the position of the parameters within the argument. Named arguments use an = (assignment operator) to associate the argument with the correct parameter. Example: personal_info(age=25, position='Senior manager', name= 'Jane') As the arguments are already defined, they do not need to be within " "