EXAM - Python Programming Flashcards
(58 cards)
Which of the following is the correct way to define a variable in Python?
a) int x = 5
b) x := 5
c) x = 5
d) let x = 5
- Answer: c)
x = 5
Explain the difference between a list and a tuple in Python. Provide an example of each.
A list is mutable, meaning it can be modified after its creation, while a tuple is immutable, meaning it cannot be changed once created.
Example of a list: my_list = [1, 2, 3]
; Example of a tuple: my_tuple = (1, 2, 3)
Write a Python program that prints all even numbers from 1 to 20 using a for
loop.
for i in range(1, 21):
if i % 2 == 0:
print(i)
Complete the following function to calculate the factorial of a given number n
:
python
def factorial(n):
if n == 0:
return 1
else:
__________your solution__________
python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Design a Python class named Car
that has attributes for make
, model
, and year
. Include a method to display the car’s information.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def display_info(self): print(f"{self.year} {self.make} {self.model}")
Example usage
my_car = Car(“Toyota”, “Corolla”, 2020)
my_car.display_info()
When asking ChatGPT for help with a coding problem, what are two key pieces of information you should always include in your question?
The specific error message (if any) and a clear description of what you are trying to achieve.
Explain why it is important to avoid plagiarism in programming assignments and list two strategies to ensure your work is original.
Avoiding plagiarism is important because it ensures academic integrity and demonstrates your own understanding of the material. Two strategies to ensure originality are: 1) Writing code from scratch and 2) Properly citing any code snippets or libraries used from external sources.
Analyse how participating in online coding communities can enhance your coding skills. Provide two specific examples.
Participating in online coding communities can enhance coding skills by providing exposure to different coding styles and solutions to common problems. For example, contributing to open-source projects can improve collaborative skills, and engaging in coding challenges can help identify and fill knowledge gaps.
Which of the following is a common coding convention in Python?
a) Using camelCase for variable names
b) Using tabs for indentation
c) Using snake_case for variable names
d) Using all uppercase letters for function names
c) Using snake_case for variable names
Which of the following is NOT a reserved word in Python?
a) if
b) while
c) true
d) class
c) true
Write a Python program that asks the user for a number and prints “Positive” if the number is greater than zero, “Negative” if it is less than zero, and “Zero” if it is exactly zero.
number = int(input(“Enter a number: “))
if number > 0:
print(“Positive”)
elif number < 0:
print(“Negative”)
else:
print(“Zero”)
Explain how flowcharts can help in solving programming problems and provide an example scenario where a flowchart would be useful.
Flowcharts help by providing a visual representation of the sequence of steps in an algorithm, making it easier to understand and debug the logic. For example, a flowchart would be useful in planning the logic for a complex conditional statement in a program.
What is Google Colab and how can it be used for coding in Python?
Google Colab is a cloud-based coding environment that allows users to write and execute Python code in a web browser. It is particularly useful for data analysis, machine learning, and collaborating on coding projects.
Write a Python function that takes a number as input and returns a list of all its multiples up to 100.
def find_multiples(n):
multiples = []
for i in range(1, 101):
if i % n == 0:
multiples.append(i)
return multiples
Example usage
print(find_multiples(5))
Understand the difference between .ipynb and .py files
A .ipynb
file is a Jupyter Notebook file that can contain both code and rich text elements (paragraphs, equations, figures, links, etc.), while a .py
file is a plain Python script that contains only Python code.
Explain how you would convert a Jupyter Notebook (.ipynb
file) to a Python script (.py
file).
You can convert a Jupyter Notebook to a Python script by using the “Download as” option in the “File” menu of Jupyter Notebook and selecting “Python (.py)”. Alternatively, you can use the command jupyter nbconvert --to script notebook.ipynb
in the terminal.
Given a Jupyter Notebook file named analysis.ipynb
, write the command you would use in the terminal to convert it to a Python script.
sh
jupyter nbconvert –to script analysis.ipynb
Compare and contrast the use cases for .ipynb
and .py
files. When would you choose one format over the other?
.ipynb
files are ideal for data analysis, visualization, and educational purposes because they allow you to combine code with rich text and visualizations. They are also useful for interactive development and sharing results with others. .py
files are more suited for production code, automation scripts, and situations where only the code is needed. They are easier to manage with version control systems and are typically used for larger projects and deployment.
Evaluate the following statement: “Jupyter Notebooks are superior to Python scripts for all types of programming tasks.” Do you agree or disagree? Provide reasons for your answer.
Disagree. Jupyter Notebooks are superior for tasks that involve data analysis, visualization, and interactive development because they allow for combining code, text, and visualizations in one document. However, Python scripts are more suitable for production code, automation, and larger projects because they are easier to manage with version control systems, are more modular, and can be executed directly by Python interpreters without the need for a Jupyter environment.
How do you upload a .ipynb
file to Google Colab?
In Google Colab, you can upload a .ipynb
file by clicking on the “File” menu, selecting “Upload notebook”, and then choosing the file from your local machine.
Describe the steps to run a .py
file in Google Colab.
To run a .py
file in Google Colab, follow these steps:
1. Upload the .py
file to your Colab environment by clicking on the “Files” tab and then the “Upload” button.
2. Use the %run
magic command followed by the file name to execute the script. For example, %run script.py
.
Explain how you can save your work in Google Colab to your local machine.
You can save your work in Google Colab to your local machine by using the “File” menu and selecting “Download .ipynb” to save the notebook as a .ipynb
file or “Download .py” to save it as a Python script. You can also use the files.download
function from the google.colab
module to download specific files.
Write the code to upload a file named data.csv
from your local machine to Google Colab.
from google.colab import files
uploaded = files.upload()
Answer: After running this code, you will be prompted to select the data.csv
file from your local machine to upload it to the Colab environment.
You have a Jupyter Notebook (.ipynb
) and a Python script (.py
) that perform similar data analysis tasks. Discuss the pros and cons of running each in Google Colab.
- Pros of running
.ipynb
in Google Colab:- Interactive environment with rich text, code, and visualizations.
- Easy to share and collaborate with others.
- Built-in support for running cells individually.
- Cons of running
.ipynb
in Google Colab:- Can be slower to load and execute compared to plain scripts.
- Requires more resources for rendering the notebook interface.
- Pros of running
.py
in Google Colab:- Faster execution since it’s a plain script.
- Easier to manage and version control.
- Suitable for automation and batch processing.
- Cons of running
.py
in Google Colab:- Lacks the interactive features and rich text capabilities of notebooks.
- More challenging to visualize and interpret intermediate results.