fuel_efficiencies = [3.5, 5.2, 4.8, 6.0, 5.5]total_efficiency = 0for efficiency in fuel_efficiencies:total_efficiency += efficiency
Error
NASA needs to calculate the total fuel efficiency of several rockets. Each rocket has a fuel efficiency rating (miles per gallon). Your task is to calculate the total fuel efficiency by adding the ratings for each rocket in the fleet. Rearrange the code snippets below to correctly complete the task.
Python3 Documentation
Basics
Variables
We use variables to store values that can be used to control commands in our code. We can also alter these values throughout the code.
Variables have an associated 'type' based on their characteristics. A string (str) is a text element, an integer (int) is a whole number element, a float (float) is a number with decimal places, and a Boolean (bool) is an element that returns either True or False. We use the type command to determine the type of an element.
Comments
We use comments to leave notes about the code to the reader. Comments are not actually run by Python, they are just there to help us read the code.
We can make multiline comments with """ and single line comments with #.
Exceptions
Exception handling allows us to prevent our programs from crashing in the event of a fault.
Random Numbers
To be able to use the randint or choice functions, you must use import random at the beginning of your code.
Control Structures
If/If Else Statments
We can tell the computer how to make decisions using if/else statements. Make sure that all the code inside your if/else statement is indented one level!
If Statments
Use an if statement to instruct the computer to do something only when a condition is true. If the condition is false, the command indented underneath will be skipped.
If/Else Statements
Use an if/else statement to force the computer to make a decision between multiple conditions. If the first condition is false, the computer will skip to the next condition until it finds one that is true. If no conditions are true, the commands inside the else block will be performed.
Operators
We use mathematical, comparison, and logical operators in our codes to compare and alter values and make decisions.
Mathematical Operators
Use mathematical operators to alter values.
Comparison Operators
Use comparison operators to compare elements in order to make decisions in your code. Comparison operators return booleans (True/False).
Logical Operators
Use logical operators to check multiple conditions at once or one condition out of multiple.
Loops
Loops help us repeat commands which makes our code much shorter. Make sure everything inside the loop is indented one level!
For Loops
Use for loops when you want to repeat something a fixed number of times.
While Loops
Use while loops when you want to repeat something an unknown number of times or until a condition becomes false. If there is no point where the condition becomes false, you will create an infinite loop which should always be avoided!
Break and Continue
We can use the break command to end our code. The continue command will leave the control structure at that point and move to the commands found afterward.
Functions
Writing a function is like teaching the computer a new word.
Naming Functions: You can name your functions whatever you want, but you can't have spaces in the function name. Instead of spaces, use underscores ( _ ) like_this_for_example
Make sure that all the code inside your function is indented one level!
Defining a Function
We define a function to teach the computer the instructions for a new word. We need to use the term def to tell the computer we're creating a function.
Returning Values in Functions
We can use the command return to have a function give a value back to the code that called it. Without the return command, we could not use any altered values that were determined by the function.
Calling a Function
We call a function to tell the computer to actually carry out the new command.
Using Parameters in Functions
We can use parameters to alter certain commands in our function. We have to include arguments for the parameters in our function call.
Data Structures
Strings
Strings are pieces of text. We can gain much information about strings and alter them in many ways using various methods.
Indexing a String
We use indexing to find or take certain portions of a string. Index values always start at 0 for the first character and increase by 1 as we move to the right. From the end of the string, the final value also has an index of -1 with the values decreasing by 1 as we move to the left.
String Methods
There are many methods that can be used to alter strings.
Tuples
Tuples are immutable sequences of items.
Creating a Tuple
We create a tuple by listing items inside parentheses. We can include elements of any type.
Altering a Tuple
Due to the immutable nature of tuples, we cannot alter individual elements in the tuple but can perform various other tasks with them.
Lists
Lists are mutable sequences of items.
Creating a List
We create a list by listing items inside square brackets. We can include elements of any type.
Altering a List
Due to the mutable nature of lists, we can alter individual elements in the list.
List Methods
There are many methods that can be used to alter lists.
2D Lists
2D Lists allow us to create lists of lists.
Dictionaries
Dictionaries have a collections of key-value pairs.
Classes
Classes hold multiple functions.
Sets
A set contains an unordered collection of unique and immutable objects.
Input/Output (I/O)
Printing
We can print elements to the screen by using the print command. If we want to print text, we need to surround the text with quotation marks " ". Unlike previous versions of Python, Python3 requires parentheses for printing.
String Formatting
User Input
We can use input from the user to control our code.
File Input
Use the following when importing files.