Basics Flashcards

(36 cards)

1
Q

Como concatenemos en Python?

A

con el + ej: bkf_str = ‘‘I love having “ + fav_breakfast + “ for breakfast.”

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

How can you add a value into a list?

A

You can add values to the end of a list using .append() orders=[‘daisies’,’periwinkle’] orders.append(‘tulips’) ===> orders es igual a [‘daisies’,’periwinkle’, ‘tulips’]

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

How do you add a list to another list?

A

Fot this you can use the + operator.
Ej.:

list1=[1, 2, 3]

list2=[4, 5, 6]

list3=list1 + list2 ===> list3 va a ser igual a [1, 2, 3, 4, 5, 6]

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

How do you count the total amount of items in a list? and how do you use the function?

A

With len: my_list=[manzana,’banana’, ‘pera’] len(my_list) es igual a 3 porque la lista tiene 3 items la lista se pone entre los parentesis de la funcion len !

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

How do you do comments in Python?

A

with the #;

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

How do you tell a for loop to do something a certain number of times?

A

The range function can be used to create a list that can be used to specify the number of iterations in a for loop. Ejemplo: for item in range(3): print(‘warning!’) entonces imprime ‘warning!’ 3 veces. Ejemplo 2: for i in range(3): print(i) so it print the numbers 0, 1, 2:

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

How do you use de sorted function?

A

The sorted funcition accepts a list as an argument and will return a new sorted list containing the same elements as the original. Ejemplo: unsorted_list=[4,2,1,3] sorted_list=sorted(unsorted_list) ==>; sorted_list va a ser igual a [1,2,3,4]

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

How do you use the List Method Remove?

A

It is used to remove an element from a list by passing in the value of the element to be removed as an argument. shopping_line=[ ‘Cole’, ‘Kip’, ‘Chris’, ‘Syl’, ‘Chris’] shopping_line.remove(‘Chris’) ==>; shopping_line va a ser igual a [ ‘Cole’, ‘Kip’, ‘Syl’, ‘Chris’] porque el remove quita el primer ‘Chris’ que encuentra.

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

How do you use the list Method count?

A

The .count() method searches a list for whatever search term it receives as an argument, then returns the number of matching entries found. Osea, cuenta la cantidad de elementos que hay iguales al argumento que se le pasa. Ej: backpack=[‘pencil’, ‘pen’, ‘notebook’, ‘textbook’, ‘pen’, ‘highlighter’, ‘pen’] numPen=backpack.count(‘pen’) ===>; numPen es igual a 3

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

How do you use the method insert?

A

The method insert allows us to add an element to a specific index in a list. It takes two inputs: the index where you want to insert the element you want to insert store_line = [“Karla”, “Maxium”, “Martim”, “Isabella”] store_line.insert(2, “Vikor”) ahora store_line va a ser igual a [‘Karla’, ‘Maxium’, ‘Vikor’, ‘Martim’, ‘Isabella’]

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

How do you use the method sort and what is it used for?

A

The sort Python method will sort the content of whatever list is called on. Numerical list will be sorted in ascending order, and list of strings will be sorted into alphabetical order. Atención: It modifies the original list and has no return value!! my_list=[4,2,5,3,1] my_list.sort() ==>; my_list va a ser igual a [1,2,3,4,5]

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

How do you use the slicing method?

A

The syntax pattern is: my_list[start_number:end_number] Ejemplo: my_list=[‘tomate’,’zanahoria’,’papa’,’batata’,’cebolla’] inner_list=my_list[1:4] ===>; inner_list es [‘zanahoria’,’papa’,’batata’] El start_number es el index desde donde empieza, y el end_number es donde termina SIN incluir ese index en la lista, por eso, ‘cebolla’ esta en el index 4 pero no esta incluido en inner_list Si hago my_list[:3] va a ser desde el comienzo hasta el index 3 SIN incluir ese elemento en la nueva lista. Si hago my_list[2:] va a ser desde el item con indice 2 hasta el final.

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

How do you write an if statement in Python?

A

Ejemplo: if i <10: print(i) no se usan paréntesis para la condición, se pone : para indicar que termina la condición y una linea abajo con tabulado se pone la sentencia a realizar de ser la condición verdadera.

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

How do you write the or operator?

A

Literalmente or. Ejemplo: if i == 2 or i == 4: print(i)

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

How does negative list indices work?

A

Negative indices can be used to reference elements in relation to the end of a list. Ej: my_list=[‘rosa’,’gardenia’,’narciso’] my_list[-1] va a ser igual a ‘narciso’ my_list[-2] va a ser igual a ‘gardenia’

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

How is the not operator?

A

not operator is used in a Boolean expression in order to evaluate the expression to its invers value. not True ====> resulta False not False ====> resulta True not 1 == 1 =====> resulta False, porque 1 ==1 resulta True

17
Q

How to interrupt a loop?

A

With the keyword break. Ejemplo: nums=[1,2,3,200,23,102] for num in nums: if num < 100: break; entonces el loop, va a ver el 1, el 2 y el 3, y cuando llegue a 200 va a interrumpir el loop por lo que no va a ver el 23 y el 102.

18
Q

Python Nested Loops

A

in Python loops can be nested inside other loops. Nested loops can be used to access items of lists which are inside other lists.

19
Q

Python for loop

A

Se usa para iterar sobre una lista. Su estructura es: for temporary_variable in list_name: action_statement Es importante indentar al comiendo de la accion que queremos que se realice.

20
Q

Python while loops

A

A while loop will repeatedly execute a code block as long as the condition evaluates to True. The condition of the while loop is always checked first before the code block runs. If the condition is not met initially, then the code block will never run. i=1 while i < 6: print(i) i+=1

21
Q

What are the arithmetic Operation you have in Python?

A

You have:
módulo, el resto de la división —> a % b
ej: 7 % 2 =1
división entera, sin decimales —> a // b
ej:7 // 2 = 3
división —> a / b multiplicación —> a * b exponentiation —> a ** b
suma —> a + b
resta —> a - b

22
Q

What are the comparison operators?

23
Q

What is a NameError?

A

A NameError is reported by the Python interpreter when it detects a variable that is unknown. This can occur when a variable is used before it has been assigned a value, if a variable name is spelled differently than the point at which it was defined.

24
Q

What is a SyntaxError?

A

A syntaxError is reported by the Python interpreter when some portion of the code is incorrect. This can include: misspelled keywords, missing or too many brackets or parenthesis, incorrect operator, missing or too many quotations marks, or other conditions.

25
What is the keyword to tell a loop to skip the remaining code?
With the keyword continue.
26
What is the not equal operator?
!= Ejemplo: if j != 2: print(''we need 2!")
27
What is the pop method?
The .pop() method allows us to remove the last element from a list while also returning it. ===> con pop puedo sacar el último elemento de una lista (modifica la lista original) y devuelve el elemento sacado de la lista. Ejemplo: cs_topics = ["Python", "Data Structures", "Balloon Making", "Algorithms", "Clowns 101"] removed_element = cs_topics.pop() ====> cs_topics ahora es igual a ['Python', 'Data Structures', 'Balloon Making', 'Algorithms'] y removed_element es igual a'Clowns 101'
28
What the equivalent to console.log() in Python?
print()
29
When do you get an infinite loop?
An infinite loop results when the condition of the loop prevent it from terminating. To interrupt a Python program that is running forever, press the Ctrl and C keys together on your keyboard.
30
como se abrevia i = i + 1 en Python?
En Python no existe i++ en cambio se usa i+=1 El += se puede usar también para concatenar
31
como separar un str en diferentes lineas?
Por medio del \ . Ejemplo: str= "This string is broken up \ over multiple lines".
32
else operator
else: sentencia
33
how do you write an else if in Python?
elif condicion: sentencia
34
how do you write the and operator?
Literalmente and. Ejemplo: if var1== True and var2==True: print("las dos variables son verdaderas")
35
how do you write the boolean values?
With the first letter is capital letter! Ej: True False
36
what is Python List Comprehension? /
Python list comprehension provides a concise way for creating a list. It consist of brackets containing an expression followed by a for clause, then zero or more for or if clauses. Something like: [EXPRESSION for ITEM in LIST < if CONDITIONAL>] Ejemplo: #List comprehension for the squares of all even numbers between 0 and 9: result= [ x**2 for x in range(10) if x % 2 == 0 ] entonces result va a ser igual al [ 0, 4, 16, 36, 64]