Python Syntax

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Without quotes will cause a

NameError This means that Python found what it thinks is a command, but doesn't know what it means because it's not defined anywhere

Mismatched quotes will cause what error example: print "Mismatched quotes will cause a'

SyntaxError: EOL while scanning a string literal. This mean that the string wasn't closed with the same quote-character that staryed it

Sometimes we have a need for variables that are either true or false. This datatype, which can only ever take one of two values, is called a _____. In Python, we define these using the keywords True and False

They are called a boolean a = True b = Flase

Python uses what to define things that are subject to change.

Variables

You can also define a float using scientific notation, with this letter indicating the power of 10

e example: # this evaluates to 150: float4 = 1.5e2

Updating Variables Examples: we start with 50 fish in a local pond the 10 were fished out so we set the variable " fish_in_clarks_pond equal to 40

fish_in_clarks_pond = 50 print "Catching fish" number_of_fish_caught = 10 fish_in_clarks_pond = fish_in_clarks_pond - numer_of_fish_caught

A number with a decimal point is called a float. You can define floats with numbers after the decimal point or by just including a decimal point at the end

float1 = 1.0 float2 = 10. float3 = -5.5

Create a variable called float_cucumbers_per_person that holdfsd the float result of dividing cucumbers by num_people. Print float_cucumbers_per_person to the console

float_cucmbers_per_person = float(100)/6

Create a varible called haiku and store this as multi-line string: The old pond, A frog jumps in: Plop!

haiku = """The old pond A frog jumps in: Plop!""" print haiku

Variables can also hold numeric values. The simplest kind of number in Python is the integer, which is a whole number with no decimal point

int1 = 1 int2 = 10 int3 = -5

Example of Python Arithmetic

mirthful_addition = 12381 + 91817 amazing_subtraction = 981 -312 trippy_multiplication = 38 * 902 happy_division = 540 / 45 sassy_combinations = 129 * 1345 + 120 / 6 -12

if we have a string like "7" and we want to perform arithmetic operations on it, we must convert it to a numeric datatype. We can do this using int()

number1 = "100" number2 = "10" string_addition = number1 + number2 #string_addition now has a value of "10010" int_addition = int(number1) + int(number2) #int_addition has a value of 110

Each cucumber costs 3.25 doubloons. Store the price per cucumber in a variable called price_per_cucumber

price_per_cucumber = 3.25

Try adding your name to the print statement with the + operator so that this Python program prints "Hello [your_name]"

print "Hello " + "Rico "

In Python the way to get the computer to speak is by using the _____ statement

print ststement

Print out total_cost. What datatype is it?

print total_cost The datatype it is a float

Print a string to the console that says: "I got x points!" with the value of point_total where x is

print("Igot"+str(point_total)+" points!")

Multiply two numbers together and assign the results to a variable called product

product = 2 * 2

Create a variable called product that contains the result of multiplying the float value of float_1 and float_2

product = float_1 * float_2

In python 2 when we divide two integers, we get an integer as a result. When the quotient is a whole number, this works fine:

quotient = 6/2 # the value of quotient is now 3, which makes sense

If the numbers do not divide evenly , the result of the division is truncated into an integer. In other words, the quotients is rounded down to a whole number. This can be surprising when you expect to receive a decimal and you receive a rounded-down integer

quotient = 7/2 # the value of quotient is 3, even though the result of the division here is 3.5

to yield a float as the result instead, programmers often change either the numerator or the denominator ( or both) to be a float:

quotient1 = 7./2 # the value of quotietnt1 is 3.5 quotient2 = 7/2. # the value of quotient2 is 3.5 quotient3 = 7./2 # the value of quotient3 is 3.5

An alternative way is to use the "Float()" method

quotient1 = float (7)/2 # the value of quotient1 is 3.5

What is the remainder when 1398 is divided by 11? Save the results in a variable called remainder.

remainder = 1398 % 11

If you use int() on a floating point number, it will round the number down. To preserve the decimal, you can use float()

string_num = "7.5" print int(string_num) print float(string_num) >>> 7 >>> 7.5

Create a variable called 'todays_date" and assign a value that will represent today's date to that variable.

todays_date = "12/14/2021"

Create a new variable called total_cost which is the product of how many cucumbers you are going to buy and the cost per cucumber

totoal_cost = cucmbers * price_per_cucumber

Create a varible called whole_cucumbers_per_person that is the integer result of fividing cucumbers by num_people Print whole_cucumbers_per_person to the console.

whole_cucumbers_per_person = 100/6

This address spans multiple lines, and is still contained in one varible, address_string. Whenm a string like this is not assigned to a variable, it works aas a multi-line comment. This can be helpful as tour code gets more complex:

""" The following piece of

When a string like this is not assigned to a variable, it works as a multi-line comment. This can be helpful as your code gets more complex:

"""The following piece of code does the following steps: takes in some input does An Important Calculation returns the modified input and a string that says "Success!" or "Failure..." """ ... a complicated piece of code here...

if you want to include a piece of information to explain a part of your code you can use this sign a text preceded by this sign is called a COMMENT

# sign

Python also offers a companion to division called the modulo operator. The modulo operator is indicated by _____ and returns the remainder after division is performed.

% percentage sign

A string can be defined in two ways but it needs to be closed by the same type of quote mark

(''),(') double quotes and single quotes

There are two different Python versions Python 2 and Python. The most significant difference between the two is how you write a PRINT statement. In Python 3, print has ?

() parentheses example : print("Hello World!")

We can combine multiple strings by using the

+ plus like so : print "This is " + "a good string"

A string in Python is

a series of letters, numbers, or symbols connected in order

We have seen how to define a string with single quotes and with double quotes. if we want a string to span multiple lines, we can also use triple quotes :

address_+string = """136 Whowho Rd Apt 7 Whosville, WZ 44494

Python automatically assigns a variable the appropriate datatype based on the value it is given. A variable with the value 7 is an integer, 7. is a float,"7" is a string. sometimes we will want to convert variables to different datatypes using str()

age = 13 print "I am " + str(age) + " years old!" This would print out to " I am 13 years old!"

Create a string called big_string that says: The product was x with the value of product where the x is

big_string = "The product was " + str(product)

example of comments in Python

city_name = "St. Potatosburg # St. Potatosburg is know for its potatos city_pop = 340000

Store the number of cucumbers you want to buy in a variable called cucumbers. Make sure it's at least 1, and that it's the appropriate datatype!

cucumbers = 1

Create a variable cucumbers that holds 100 and num_people that holds 6

cucumbers = 100 num_people =6


Set pelajaran terkait

(Complete) Chapter 13: Local markets, poverty, and Income Distribution

View Set

Comic Spirit Final Study Guide FALL2018

View Set

Vocabulary Workshop Word Families #11

View Set

JavaScript operator,arithmetic,assignment,Data Type,functions, objects

View Set