Python Intro Course

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Python can also perform exponentiation. In written math, you might see an exponent as a superscript number, but typing superscript numbers isn't always easy on modern keyboards. Since this operation is so related to multiplication, we use the notation______.

** # 2 to the 10th power, or 1024 print(2 ** 10) # 8 squared, or 64 print(8 ** 2) # 9 * 9 * 9, 9 cubed, or 729 print(9 ** 3) # We can even perform fractional exponents # 4 to the half power, or 2 print(4 ** 0.5) Here, we compute some simple exponents. We calculate 2 to the 10th power, 8 to the 2nd power, 9 to the 3rd power, and 4 to the 0.5th power.

The _______ doesn't just add two numbers, it can also "add" two strings! The process of combining two strings is called _________. Performing (2) creates a brand new string comprised of the first string's contents followed by the second string's contents (without any added space in-between).

+ operator string concatenation greeting_text = "Hey there!" question_text = "How are you doing?" full_text = greeting_text + question_text # Prints "Hey there!How are you doing?" print(full_text) In this sample of code, we create two variables that hold strings and then concatenate them. But we notice that the result was missing a space between the two, let's add the space in-between using the same concatenation operator! full_text = greeting_text + " " + question_text # Prints "Hey there! How are you doing?" print(full_text)

Python performs addition, subtraction, multiplication, and division with_____, _______, _____ and ______.

+, -, *, and / # Prints "500" print(573 - 74 + 1) # Prints "50" print(25 * 2) # Prints "2.0" print(10 / 5)

Python offers a shorthand for updating variables. When you have a number saved in a variable and want to add to the current value of the variable, you can use the_________. The (1) also can be used for _________, like so: (see back of card)

+= (plus-equals) operator string concatenation # First we have a variable with a number saved number_of_miles_hiked = 12 # Then we need to update that variable # Let's say we hike another two miles today number_of_miles_hiked += 2 # The new value is the old value # Plus the number after the plus-equals print(number_of_miles_hiked) # Prints 14 hike_caption = "What an amazing time to walk through nature!" # Almost forgot the hashtags! hike_caption += " #nofilter" hike_caption += " #blessed"

Comments can...

1. Provide context for why something is written the way it is 2. Help other people reading the code understand it faster 3. Ignore a line of code and see how a program will run without it:

What is a python string & how do you create them

A string in Python is a sequence of characters. It is a derived data type. Strings are immutable. This means that once defined, they cannot be changed. Many Python methods, such as replace(), join(), or split() modify strings. However, they do not modify the original string. They create a copy of a string which they modify and return to the caller. Python strings can be created with single quotes, double quotes, or triple quotes. When we use triple quotes, strings can span several lines without using the escape character. a = "proximity alert" b = 'evacuation' c = """ requiem for a tower """

What is Syntax

In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language.[1] This applies both to programming languages, where the document represents source code, and to markup languages, where the document represents data.

Python strings are very flexible, but if we try to create a string that occupies multiple lines we find ourselves face-to-face with a _______. Python offers a solution: ________. By using __________ instead of one, we tell the program that the string doesn't end until the (3). This method is useful if the string being defined contains a lot of quotation marks and we want to be sure we don't close it prematurely.

SyntaxError multi-line strings three quote-marks (""" or ''') leaves_of_grass = """ Poets to come! orators, singers, musicians to come! Not to-day is to justify me and answer what I am for, But you, a new brood, native, athletic, continental, greater than before known, Arouse! for you must justify me. """

Two common errors that we encounter while writing Python are SyntaxError and NameError What are they exactly ?

SyntaxError means there is something wrong with the way your program is written — punctuation that does not belong, a command where it is not expected, or a missing parenthesis can all trigger a SyntaxError. A NameError occurs when the Python interpreter sees a word it does not recognize. Code that contains something that looks like a variable but was never defined will throw a NameError.

Why is the print() function good for debugging

The print function in Python is a function that outputs to your console window whatever you say you want to print out. At first blush, it might appear that the print function is rather useless for programming, but it is actually one of the most widely used functions in all of python. The reason for this is that it makes for a great debugging tool. "Debugging" is the term given to the act of finding, removing, and fixing errors and mistakes within code. If something isn't acting right, you can use the print function to print out what is happening in the program. Many times, you expect a certain variable to be one thing, but you cannot see what the program sees. If you print out the variable, you might see that what you thought was, was not. You can follow this tutorial with the video and the embedded console, via the text and consoles, or via your own installation of Python.

________ offer a method of storing data for reuse. If there is a greeting we want to present, a date we need to reuse, or a user ID we need to remember we can create a ______ which can store a _____. In Python, we _______ by using the _______.

Variables - variable - value - assign variables - equals sign (=) # Greeting message_string = "Hello there" print(message_string) # Farewell message_string = "Hasta la vista" print(message_string)

Variables that are ___________ can be treated the same as the numbers themselves. Two variables can be added together, divided by 2, and multiplied by a third variable without _____________ (like the number 2 in this example). Performing arithmetic on variables does not change the variable — you can only update a variable ___________.

assigned numeric values Python distinguishing between the variables and literals using the = sign coffee_price = 1.50 number_of_coffees = 4 # Prints "6.0" print(coffee_price * number_of_coffees) # Prints "1.5" print(coffee_price) # Prints "4" print(number_of_coffees) # Updating the price coffee_price = 2.00 # Prints "8.0" print(coffee_price * number_of_coffees) # Prints "2.0" print(coffee_price) # Prints "4" print(number_of_coffees) We create two variables and assign numeric values to them. Then we perform a calculation on them. This doesn't update the variables! When we update the coffee_price variable and perform the calculations again, they use the updated values for the variable!

Text written in a program but not run by the computer is called a _______. Python interprets anything after a ____ as a ______

comment - # - comment

Python is a programming language. Like other languages, it gives us a way to ________. In the case of a programming language, these ideas are "________" that people use to communicate with a computer! We convey our commands to the computer by writing them in a ______ using a ____________. These files are called _______. Running a program means telling a computer to ______, ________, ________.

communicate ideas - commands - text file - programming language - programs - read the text file, translate it to the set of operations that it understands, and perform those actions

If you want to _______ a string with a number you will need to make the number a _________, using the _________. If you're trying to print() a numeric variable you can use commas to pass it as a different argument rather than converting it to a string. Using (3) we can convert ________ that are not strings to strings and then (1) them. But we don't need to convert a number to a string for it to ___________.

concatenate string first str() function variables be an argument to a print statement birthday_string = "I am " age = 10 birthday_string_2 = " years old today!" # Concatenating an integer with strings is possible if we turn the integer into a string first full_birthday_string = birthday_string + str(age) + birthday_string_2 # Prints "I am 10 years old today!" print(full_birthday_string) # If we just want to print an integer # we can pass a variable as an argument to # print() regardless of whether # it is a string. # This also prints "I am 10 years old today!" print(birthday_string, age, birthday_string_2)

Python refers to mistakes as ______ and will point to the location where an (1) occurred with a ____ _____. When programs throw (1) that we didn't expect to encounter we call those (1) ______. Programmers call the process of updating the program so that it no longer produces unexpected (1) ________.

errors ^ character bugs debugging

A ________, or a ______, is a decimal number. It can be used to represent fractional quantities as well as precise measurements. If you were measuring the length of your bedroom wall, calculating the average test score of a seventh-grade class, or storing a baseball player's batting average for the 1998 season you would likely use a (1).

floating-point number float an_int = 2 a_float = 2.1 print(an_int + 3) # prints 5 Above we defined an integer and a float as the variables an_int and a_float. We printed out the sum of the variable an_int with the number 3. We call the number 3 here a literal, meaning it's actually the number 3 and not a variable with the number 3 assigned to it.

One type of numeric data type Python has is... An ______, or (syntax), is a whole number. It has no decimal point and contains all counting numbers (1, 2, 3, ...) as well as their negative counterparts and the number 0. If you were counting the number of people in a room, the number of jellybeans in a jar, or the number of keys on a keyboard you would likely use an(1). Numbers can be assigned to ________ or used _______ in a program:

integer int variables literally

Notice that when we perform division, the result has a decimal place. This is because Python converts all ______ before performing division. In older versions of Python (2.7 and earlier) this conversion did not happen, and integer division would always round down to the nearest integer. Division can throw its own special error: _________. Python will raise this error when attempting to ______________.

ints to floats ZeroDivisionError divide by 0

The printed words that appear as a result of the print() function are referred to as ______ which appear on the _______.

output - console

In Python, the ______ is used to tell a computer to talk.

print() function -


Ensembles d'études connexes

Ch. 11: Axial and Appendicular Muscles

View Set

Young Adult Physical Development

View Set