What are the use of comments in python?
What does print mean?
The “print” word seen in the statement below is a python keyword. That means it is part of the python language, which makes it a reserved word. This word cannot be used for any other purpose other than printing information to the screen.
What is a string/string literal?
Anything in quotation marks is a “String Literal”. A string is simply a string or set of characters.
This is an explicit statement that says “print to the console whatever is in the quotes”. print(“Hello World!”)
How do you write multiple print statements?
We can create multiple print statements to print multiple items on a seperate line.
print(“Jack be nimble,”)
print(“Jack be quick,”)
print(“Jack jumped over”)
print(“The candlestick.”)
print()
What does it mean to concatenate?
The most common use of concatenation in Python is with strings. You can combine strings using:
The + operator: This operator directly joins two strings together.
We can use of the addition operator to contatenate string literals. print(“Corpus Christi Catholic Secondary” + “5150 Upper Middle Road” + “Burlington, Ont” + “L7L 0E5”) print()
use plus sign as a concatenation operation to join strings in one argument
What is \n?
New line Escape Sequence charcters:
print(“Welcome to Corpus Christi” + “\nDignity” + “\nEquity” + “\nRespect”)
print()
How do you use \n without concatenation?
print(“This is my grocery list:\nMilk\nBread\nApples\nPeaches\nPears”)
print()
What is \t?
Tab Escape sequence characters:
print(“These are my favourite video games:\n\tOverwatch\n\tSmash Brothers\n\tThe Legend of Zelda”)
What are variables?
How does a programmer create and initialize a variable?
The programmer decides on the variable name. As a general rule, the variable name should represent the piece of data that we want to hold. The name that one uses for a variable must be written as one word and it must not contain spaces.
temperatureSensor = 1
vehiclePosition = 230
droneHeight = 2000
wallDistance = 100
What is allowed and not allowed in variable names?
my Name = “Mr. Petti” Variables cannot have spaces, therefore this would not work
myName = “Mr. Petti” # This variable name would work
Variables Cannot use Symbols such as !, @, #, $, %, ^, &, *
total%Value = 17 This variable name would not work
totalPercentageValue = 0.25 #This variable name will work
# 99problems This variable name would not work
problems99 = 2 # This would work
problems_99 = 3 # This would work as well
# Variables are case sensative. Case Matters!
x = 15.2
X = 18
age = 23
What does the input function do?
The raw_input function does two things. First, it asks for a name and then it stores that name in a variable called “name”. If we do not place the user’s input in a variable, Python will have no way of remembering what the user entered.
By default, whatever is entered by the user, will be assigned to the variable “name” as a string literal. When the raw_input function is called in Python, the program pauses at this line and waits for someone to type in an answer.
Python continues to the next line in our program when somebody finally types something and presses enter.
print(“Hello “ + name + “, welcome to Python. You are a “ + profession +” at “ + institution +”.”)
What is the use of a comma in python?
print(“Hello” ,name + “, welcome to Python. You are a” ,profession, “at” ,institution, “.”)
This statement uses the comma to create a list of elements. The comma automatically concatenates all elements and it separates each by one space. The elements in the statement above are “Hello” “name” “, welcome to Python. You are a” “profession” “at” “institution” “.”
What are the most common kind of values in computer programs?
The most common kind of values in computer programs are numeric values. When one creates a program, one is ususally representing concepts seen in the real world, for example, if we were creating a program such as Google maps, then we would need to create a variable that holds the x and y location coordinates of a car. If we were working on a robotic arm, we would have to create a variable for its extension and a separate variable for its rotation.
When we create a program, we are dealing with a multitude of values, therefore you need to know how to work with numbers when programming because everything is represented by a number.
What is a command?
a = 5
This is a command. This is an instruction saying “take the value 5 and put it in the variable called a”.
What does the = sign mean?
The = sign assigns a value… that is why it is called an “assignment operator”. Notice that there are no quotes around the number 5. The number 5 here is referred to as a “numeric literal”. It represents the fixed value 5.
How is a variable changed?
a = 1000000
We can change what is contained in the variable a by assigning it another value. After this line executes, a no longer contains 5, but instead contains the value 1000000. Notice that we do not use commas, we just use the number itself. Using commas would make the variable a into a list.
What is a floating point number?
a = 123.654
This is considered a floating point number. All floting point numbers contain decimals.
What is the difference between no quotes and quote when assignning a varaible?
Notice that quotes are not used. We don’t want to write out the letter a to the console. We want to write out the value of the variable a, which is what this line is going to do.
b = 123
c = “123”
One final note. Understand there’s a very big difference between creating a variable like b = 123, and creating a variable like c = “123”. The b, is a number, 123. The second, c, is a string. Not the number 123, but the digits 1, 2, 3 and sometimes that’s a pretty big difference in behavior.
print(type(b), b)
print(type(c), c)
What are the operators?
The most obvious operators are the arithmetic operators. We have operators for addition, subtraction, multiplication, and division.
What does this mean, a = 20?
a = 20
This statement creates a variable named a and initializes it with a value of 20.
How to create more than one variable on the same line?
a, b, c = 100, 50, 12.1
What is the difference with using concatenation and a comma with operators stored in total?
print (“Example 1 (With concatenation): “ + str(total))
With concatenation, you must cast the integer variable into a string
print (“Example 1 (With a comma):”, total)
One does not need to use the cast when using a comma to create a list
What is operator precedence?
Operator precedence is the set of rules that dictates the order in which operations are performed in an expression
Operator Precedence
result = 5 + 5 * 10
This statement first evaluates 5 times 10 and then adds 5.
print (“Example 2: “ + str(result))
This statements changes result into a string before it can be concatenated with the