Introduction to Computers and Python Flashcards

(16 cards)

1
Q

What is an algorithm?

A

An algorithm is a sequence of instructions to solve a problem.

Example:

```python
def add(a, b):
return a + b
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a class in Python?

A

A class is a blueprint for creating objects with shared attributes and methods.

Example:

```python
class Dog:
def bark(self):
print(“Woof!”)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an object in Python?

A

An object is an instance of a class.

Example:

```python
my_dog = Dog()
my_dog.bark() # “Woof!”
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an instance?

A

An instance is a specific object created from a class.

Example:

```python
dog1 = Dog()
dog2 = Dog()
# dog1 and dog2 are different instances
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an attribute in Python?

A

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
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a method in Python?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create an object in Python?

A

By calling the class name with parentheses.

Example:

```python
dog = Dog() # creates an object of class Dog
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does __init__ do in a Python class?

A

__init__ is the constructor that initializes the object’s attributes.

Example:

```python
class Dog:
def __init__(self, name):
self.name = name
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between a method and a function?

A

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”)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can objects have different values for the same attribute?

A

Yes, each instance can store its own data.

Example:

```python
dog1 = Dog(“Fido”)
dog2 = Dog(“Rex”)
print(dog1.name) # Fido
print(dog2.name) # Rex
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What characterizes Python as a programming language?

A

Python is an interpreted, high-level, and easy-to-learn language widely used in data science, web development, and AI.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Jupyter notebook?

A

It is a web-based tool that allows mixing code, text, and visualizations for interactive Python programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is IPython?

A

IPython is an enhanced interactive Python shell with features like auto-completion, syntax highlighting, and magic commands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why are Python libraries important?

A

Python libraries are reusable code collections that extend the language’s capabilities, such as NumPy, pandas, and matplotlib.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between a class attribute and an instance attribute in Python? Provide a code example.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. 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).

A

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))