Mapa mental
course 3 AFEC
Contenido del mapa
course 3 AFEC
sometimes you may need to store multiple variables, for example, if you need a list of names, you can have three variables, name_1, name_2, name_3, and store the names using those variables
if you have a list of items, such as strings, that you want to group together within the program, you can use a 'list', which is a Python data structure
- to create a list, you start by naming a variable and placing the equals sign = in front of it
- after the equals sign, type a square bracket, list all the items you want to group (make sure to put double speech marks around each seperate item of the group) and end the list with another square bracket
- if you want to print the entire list, just use the print function like this: print(state the variable here)
- when you print, the entire list (including the square brackets) will appear
- if you want to print the entire list, just use the print function like this: print(state the variable here)
- after the equals sign, type a square bracket, list all the items you want to group (make sure to put double speech marks around each seperate item of the group) and end the list with another square bracket
- to create a list, you start by naming a variable and placing the equals sign = in front of it
to print a specific part of a list, such as the first item, use print(name of variable[item number])
- for example, print(phrases[0])
- 0 always refers to the first item, 1 to the second, etc.
- for example, print(phrases[0])
to output a random item, use 'import.random' at the start of the programme
- then, to pick something random, type print(random.choice(name of variable here))
a while loop can be used within coding: for example, while True: and then list conditions
- if you want something to go on forever, use a while True loop, and write all the code below that you want to go on forever
creating dictionaries
- a data structure containing a series of keys
- each unique key has a data value- integer, string, etc.- called key:value pairs
- a dictionary is defined by curly brackets, a colon after the key and a comma after the pair
- similar to lists
- an example is "hello": "Hello, how are you?"
- a full example is: my_dictionary = {"hello": "Hello, how are you?", "goodbye": "Goodbye, see you soon!" (next line) print(my_dictionary["goodbye"])
- this will print the full phrase (not just goodbye)
- a full example is: my_dictionary = {"hello": "Hello, how are you?", "goodbye": "Goodbye, see you soon!" (next line) print(my_dictionary["goodbye"])
- for item in variable name: (next line) print(item), this code will print all keys
- to list all values, use code as below
- for value in variable.values(): (next line) print(value)
- to print a specific
- to list all values, use code as below
- a data structure containing a series of keys
validation is a way to check that the program is not going to produce an error.
- for example, using a dictionary to list some data (like football world cup winners), if a year is stated and there is no information for that, instead of an error coming up, you can just print 'we don't have that information'
if you want to output key and values, use the code as listed, (world_cup_winners is the variable in this case); for key in world_cup_winners: (next line) print("{} -> {}".format(key, world_cup_winners[key]))
- this will list each key and value, one key and one value per row. The key will appear on the left and there will be an arrow (->) pointing to the value, on the right
Información
- Descripción
- Mapa Mental sobre course 3 AFEC, creado por emily ! el 06/05/2020.
- Creado por
- emily ! · may 2020
- Actualizado
- 31 may 2020
- Visitas
- 0
- Valoración