Basics Flashcards
(36 cards)
Como concatenemos en Python?
con el + ej: bkf_str = ‘‘I love having “ + fav_breakfast + “ for breakfast.”
How can you add a value into a list?
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 do you add a list to another list?
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 do you count the total amount of items in a list? and how do you use the function?
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 do you do comments in Python?
with the #;
How do you tell a for loop to do something a certain number of times?
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 do you use de sorted function?
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 do you use the List Method Remove?
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 do you use the list Method count?
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 do you use the method insert?
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 do you use the method sort and what is it used for?
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 do you use the slicing method?
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 do you write an if statement in Python?
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 do you write the or operator?
Literalmente or. Ejemplo: if i == 2 or i == 4: print(i)
How does negative list indices work?
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 is the not operator?
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
How to interrupt a loop?
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.
Python Nested Loops
in Python loops can be nested inside other loops. Nested loops can be used to access items of lists which are inside other lists.
Python for loop
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.
Python while loops
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
What are the arithmetic Operation you have in Python?
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
What are the comparison operators?
< > <= >=
What is a NameError?
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.
What is a SyntaxError?
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.