Python Flashcards

1
Q

How do you keep the output of the next print on the same line?

A

end=’ ‘ inside of print()
EX: print(‘Hello there.’, end=’ ‘)

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

newline character escape sequence

A

\n

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

tab escape sequence

A

\t
print(‘Name\tJob’)
Name Job

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

\ escape sequence

A

\

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

escape sequence

A

two item sequence used to add in a special character such as new line, tab, and other symbols that may be used for other things in the language that need to be used as a literal, like ‘ “ \

a \ starts the escape sequence so it knows to use the next character literally

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

raw string

A

Escape sequences can be ignored using a raw string. A raw string is created by adding an “r” before a string literal, as in

r’this is a raw string'’
which would output as
this is a raw string'

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

convert string to int

A

int()
wage = int(input())

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

ask user for input and convert it to int

A

hourly_wage = int(input(‘Enter hourly wage: ‘))

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

syntax error

A

violates a programming language’s rules on how symbols can be combined to create a program

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

runtime error

A

program’s syntax is correct but the program attempts an impossible operation

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

logic error

A

aka a bug - a logical flaw

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

what are scripting languages used for?

A

to execute programs without the need for compilation

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

script

A

file that contains code designed to be executed directly

A script is a Python file (ending in .py) that contains code designed to be executed directly. When you run a script, the Python interpreter reads and executes all the lines in the file.

Use Case: Scripts are typically used for programs meant to be executed as standalone applications, such as automating tasks, running data analysis, or simple command-line programs.

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

garbage collection

A

Deleting unused objects is an automatic process that frees memory space

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

type()

A

returns the type of an object.

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

Mutability

A

indicates whether the object’s value is allowed to be changed

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

immutable

A

integers and strings are immutable; meaning integer and string values cannot be changed. Modifying the values with assignment statements results in new objects being created and the names bound to the new object.

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

id()

A

gives the value of an object’s identity

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

Properties of objects

A

Value: A value such as “20”, “abcdef”, or “55”.

**Type: ** The type of the object, such as integer or string.

Identity: A unique identifier that describes the object, which is usually the memory address where the object is stored.

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

floating-point number (float)

A

real number, like 98.6, 0.0001, or -666.667. The term “floating-point” refers to the decimal point appearing anywhere (“floating”) in the number

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

floating-point literal

A

written with the fractional part even if that fraction is 0, as in 1.0, 0.0, or 99.0

22
Q

syntax for outputting the float myFloat with two digits after the decimal point

A

print(f’{myFloat:.2f}’)

23
Q

exponent

A

**

24
Q

floor division operator

A

//
can be used to round down the result of a floating-point division to the closest smaller whole number value.

25
Q

module

A

file designed to be imported and used by other scripts or modules - not meant to be standalone

A module is also a Python file (ending in .py), but instead of being executed directly, it is designed to be imported and used by other scripts or modules. Modules are used to organize and reuse code efficiently.

Use Case: Modules are commonly used to break down a program into smaller, manageable, and reusable components. They are essential for larger projects, allowing you to structure your code more logically.

26
Q

import statement

A

makes modules available for use

27
Q

dot notation

A

used to access any defined object in an imported module

Ex: A variable speed_of_light defined in universe.py is accessed via universe.speed_of_light.

28
Q

__name__

A

used to check if a file was executed as a script, or imported as a module

use this built-in special name (like an embedded nametag given to each file) to determine if the file was executed as a script by the programmer or imported by another module. If the value of __name__ is the string ‘__main__’, then the file was executed as a script, otherwise, it would be the name of the file that called it

29
Q

randint(min, max)

A

returns a random integer between min and max inclusive.

Returns a random integer between 12 and 20 inclusive
print(random.randint(12, 20))

30
Q

randrange(min, max)

A

returns a random integer between min and max - 1 inclusive.

Returns a random integer between 12 and 19 inclusive
print(random.randrange(12, 20))

31
Q

seed

A

built-in integer based on the current time

32
Q

List

A

[] - ordered collection - mutable (size can be changed) - can store multiple data types, whereas an array can only store a single datatype. (in python, arrays must use the array module)

33
Q

Difference between methods and functions

A
| **Aspect**         | **Function**                                   | **Method**                                      |
|--------------------|------------------------------------------------|-------------------------------------------------|
| **Definition**     | Independent block of code                      | Block of code defined inside a class            |
| **Association**    | Not associated with any object or class        | Associated with an object of a class            |
| **Calling**        | Called using the function name directly        | Called using the `.` operator on an object      |
| **Use of `self`**  | No `self` parameter needed                     | Uses `self` to refer to the instance of the class |

Aspect | Function | Method |

34
Q

method

A

A piece of code that belongs to an object or class and often works with that object’s data.

A method instructs an object to perform some action, and is executed by specifying the method name following a “.” symbol and an object.

35
Q

function

A

A standalone piece of code you can use anywhere.

36
Q

tuple

A

stores a collection of data, like a list, but is immutable – once created, the tuple’s elements cannot be changed

used when element position, and not just the relative ordering of elements, is important. Ex: A tuple might store the latitude and longitude of a landmark because a programmer knows that the first element should be the latitude, the second element should be the longitude, and the landmark will never move from those coordinates.

37
Q

set

A

unordered collection of unique elements. A set has the following properties:
Elements are unordered: Elements in the set do not have a position or index.
Elements are unique: No elements in the set share the same value.

A set is often used to reduce a list of items that potentially contains duplicates into a collection of unique values. Simply passing a list into set() will cause any duplicates to be omitted in the created set.

38
Q

dictionary

A

a Python container used to describe associative relationships.

created using curly braces { } to surround the key:value pairs that comprise the dictionary contents.
Ex: players = {‘Lionel Messi’: 10, ‘Cristiano Ronaldo’: 7}

39
Q

Sequence types

A

string, list, and tuple are containers for collections of objects ordered by position in the sequence, where the first object has an index of 0 and subsequent elements have indices 1, 2, etc. A list and a tuple are very similar, except that a list is mutable and individual elements may be edited or removed. Conversely, a tuple is immutable and individual elements may not be edited or removed. Lists and tuples can contain any type, whereas a string contains only single characters. A programmer can apply sequence-type functions such as len() and element indexing using brackets [ ] to any sequence type.

40
Q

mapping type

A

in Python is the dict type. Like a sequence type, a dict serves as a container. However, each element of a dict is independent, having no special ordering or relation to other elements. A dictionary uses key-value pairs to associate a key with a value.

41
Q

Formatted string literals (f-strings)

A

allows a programmer to create a string with placeholder expressions that are evaluated as the program executes. An f-string starts with an f character before the starting quote and uses curly braces { } to denote the placeholder expressions. A placeholder expression is also called a replacement field, as its value replaces the expression in the final output.

42
Q

Slice notation

A

Multiple consecutive characters can be read using slice notation. Slice notation has the form my_str[start:end], which creates a new string whose value contains the characters of my_str from indices start to end -1. If my_str is ‘Boggle’, then my_str[0:3] yields string ‘Bog’.

43
Q

alignment character

A

A format specification can include an alignment character that determines how a value should be aligned within the width of the field. Alignment is set in a format specification by adding a special character before the field width integer. The basic set of possible alignment options include left-aligned (<), right-aligned (>) and centered (^). Numbers will be right-aligned within the width by default, whereas most other types like strings will be left-aligned.

44
Q

enumerate()

A

The enumerate() function retrieves both the index and corresponding element value at the same time, providing a cleaner and more readable solution.

45
Q

parameter

A

A parameter is a function input specified in a function definition. Ex: A pizza area function might have diameter as an input.

46
Q

argument

A

An argument is a value provided to a function’s parameter during a function call. Ex: A pizza area function might be called as calc_pizza_area(12.0) or as calc_pizza_area(16.0).

47
Q

polymorphism

A

The function’s behavior of adding together different types

For example, consider the multiplication operator *. If the two operands are numbers, then the result is the product of those two numbers. If one operand is a string and the other an integer (e.g., ‘x’ * 5), then the result is a repetition of the string five times: ‘xxxxx’.

48
Q

*args

A

Sometimes a programmer doesn’t know how many arguments a function requires. A function definition can include an *args parameter that collects optional positional parameters into an arbitrary argument list tuple.

49
Q

Unpacking

A

a process that performs multiple assignments at once, binding comma-separated names on the left to the elements of a sequence on the right. Ex: num1, num2 = [350, 400] is equivalent to the statements num1 = 350 and num2 = 400.

50
Q

**kwargs

A

keyword arguments, creates a dictionary containing “extra” arguments not defined in the function definition. The keys of the dictionary are the parameter names specified in the function call.

51
Q

help() function

A

aids a programmer by providing them with all the documentation associated with an object. A statement such as help(ticket_price) would print out the docstring for the ticket_price() function, providing the programmer with information about how to call that function.

52
Q
A