Introduction to Computers and Python Flashcards
(16 cards)
What is an algorithm?
An algorithm is a sequence of instructions to solve a problem.
Example:
```python
def add(a, b):
return a + b
~~~
What is a class in Python?
A class is a blueprint for creating objects with shared attributes and methods.
Example:
```python
class Dog:
def bark(self):
print(“Woof!”)
~~~
What is an object in Python?
An object is an instance of a class.
Example:
```python
my_dog = Dog()
my_dog.bark() # “Woof!”
~~~
What is an instance?
An instance is a specific object created from a class.
Example:
```python
dog1 = Dog()
dog2 = Dog()
# dog1 and dog2 are different instances
~~~
What is an attribute in Python?
An attribute is a variable that stores data inside an object.
Example:
```python
class Dog:
def __init__(self, name):
self.name = name # ‘name’ is an attribute
dog = Dog(“Fido”)
print(dog.name) # Fido
~~~
What is a method in Python?
A method is a function defined inside a class that operates on the object’s data.
Example:
```python
class Dog:
def bark(self): # ‘bark’ is a method
print(“Woof!”)
dog = Dog()
dog.bark()
~~~
How do you create an object in Python?
By calling the class name with parentheses.
Example:
```python
dog = Dog() # creates an object of class Dog
~~~
What does __init__ do in a Python class?
__init__ is the constructor that initializes the object’s attributes.
Example:
```python
class Dog:
def __init__(self, name):
self.name = name
~~~
What is the difference between a method and a function?
Function
A method is defined in a class and operates on an instance; a function is not.
Example:
```python
# Method
class Dog:
def bark(self):
print(“Method”)
def greet():
print(“Function”)
~~~
Can objects have different values for the same attribute?
Yes, each instance can store its own data.
Example:
```python
dog1 = Dog(“Fido”)
dog2 = Dog(“Rex”)
print(dog1.name) # Fido
print(dog2.name) # Rex
~~~
What characterizes Python as a programming language?
Python is an interpreted, high-level, and easy-to-learn language widely used in data science, web development, and AI.
What is a Jupyter notebook?
It is a web-based tool that allows mixing code, text, and visualizations for interactive Python programming.
What is IPython?
IPython is an enhanced interactive Python shell with features like auto-completion, syntax highlighting, and magic commands.
Why are Python libraries important?
Python libraries are reusable code collections that extend the language’s capabilities, such as NumPy, pandas, and matplotlib.
What is the difference between a class attribute and an instance attribute in Python? Provide a code example.
Class attribute: Defined directly inside the class body; there is one shared copy that the class and all its instances reference.
Instance attribute: Created with self.attribute (usually in __init__); each object has its own independent copy.
class Dog:
species = “Canis lupus” # class attribute
def \_\_init\_\_(self, name): self.name = name # instance attribute
d1 = Dog(“Fido”)
d2 = Dog(“Rex”)
print(d1.species, d2.species) # both see the same class attribute
d1.species = “Canis catus” # shadows only for d1
print(d1.species) # Canis catus (instance attribute now)
print(d2.species) # Canis lupus (still class attribute)
print(Dog.species) # Canis lupus (class unchanged)
- Assignment
Create a Car class that models a car in a dealership system.
The constructor (__init__) must receive three parameters besides self
brand (e.g., “Toyota”)
model (e.g., “Corolla”)
year (e.g., 2022)
Store each parameter in an instance attribute.
Add a method full_name() that returns a string like “2022 Toyota Corolla”.
Add a method age(current_year) that returns the car’s age.
In the main program:
Create two different Car objects.
Print each car’s full name and age (assume current_year = 2025).
Instanciar la clase Car, crear objeto c1, le pasamos los atributos
class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
def full_name(self):
return f”{self.brand} {self.model} ({self.year})”
def age(self,current_year):
return current_year-self.year
dealership_year = 2025
# brand, model y year que el constructor (__init__) recibirá como parámetros.
c1= Car(“Toyota”, “Corolla”, 2022)
#creamos otra clase c2, con atributos independientes de c1
c2= Car(“Honda”, “Civic”, 2025)
print(c1.full_name(), “– age:”, c1.age(dealership_year))
print(c2.full_name(), “– age:”, c2.age(dealership_year))