Python Overall Flashcards

1
Q

What is a Value in Python?

A

A value is one of the basic things a program works with. A value can be a letter or a number. Examples of values are 1, 2, and ‘Hello, World!’

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

What are examples of Value Types?

A

2 is an integer, and ‘Hello, World!’ is a string, so-called because it contains a “string” of letters.

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

How can we identify strings? because they are enclosed in quotation marks.

A

You can identify strings because they are enclosed in quotation marks. Python gives us the freedom to use either single or double quotes, so the string ‘Hello, World’ and “Hello, World!” are the same string. Note, however, that we must begin and end every string with the same type of quotation mark.

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

(True or False) Commas act as separators in Python.

A

True

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

What does a Print Statement allow us to do?

A

The print statement allows us to display both strings and integers when we run the created program.

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

(True or False) If you are not sure of your Value Type, the Python Interpreter can tell you the values when you run your program.

A

True

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

What is the type code for String?

A

str

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

What is the type code for Integers?

A

int

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

In Python, numbers with a decimal point belong to a type called?

A

float

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

What is a Float?

A

Numbers with a decimal point belong to a type called float because these numbers are represented in a format called floating-point.

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

How do you identify types in Python?

A

By calling the type function into your statement - E.g. print(type(“Hello World!”)) This will define the type for you.

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

(True or False) When using underscores in large numbers underscores are included.

A

False

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

(True or False) A variable is a name that refers to a value.

A

True

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

What can be used for Variable names?

A

They can contain letters, numbers, and underscores. However, they cannot start with a number. Although it is legal to use uppercase letters, it is customary to use all lower-case letters in variables names.

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

(True or False) The underscore character _ can appear in a name.

A

True. It is often used in names with multiple words, such as my_name, etc. Variable names can start with an underscore character, but we generally avoid doing this unless we are writing library code for others to use.

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

What is a Syntax error?

A

Python can only execute a program if the syntax is correct; otherwise, the interpreter displays an error message. Syntax refers to the structure of a program and the rules about that structure. For example, parentheses have to come in matching pairs, so (1 + 2) is legal, but 8) is a syntax error. If there is a single syntax error anywhere in your program, Python will display an error message and quit, and you will not be able to run your program.

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

What is a Runtime error?

A

This error does not appear until after the program has started running. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.

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

What is a Semantic error?

A

If there is a semantic error in your program, it will run successfully in the sense that the computer will not generate any error messages, but it will not do the right thing. It will do something else. Specifically, it will do what you told it to do.
The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing.

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

Explain the Python String identifier() Method.

A

A string is considered a valid identifier if it only contains alphanumeric letters (a-z) and (0-9), or underscores (_). A valid identifier cannot start with a number, or contain any spaces.

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

Python has a number of other keywords or reserved words. Explain.

A

The keywords or reserve words are used to define the syntax and structure of the Python language. In Python, keywords are case-sensitive. There are 33 keywords in Python 3.7. This number can vary slightly over the course of time.

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

Python reserves 31 keywords for its use:

A

This is the list(however it may vary):
and - del - from - as - elif - global - assert - else - if - break - except - import - class - exec - in - continue - finally - is - def - for - lambda not - while - or - with - pass - yield - print - raise - return - try

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

Explain the function of Keywords in Python.

A

Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name, or any other identifier. They are used to define the syntax and structure of the Python language.

23
Q

In Python, what is the interpreter, and how does it works?

A

An interpreter is a kind of program that executes other programs. When you write Python programs, it converts source code written by the developer into an intermediate language which is again translated into the native language/machine language that is executed.

24
Q

In Python, what is a Statement?

A

A statement is a unit of code that the Python interpreter can execute. A Python program contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.

25
Q

Give an example of two kinds of Statements in Python.

A

Print and Assignment

26
Q

Programmers go through a process when creating a program. What does it look like?

A
  1. They design the algorithm. They document the steps that the program needs to perform to work correctly. This is done in English and we normally call this pseudo-code.
  2. They code the pseudo-code in a programming language. In our case that will be Python.
  3. They execute, or run, the program looking for defects. This is called testing.
  4. Each time that they discover a defect they analyze the issue and fix it. Then they go back to step 3 and continue testing it until they are confident that the defects have been fixed.
27
Q

What is a program?

A

A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or (strangely enough) compiling a program.

28
Q

Name 5 basic instructions that appear in just about every language:

A

Input: Get data from the keyboard, a file, or some other device.

Output: Display data on the screen or send data to a file or other device.

Math: Perform basic mathematical operations like addition and multiplication.

Conditional execution: Check for certain conditions and execute the appropriate code.

Repetition: Perform some action repeatedly, usually with some variation.

29
Q

What is debugging?

A

Programming is error-prone. For whimsical reasons, programming errors are called bugs and the process of tracking them down is called debugging.

30
Q

What are Operators and Operands?

A

Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands.

The operators +, -, *, /, and ** perform addition, subtraction, multiplication, division, and exponentiation, as in the following examples:

20 + 32
year - 1
price * tax_rate
90 / 60
5 ** 2
31
Q

(Complete the statement) To see the values generated by the mathematical formulas, we must…

A

We must be sure to first assign values to the variables and then use the print function to display them.

32
Q

What is an Expression?

A

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions - (assuming that the variable x has been assigned a value):
17
x
x + 17

33
Q

Explain Operator Precedence and Associativity.

A

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. For mathematical operators, Python follows mathematical convention.

34
Q

What does the acronym PEMDAS stand for?

A
  • Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3 - 1) is 4, and (1 + 1) ** (5 - 2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even if it doesn’t change the result.
  • Exponentiation has the next highest precedence, so 2 ** 1 + 1 is 3, not 4, and 3 * 1 ** 3 is 3, not 27.
  • Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So, 2 * 3 - 1 is 5, not 4, and 6 + 4 / 2 is 8, not 5.
  • With the exception of exponentiation, operators with the same precedence are associated from left to right. So, the expression 5 - 3 - 1 is 1, not 3, because the 5 - 3 happens first and then 1 is subtracted from 2.
35
Q

Explain Integer Division and Modulus Operators.

A

Python also has a second division operator that is used to perform integer division, which is the division that truncates the result. The symbol for this operator is // rather than just /.

36
Q

What is an Integer?

A

An integer is a whole number (not a fractional number) that can be positive, negative, or zero. Examples of integers are: -5, 1, 5, 8, 97, and 3,043. The integers are generated from the set of counting numbers 1, 2, 3, . . . and the operation of subtraction. When a counting number is subtracted from itself, the result is zero.

37
Q

There is one additional operator that is often used in conjunction with the // operator, which is %. Explain.

A

Although the symbol is the percent symbol, this operation does not involve taking a percentage but instead gives the remainder after performing integer division. It is referred to as the modulus or remainder operator.

38
Q

What is truncated in math?

A

Truncating is a method of approximating a decimal number by dropping all decimal places past a certain point without rounding. Example: 3.14159265 can be truncated to 3.1415.

39
Q

What is the meaning of concatenation?

A

The concatenation of two or more numbers is the number formed by concatenating their numerals. … For example, the concatenation of 1, 234, and 5678 is 12345678. The value of the result depends on the numeric base, which is typically understood from the context.

40
Q

Explain how the + operator works with strings.

A

The + operator works with strings, but it is not addition in the mathematical sense. Instead, it performs concatenation, which means joining the strings by linking them end to end. The type of operands will determine whether that operator performs addition or concatenation.

41
Q

What is User Input? (Soft of set up as a two-way communication designed to collect information or exchanges from the user or (target audience). A script or series of questions that prompts a reply from the user or (target audience).

A

Python user input from the keyboard can be read using the input() built-in function. The input from the user is read as a string and can be assigned to a variable. After entering the value from the keyboard, we have to press the “Enter” button. Then the input() function reads the value entered by the user. The program resumes, based on established input returns, after assessing what the user typed as a string.

42
Q

What is the symbol or Escape Character for Next Line?

A

The character sequence \n represents a newline, which is a special character that causes a line break.

43
Q

In Python, what is a Glyph?

A

A glyph is a vectorized graphical shape or marker that is used to represent your data. A few categories of Glyph are shapes like circles, diamonds, squares, and triangles. Character sequences that begin with \ are often also referred to as escape characters. They can be used to represent characters that do not have an associated glyph.

44
Q

What is Eval?

A

The Eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer.

45
Q

How can you add Comments to Python?

A

As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why.

For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing.

These notes are called comments, and in Python, they start with the # symbol.

When the # symbol is the first symbol on the line, the whole line is a comment.

You can also put comments at the end of the line after other Python code.

Everything from the # to the end of the line is ignored—it has no effect on the program.

There is no evidence of the comments in the output.

46
Q

What is the best way to use Comments?

A

Comments are most useful when they document non-obvious features of the code. It is reasonable to assume that the reader can figure out what the code does; it is much more useful to explain why.

Good variable names can reduce the need for comments, but names that are too long can make complex expressions hard to read, so there is a trade-off.

47
Q

What is Pseudocode?

A

It is a technique for designing the control structure of programs. Pseudocode combines English with important keywords of Python.
Pseudocode will consist primarily of a high-level English description of the required actions. When we translate our pseudocode into Python, we will retain the pseudocode as comments.

48
Q

Explain Python Assignment Statements.

A

Python assignment statements assign objects to names. The target of an assignment statement is written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that computes an object. Assignment creates object references instead of copying the objects.

49
Q

Explain the Python print() Function

A

The print() function prints the specified message to the screen or other standard output device. The message can be a string or any other object, the object will be converted into a string before written to the screen.

50
Q

What is a Boolean Expression?

A

A Boolean expression is an expression that is either true or false.
The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise.
So 5 == 5 would evaluate to True, whereas 5 == 6 would evaluate to False.

Like arithmetic expressions, Boolean expressions can be assigned to variables:

different = 5 == 6

51
Q

(True or False)True and False are special values that belong to the type bool; they are not string.

A

True.

52
Q

Comparison Operators

The == operator is one of six comparison operators for comparing values; the others are:

A
x == y    #x is equal to y 
x != y     #x is not equal to y
x > y      #x is greater than y
x < y      #x is less than y
x >= y     #x is greater than or equal to y
x <= y     #x is less than or equal to y
53
Q

Order of Operations of Logical Operators

A

not #higest precedence
and # middle precedence
or #lowest precedence