Flashcards
PYTHON BASICS
Cards in this deck (23)
- 1What is a Conditional StatementA conditional statement is a statement that accommodates a condition inside itself. A conditional statement always generates a Boolean output that is either true or false.
- 2Describe the IF Statement and its function.The if statement is a conditional statement in python, that is used to determine whether a block of code will be executed or not. Meaning if the program finds the condition defined in the if statement to be true, it will go ahead and execute the code block inside the if statement.
- 3Describe the ELSE Statement and its function.The else statement works in conjuncture with the if statement to execute a code block when the defined if condition is false.
- 4Describe the ELIF Statement and its function.The elif statement is used to check for multiple conditions and execute the code block within if any of the conditions evaluate to be true.
The elif statement is similar to the else statement in the context that it is optional but unlike the else statement, there can be multiple elif statements in a code block following an if statement. - 5What order would IF ELSE ELIF be in code?if
Multiple elifs
else - if none of the above fits do this... - 6What is a function in programming and why do we use them?A function is a block of code that performs a certain task or a group of related tasks. Functions are convenient when you need to regularly accomplish the same task. After defining a function, you simply call it instead of typing or copy-pasting the same code snippets over and over again, Moreover, if you decide to change a block of code, you only need to change it where you define the function. This change will be applied anywhere the function is called.
- 7What's the syntax of defining a function in Python?We start with the def keyword to inform python that a new function is being defined. Then, we give our function a meaningful name. Next, in parentheses, we list the arguments or parameter that the function needs to perform a task. You can include as many parameters as you want. The colon symbol ":" denotes the end of the functions header and a transition to the functions body. After the indentation, we write the code block needed to perform the task at hand. When done with the code block, we leave an empty line to mark the end of the function definition.
- 8What do you need to do to use a function within another function?Define your function: def and function name first then (shortened name first short) colon:
return (first short) * 25 for instance
then leave a blank line and def new name (with second) and the shorted name from your first in parentheses colon:
return new(first short) + 50
Call first (5), new_name(8) - 9Combining conditional statements + functions.. If johnny has saved at least $100 by the end of the week his mum will give him an additional $10 if not, she wont give him anythingFirst def a function
def add_10(m):
m here is the parameter m here represents the money saved.
Now we go to our conditional statements
if m <= 100:
m = m + 10
return m
else:
return "Save more!"
Explained if m is greater than or equal to 100, let m assume the value of M plus 10. Else (if not) return save more.
We mentioned m as a parameter, then we substituted its value with a great value than m with 10. At the end, we say from now on return a value equal to the new M.
Remember the necessary indentations. - 10What are the notable built-in functions?type() max() min() sum() abs() len() int() float() round(x,y) pow(x,y)
- 11What does the function tpye()Type() obtains the type of variable you use as an argument i.e
type(10)
int - 12What does the function round(x,y) do?Round() returns the float of its argument (x), rounded to a specified number of digits (y) after the decimal point.
- 13What does the pow(x,y) function do?pow(x,y) returns x to the power of y.
- 14What is a list?Lists are used to store multiple items in a single variable. List items are ordered,changeable, and allow duplicate values. List items are indexed - starting with [0]. They are one of the four built-in data types in Python used to store collections of data, the other three are tuple,set and dictionary.
- 15Syntax of a list?List name - equals - square bracket - speech marks - string or int/float - comma - repeat - close the square bracket when finished - print (list name)
thislist = ["apple","banana","cherry"]
print(thislist) - 16How does list slicing work and what is it ?Slicing ...
- 17What is a tuple?A tuple is a collection which is ordered and unchangeable, and allows duplicate members
- 18What is a set?A set is a collection which is unordered, unchangeable although you can remove and/or add items whenever, it is also unindexed so no duplicate members.
- 19What is a dictionary?A dictionary is a collection which is ordered and changeable with no duplicate members.
- 20What does the method .append() do?append() is used to add an item to the end of a list. The method doesn't return any value.
- 21append() Syntax?directly after the name of the object, which in this case is the participants list. We'll put a dot operator, the dot operator allows you to call in or invoke a certain method. To call the method append, states its name, followed by parentheses. To insert the name dwayne into our list, we must put the string dwayne in inverted commas between the parentheses.
- 22What does the python list extend() method do?Extend() method iterates over an iterable like string, list, tuple ets and adds ech element of the iterable to the end of the list, modifying the orginal list.
- 23—
Information
- Description
- FUNCTIONS / LISTS / TUPLES / ITERATIONS / CONDITIONAL STATEMENTS
- Created by
- Valentine Attew
- Views
- 5
- Rating