Week 1: Variables, Operators, Strings Flashcards

1
Q

a) Define Variable

b) In python everything is an ______

c) Variables _______objects stored in a computer’s memory

d) Python does not require variables to be _____ ______ (ie. you can create a variables by simply assigning it a value)

A

a) Variable: Named area in a computer’s memory that can store a value

b) In python, everything is an object

c) Variables point to (reference) objects stored in the computer’s memory

d) Python does not require variables to be explicitly stated (you can create a variable by simply assigning it a value)

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

Define identifier

What are the rules for variable or function identifiers?

A

An identifier is a name we give to variables or functions

  • In python there are certain rules we must follow when naming variables or functions
  1. Case sensitive
  2. Must start with a letter or underscore
  3. Can only contain letters, digits, or underscores
  4. Can not be identical to a built-in keyword
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Some common naming conventions for identifiers

A

Some common naming conventions:

  • snake_case
  • camelCase
  • SCREAMIG_SNAKE_CASE
  • SCREAMINGCASE
  • UpperCamelCase (aka. Pascal Case)
  • flatcase
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Style guidelines for variable identifiers

A
  • should always have a meaningful name
  • try not to make it too long
  • should follow a consistent style
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is PEP8
Naming conventions under PEP8

A

PEP8 is python’s recommended style guide

PEP8 Naming Conventions:

  • snake_case should be used for variable and function identifiers
  • Never use the characters “I” “l” or “O” as single character variable names
  • Shoud only use English words and letters whenever feasible
  • SCREAMING_SNAKE_CASE should be used for constants
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define Constants

A

Constants are variables whose value should never change after its initial value set

  • python does not enforce this, but you should let other programs know a value is a constant by using SCREAMING_SNAKE_CASE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Variables in python are ____ typed
This means ___?

A

Variables in python are dynamically typed
This means one variable can be assigned values of different types

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

Some of the built-in data types

A
  • int (integer)
    used for whole numbers, negative or positive
  • float (floating-point)
    used for real numbers with decimal points, can be positive or negative
  • bool (boolean)
    stores a true or false value, often used in conditions and logical operations
  • str (string)
    used for text, allowing you to work with strings of characters. can include most numbers, letters, and symbols
  • list (list)
    used to store a collection of items of any type. lists are often ordered and changeable, meaning you can add, remove or modify elements after creating them
  • tuple (tuple)
    similar to a list but immutable, meaning once its created, its elements can not be changed, added, or removed
  • set (set)
    unordered collection of unique elements, meaning it does not allow duplicate values and does not maintain any specific order. supports set operations like unions and intersections
  • dict (dictionary)
    collection of key-value pairs where each key is unique and maps to a corresponding value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the type() function

A
  • We can find the type of an object using the type function
  • The type function takes an object and returns its type

I.e.

Input:
w = true
type(w)
Output:
< class ‘bool’>

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

Define casting
What is implicit casting vs. explicit casting

A

Casting: converting values from one type to another is called casting

  • Casting in python can either be implicit or explicit
  • Implicit casting occurs automatically when some operation is performed by an object
  • Explicit casting requires the programmer to manually use one of the built-in data type functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Some built-in data type functions

A

int()

  • Creates an integer value by converting a float or string (when possible)
  • trunicates the decimal point

float()

  • Creates a floating-point value by converting an integer or string (when possible)

str()

  • Creates a string value by converting a value of another type (supports most other types)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

2 common cases where you need explicit casting

A
  1. You wish to input a numerical value from the user, but the input() function returns a string
  2. You wish to concatenate (add) a numerical value to a string, but strings can not be added to numerical types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define operators
How are they represented?

A
  • Operators are used to perform operations on objects and values
  • Operators are represented as special symbols or keywords that designate some type of computation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Categories of operators

A
  • Assignment operators
  • Arithmetic operators
  • Comparison operators
  • Booelean or logical operators
  • Identity operators
  • Membership operators
  • Concatenation and repetition operators
  • Bitwise Operators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Assisgnment Operator and how it works?

A

The Assignment Operator: used to assign values to variables

  • The value of the expression on the right is evaluated and the result is stored in the variable on the left
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Assignment Operator Example:

Input:
x = 10
y = 5
z = x + y
x = 3
y = 2
z = z +1
print(x, y , z)

What is the output, and explain why?

A

3 2 16

  • x is assigned to 10 then reassigned to 3 so x is 3
  • y is assigned to 5 then reassigned to 2 so y is 2
  • z is assigned to x+y. At the time x was 10 and y was 5 so z is 15, and 15 is stored in z even after x+y change.
  • then z is reassigned to z+1 so z now stores the value 16
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Define arithmetic operators

A

arithmetic operators allow you to perform arithmetic operations on numeric values

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

What are some common arithmetic operators

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

More common arithmetic operators

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

What is the order of operations for arithmetic operators?

A

The order of operations from highest to lowest priority is:
1. Parentheses
2. Exponents
3. Multiplication, Division, and Modulo
4. Addition and Subtraction

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

What are compound operators.
Examples of compound operators.

A

additional Assignment Operators called compound operators provide shorthand for some common assignments we tend to see with variables.

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

What are some built-in math functions?

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

What is a module

A

A python module is a file containing Python code, such as functions, classes, and variables, that can be imported and used in other Python programs

The Python standard library comes with many premade modules that include helpful functions and classes we can use in our programs.

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

How do we import modules? (what keywords do we use)

A

To use a module, we first need to import it into our program using the from and import keywords.

Example:

  • from math import sqrt
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the math module?

What are some common functions in the math module.

A

The math module contains extra math functions that are not included by default. These must be imported before using them.

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

Some more math module functions (2)

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

Some more math module functions (3)

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

What are 4 ways to import modules

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

Define strings and string literals

A

strings are immutable sequences of characters that store text (letters, digits, symbols etc)

  • Immutable means strings can not be changed after they are created

string literals are string values specified by the programmer in the source code of a program

  • String literals are always surrounded by quotation marks
  • Can be single or double quote
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What does immutable mean?

A

can not be changed after its creation

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

Define escape sequences
What is the escape character?

A

escape sequences are used to represent special characters within strings

  • each escape sequence starts with the escape character, a backslash
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Common escape sequences

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

Why is dealing with quotes within strings a common problem and how do we solve it (2 ways)?

A
  • Dealing with quote marks inside strings is a problem because the string is represented by quotation marks.
  • You can use escape sequences to insert quotation marks
  • Also if you use double quote marks for the string then you can have single inside it with no problems and vice versa
    BUT to keep the style consistent you have to use double or single for the entire thing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is the len( ) function and what can we use it for?

A

The len( ) function takes a collection or sequence value and returns the length of that value. For strings, this is the number of characters.

We can find the number of characters in a string using the built-in len( ) function.

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

Explain string indexing

What do we use to represent string indexing?

A

We can think of strings as a continuous set of boxes, one for each character, each with their own index.

We can access the value of a specific character using string indexing.
String indexing uses brackets [ ] after the variable name that contain the index of the character we want

  • is read from left to right
  • starts with index 0 and counts up per character
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Explain negative string indexing

A

like string indexing but is read from right to left (backwards) and the indexing starts at -1

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

Example:

Input:
s = “Hello World”
print(s[0])
print(s[5])
print(s[10])

What is the output?

A

H

d

  • index 0 outputs H
  • index 5 outputs a whitespace character
  • index 10 outputs d
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Example:

Input:
s = “Hello World”
print(s[-1])
print(s[-3])
print(s[-10])

What is the output?

A

d
r
e

  • index -1 is the last character in the string in the string
  • index -3 is the third from last character in string
  • and index 10 is the 10th from last/10th character when you read string from right to left
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Example:

Input:
s = ‘Hello World’
print(s[11])
print(s[-12])
s[0] = ‘Z’

What is the problem/error with each of these?

A
  • print(s[11]) is out of range, there is no index 11
  • print(s[-12]) is out of range, there is no index -12
  • s[0] = ‘Z’ is not possible - not allowed to modify strings
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What are some string operations?

A

several of the arithmetic operations we have seen have a special meaning when used on strings.

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

What are methods?

A
  • Some objects have methods associated with them
  • Methods are a collection of programming instructions to carry out a specific task
  • Methods define the behaviors of objects or the actions we can perform on them
  • Similar to functions but methods can only be applied to objects of a given type
42
Q

What are some common string methods

A
43
Q

What are docstrings
How do we represent them?

A
  • Python provides a second way to document our code in addition to comments (lines that start with a #)
  • Docstrings are a special kind of string literal that can be used to document a module (file), function, method, or class
  • If we don’t assign a string to any variable it acts like a comment and is ignored by Python. Docstrings take advantage of this.
  • Docstrings are surrounded by triple quotes (The double quote character repeated 3 times “””)
44
Q

Can multiple variables point to the same object

A

yes

45
Q

What is an algorithm

A

a sequence of instructions that solves a problem

46
Q

What is computational thinking

A

creating a sequence of instructions to solve a problem (an algorithm) - in the information age, many people believe this will become increasingly important, like mathematical thiking was in the industrial age

47
Q

What is a computer program

A

consists of instructions executing one at a time.

48
Q

What are the basic instruction types (the basic things a computer program consists of)

A
  • input: a program receives data from a file, keyboard, touchscreen, network, etc.
  • process: a program performs computations on that data such as adding two values like x + y
  • output: a program puts that data somewhere, such as a file, screen or network
49
Q

Where does the name variable come from for programming

A

the name variable is to due a variable’s value varrying as a program assigns a variable with new values

50
Q

What is the Python interpreter?
What is an interactive interpreter?

A

The Python interpreter is a computer program that executes code written in Python language

Aninteractive interpreteris a program that allows the user to execute one line of code at a time.

51
Q

What is code? What is a line?

A

code: the text that represents a program
line: a single row of text in the code

52
Q

What symbol does the interactive interpreter use to show it’s ready for input?

A

> > >

53
Q

How do you execute a line of code in the interactive interpreter?

A

Type the code and press Enter.

54
Q

When is the interactive interpreter useful?

A

For simple operations or short programs.

55
Q

Why might a programmer write code in a file instead of using the interactive interpreter?

A

Writing in a file is better for longer programs, as the interpreter can run all lines from top to bottom.

56
Q

What is a statement?

A

A single instruction in a program.

57
Q

What is an expression?

A

Code that returns a value, like wage * hours * weeks.

58
Q

How do you create a variable in Python?

A

By using the = symbol, like salary = wage * hours * weeks.

59
Q

What does the print() function do?

A

It displays values or results.

60
Q

What symbol is used for comments in Python?

A

#

61
Q

are comments optional in python code

A

yes, you dont need comments for your code to run, but they can help a human reader understand the code

62
Q

How to keep output on the same line with print()

A

Use end=’ ‘ inside print()

63
Q

What is the newline escape sequence and its use

A

\n moves output to the next line

64
Q

How to print “1”, “2”, and “3” each on a new line

A

print(‘1\n2\n3’)

65
Q

What is whitespace in programming

A

Includes spaces, tabs, and newlines

66
Q

What does the input() function do

A

It reads input from a user

67
Q

How to store user input as a variable in Python

A

Use variable_name = input()

68
Q

What happens when input() is called

A

The program waits until the user enters text and presses Enter

69
Q

What type of data is returned by input()

A

A string

70
Q

How to prompt the user with input()

A

Add text inside input(“Enter a number: “)

71
Q

What is IDLE?

A

The official Python IDE, included with Python installation

72
Q

What is an IDE?

A

An integrated development environment for writing and debugging code

73
Q

What does a processor do?

A

Executes instructions stored in memory

74
Q

What is memory in a computer?

A

A circuit that stores instructions and data as 0s and 1s in addressed locations

75
Q

What are high-level languages?

A

Programming languages using formulas or algorithms, like FORTRAN and ALGOL

76
Q

How does a processor execute a program?

A

By reading and executing instructions in sequence from memory

77
Q

What are bits?

A

Binary digits (0s and 1s) used to represent data in computers

78
Q

What is a switch in computing?

A

A device that controls electricity flow, with “1” for flow and “0” for no flow

79
Q

What are machine instructions?

A

Program instructions written in 0s and 1s

80
Q

What is assembly language?

A

A human-readable form of machine instructions (e.g., “Mul 97, #9, 98”)

81
Q

What is an assembler?

A

A program that translates assembly language into machine code

82
Q

What is a compiler?

A

A program that translates high-level language code into executable programs

83
Q

What is the base for decimal numbers?
What is the base for binary numbers?
What digits are used in binary numbers?
How is each digit weighted in decimal numbers?
How is each digit weighted in binary numbers?
What limits the range of values a variable can hold?
Why do computers use binary numbers?

A
  • Base 10
  • Base 2
  • 0 and 1
  • By increasing powers of 10
  • By increasing powers of 2
  • The number of bits allocated for it
  • Memory locations are composed of bits (0s and 1s)
84
Q

What is binary? What is a binary number?

A

a base 2 number system (only uses 2 digits: 0 and 1), which is different than the base 10 decimal system we are used to (which uses digits 0-9)

therefore, binary numbers are digits 0 and 1

85
Q

How binary works vs. how decimal system works
what does the number 1011 in binary convert to in decimal? Explain why

A

How Binary Works

In the decimal system, each position (place) in a number represents a power of 10:

  • For example, in the number 123, you have:
    • 1 * 100 (10^2)
    • 2 * 10 (10^1)
    • 3 * 1 (10^0)

In binary, each position represents a power of 2:

  • For example, in the binary number 1011, you have:
    • 1 * 8 (2^3)
    • 0 * 4 (2^2)
    • 1 * 2 (2^1)
    • 1 * 1 (2^0)

So, 1011 in binary equals:

  • (1 * 8) + (0 * 4) + (1 * 2) + (1 * 1) = 8 + 0 + 2 + 1 = 11 in decimal
86
Q

What are input/output devices?

A

Devices like screen, keyboard, mouse, and USB ports

87
Q

What is storage in a computer?

A

Disk or flash storage that holds files and data, non-volatile

88
Q

What is RAM?

A

Temporary memory that holds data for quick access, volatile

89
Q

What is a processor (CPU)?

A

The component that runs programs and executes instructions

90
Q

What is the clock in a processor?

A

A timer that controls the speed of processor instructions

91
Q

How does adding RAM make a computer faster?

A

It allows more data to be kept close to the processor, reducing storage access

92
Q

What is a scripting language?

A

A language that executes programs without needing compilation

93
Q

What is a script?

A

A program executed by an interpreter

94
Q

Why is interpreted execution slower?

A

It requires multiple interpreter instructions for each script instruction

95
Q

Who created Python?

A

Guido van Rossum

96
Q

What did Python 1.0 introduce?

What new feature did Python 2.0 add?

Why isn’t Python 3.0 backward compatible?

A
  • Functional programming constructs
  • Automatic memory management (garbage collection)
  • It has design changes that prevent Python 2.7 programs from running on Python 3
97
Q

What does it mean that Python is open-source?
As of January 2023, how popular is Python?

A

The community helps define the language and create interpreters
It’s the most popular language, with 16.36% popularity

98
Q

Python is a high-level language that excels at creating exceptionally fast-executing programs.
True or False? Explain.

A

False

Python excels at providing powerful programming paradigms such as object-oriented programming and dynamic typing. Python incurs additional overhead costs because the language is interpreted.

99
Q

What is an object?

A

represents a value and is automatically created by the interpreter when executing a line of code. For example, executing x = 4 creates a new object to represent the value 4.

100
Q
A