AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
While python not equal11/21/2023 # can use the items to assign arguments using an asterisk # If we have a list with the same number of parameters, we # We call the function with values inside the parentheses # In this program, parameters are used to give two numbers We have to include arguments for the parameters in our function call. Using Parameters in Functions We can use parameters to alter certain commands in our function. # The output will be the number 3 printed on 3 separate lines # Call the add_numbers() function 3 times and print the output # We add a return statement in order to use the value of the sum variableĬalling a Function We call a function to tell the computer to actually carry out the new command. Without the return command, we could not use any altered values that were determined by the 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. # Code that will run when you make a call to We need to use the term def to tell the computer we’re creating a function. 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. Naming Functions: You can name your functions whatever you want, but you can't have spaces in the function name. # This code will continue running while the user answers ‘Yes’įunctions Writing a function is like teaching the computer a new word. # You can also use user input to control a while loop # This program will run as long as the variable 'number' is greater than 0 If there is no point where the condition becomes false, you will create an infinite loop which should always be avoided! While Loops Use while loops when you want to repeat something an unknown number of times or until a condition becomes false. # This loop will print 1, then 5, then 10, then 15 # This code executes on each item in my_list # This for loop will print out even numbers 1 through 10 # This for loop will print "hello" 5 times 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. Loops Loops help us repeat commands which makes our code much shorter. Logical Operators Use logical operators to check multiple conditions at once or one condition out of multiple. # This code will end when the use enters a negative The continue command will end the current iteration in a loop and continue to the next iteration. # Use single line comments to clarify parts of code.īreak and Continue We can use the break command to end our code. Print("Sum: " + str(number_one + number_two)) Number_two = int(input("Enter a second number: ")) Number_one = int(input("Enter a number: ")) Then it will add the numbers and print the final value. This program will ask the user for two numbers. We can make multiline comments with """ and single line comments with #. Comments are not actually run by Python, they are just there to help us read the code. # This input can then be used to control different parts of the codeĬomments We use comments to leave notes about the code to the reader. Num_two = int(input("Enter a second number: ")) # If input needs to be used as a number, include the term 'int' # This will only work if the input is being used as a string # Ask the user for input and save it to a variable to be used in code User Input We can use input from the user to control our code. # We do this when concatenating strings, such as: # We can change the type of an element by using the shortened name of the type We use the type command to determine the type of an element. 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. Variables have an associated ‘type’ based on their characteristics. # The value of a variable can be changed after it has been created # We can also assign multiple variables at once We can also alter these values throughout the code. Variables We use variables to store values that can be used to control commands in our code. Print("My name is %s and I am %d years old!" % ('Zara', 21)) If we want to print text, we need to surround the text with quotation marks " ". Python Documentation Printing We can print elements to the screen by using the print command.
0 Comments
Read More
Leave a Reply. |