Intro to Python Test Flashcards

(54 cards)

1
Q

What are the use of comments in python?

A
  • Comments are a way to keep track of what is happening in your code.
    They are useful if you are working in a team environment because other coders will be looking at, and building upon your code, therefore they will need to know your line of thinking.
  • Comments are also useful for yourself because as your programs become more complex, you will need to keep track of what is going on. You will also find comments useful when you revisit your code, days, weeks or months down the road. You begin comments in python with a hash tag. Any line beginning with a hash tag will be ignored by the interpreter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does print mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a string/string literal?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you write multiple print statements?

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does it mean to concatenate?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is \n?

A

New line Escape Sequence charcters:
print(“Welcome to Corpus Christi” + “\nDignity” + “\nEquity” + “\nRespect”)
print()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you use \n without concatenation?

A

print(“This is my grocery list:\nMilk\nBread\nApples\nPeaches\nPears”)
print()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is \t?

A

Tab Escape sequence characters:

print(“These are my favourite video games:\n\tOverwatch\n\tSmash Brothers\n\tThe Legend of Zelda”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are variables?

A
  • Variables are simply containers.
  • When we create a variable, we are reserving a space in RAM to hold the contents of that variable. We take this space, give it a name and a value.
  • Think of a variable as a shoe box with a specific name on it. We give the shoe box a name and we insert data into that box.
  • Data could be a words or numbers however it cannot be both.
  • Variables can only hold one item of data at a time. For example, it cannot hold a name name and a number, it can only hold one or the other.
  • Once we create a variable, the contents of that variable can change while the program is running, so at one point it can hold a value, and then at another point in the program, it can hold a completely different value.
  • Variables can be made of letters, numbers, the dollar sign and the underscore character, no other characters are allowed. Also a varable cannot begin with a number.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does a programmer create and initialize a variable?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is allowed and not allowed in variable names?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the input function do?

A

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 +”.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the use of a comma in python?

A

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” “.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the most common kind of values in computer programs?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a command?

A

a = 5
This is a command. This is an instruction saying “take the value 5 and put it in the variable called a”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the = sign mean?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How is a variable changed?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a floating point number?

A

a = 123.654
This is considered a floating point number. All floting point numbers contain decimals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the difference between no quotes and quote when assignning a varaible?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the operators?

A

The most obvious operators are the arithmetic operators. We have operators for addition, subtraction, multiplication, and division.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does this mean, a = 20?

A

a = 20
This statement creates a variable named a and initializes it with a value of 20.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to create more than one variable on the same line?

A

a, b, c = 100, 50, 12.1

23
Q

What is the difference with using concatenation and a comma with operators stored in total?

A

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

24
Q

What is operator precedence?

A

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

25
What is the modulus operator?
modulus = 5 % 2 print ("Example 4: " + str(modulus)) The modulus operator returns the remainder of a division... 2 goes into 5 twice, with a remainder of 1. Therefore example 4 will return a value of 1.
26
What is common with varaible in python?
One thing that is very common is to see the same variable name on both sides of the equals sign. score = 10 score = score + 10 This statement takes the current value of score and adds 10 to it. The result is score will equal 20. print ("Example 5:", score) it is an accumluating variable
27
What is score += 10?
This type of calculation is so common in programming that there exists a shortcut. score += 10 This statement is the same as saying score = score + 10 print ("Example 6:", score) score -= 10 score *= 10 score /= 10
28
How do you calculate percentages?
Write a program that calculates the tax on a $20 item. The program should print the total tax of the item and the total price of the item including the tax. price = 20 tax = 0.13 totalTax = price * tax print ("The tax on a $%.2f item is $%.2f" %(price, totalTax)) totalPrice = price + totalTax print ("The price of the item with the tax is $%.2f" %totalPrice)
29
Explain the conversion between changing input into string to number?
number1 = input("Enter the fisrt number: ") The value entered by the user will be saved as a string literal. We must change it into a numeric literal. number1 = float(number1) This line changes a string into number. number2 = input("Enter the second number: ") The value entered by the user will be saved as a string literal. We must change it into a numeric literal. number2 = float(number2) This line changes a string into number. total = number1 + number2 This line finally calculates the sum of the two numbers entered print(total)
30
What is white space?
You have to program a space in between lines - if you press enter in the code its called white space but it won’t appear in display Space in between line - code print() so the code will advance it to next line and print nothing compiler does not read white space
31
What are escape sequence charcaters?
Escape sequence characters - \t and \n (new line character) - allows us to format our code
32
What is variable declaration?
Variable declaration - when we create a variable (name of variable) - Start variable with lower case and any subsequent word starts in uppercase for readability - structure is named camel case convention (no spaces)
33
What is variable initilization?
Variable initialization - when we give a variable a value - different because we could name it but it could be empty
34
What is a casting operator?
Instead, the process of converting one data type to another is referred to as type casting or type conversion Casting operator - str is there because age is an integer value (different type of data - can't concatenate 2 different types of data) str makes age = string
35
what is camel case convention?
Camel case is a naming convention where multiple words in an identifier are joined together without spaces, and the first letter of each word (except possibly the first word) is capitalized.
36
what is the import random?
This statement imports the random module
37
what is the import math?
This statements imports the math module, allows us to do complex math
38
what does the random.random function do?
The random functon generates a number between 0 and 1. a = random.random() print ("Example 1:", a) WONT generate one but can generate zero
39
what does random.randrang do?
Generate a random interger number between a certain range b = random.randrange(20, 40) print ("Example 2:", b) excluding 40, upper bound exclusive(the range of values extends up to, but does not include, the specified boundary value)
40
what does random.uniform do?
Generate a random floating point number between a certain range c = random.uniform(5, 25) print ("Example 3:", c) INCLUDES 25
41
what does the min function do?
The min function finds the smallest number in a list of numbers. d = min(45, 34, 67, 90, 126, 54, 39, -19) print ("Example 4:", d)
42
what does the max function do?
The max function finds the largest number in a list of numbers. l = max(45, 34, 67, 90, 126, 54, 39, -19) print ("Example 5:", l)
43
what does the round function do?
The round function rounds a number to the smallest or highest value. f = round(4.7) print ("Example 6:", f)
44
what does the ceil function do?
The ceil function rounds a number to the highest possible value... e.g 4.4 to 5 g = math.ceil(4.4) print ("Example 7:", g)
45
what does the floor function do?
The floor function rounds a number to the lowest possible value... e.g 4.7 to 4 h = math.floor(4.7) print ("Example 8:", h)
46
what does the pi function do?
The PI function generates the Pi constant i = math.pi print ("Example 9:", i)
47
what does the pow function do?
The pow function accepts 2 arguments... argument 1 to the power of argument 2. j = math.pow(2, 4) print ("Example 10: ", j) also adds a 0 to it
48
what does the sqrt function do?
The sqrt function returns the square root of a specified number k = math.sqrt(9) print ("Example 11:", k)
49
what makes a varaible completely different?
underscore or changes in capitalization
50
what are random modules/libraraies?
The Python random module offers you a variety of functions for generating random numbers in various formats, ranging from integers to floating-point numbers to selecting elements from lists, sepearte code that allows you to create random numbers
51
what is the general format with random or math functions?
General format -- variableName = random or math.function(value)
52
what functions use random module?
Functions that use random module - random, randrange, uniform
53
what functions use math module?
Functions that use math module - ceil, floor, pi, pow, sqrt
54
what is a module?
Simply put, a module is a file consisting of Python code. It can define functions, classes, and variables and can also include runnable code