ch. 2: numbers and strings

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/36

flashcard set

Earn XP

Description and Tags

programming w numbers and strings

Last updated 4:58 PM on 9/24/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

37 Terms

1
New cards

variables

a named storage location in a computer program


define a variable by telling the compiler:

  • what name you will use to refer to it

  • initial value

it’s like the parking space in a garage

2
New cards

the assignment statement

  • use assignment operator (=) to place a value into a variable

  • assigning a value to an existing variable replaces the previously stored value

  • the “=” sign is NOT used for comparison


3
New cards

3 primitive data types

  • whole number (7) integer

  • number with a fraction part (7.0) float

  • sequence “Bob” string

string literal are specified by enclosing a sequence of character within a matching pair of either single or double quotes


the data type in Python is associated with the value, not the variable:

cansPerPack = 6 # int

canVolume = 12.0 # float


Python infers the data type of a variable by the value it holds

  • a variable can be assigned different values at different places in a program


4
New cards

naming variables

  1. must start with a letter or the underscore (_) character

  2. cannot use other symbols and spaces are not permitted

  3. separate words with ‘camelCase’, ‘PascalCase’, or ‘snake_case’ notation

  4. don’t use ‘reserved’ Python keywords


5
New cards

snake_case

  • pros: concise when it consists of a few words

  • cons: redundant when it gets longer

push_something_to_first_queue, pop_what, get_whatever…


6
New cards

PascalCase

  • pros: seams neat

GetItem, SetItem, Convert, …

  • cons: barely used


7
New cards

camelCase

  • pros: widely used int he programmer community

  • cons: looks ugly when a few methods are n-worded

push, reserve, beginBuilding, …


8
New cards

constants

a variable whose value should not be changed after it’s assigned an initial value

python will let you change the value of a constant


totalvolume = bottles * 2.0 ← aviod magic numbers, a number in the code that seems arbitrary and has no context/meaning

totalvolume = bottles * BOTTLE_VOLUME

9
New cards

python comments

use comments at the beginning of each program and to clarify details of the code

10
New cards

basic arithmetic operations

+ → addition

- → subtraction

* → multiplication

/ → division

% → modulus operator to get remainder in integer division ( ex. 5%2, result: 1)

** → exponent

// → integer division/floor division (ex. 5//2 III -5//2, results: 2 III -3)

11
New cards

line continuation

the backslash (\)

12
New cards

calling functions

a function is a collection of programming instructions that carry out a particular task

print() function can display information, but there are other function that requires arguments

13
New cards

calling function that return a value

most function return a value, when a function completes its task, it passes a value back to the point where the function was called

14
New cards

type conversion functions

use int() and float() to convert between integer and floating-point values

you lose the fractional part of the floating-point value (no rounding occurs) when you use int()

15
New cards

functions in python libraries: library

a collection of code, written and compiled by someone else, that’s ready for you to ise in your program

16
New cards

standard library

a library that’s considered part of the language and must be included with any Python system

provide standardized solutions for many problems that occur in everyday programming

17
New cards

modules

python’s standard library is organized into__

functions defined in a module other than the built-in module must be explicity imported into your program before they can be used

18
New cards

built-in functions

a small set of functions that are defined as a part of the python language that can be used directly

can be used w/o importing any modules

19
New cards

strings

  • sequence of characters

  • in python, string literals are specified by enclosing a sequence of characters witghin a matching pair of either single or double quotes


20
New cards

string length

number of characters in a string is

a string of length 0, empty string. contains no characters and is written as ““ or ‘‘

21
New cards

string concatenation “+”

concatenation: you can ‘add’ one String onto the end of another

using “+” to concatenate string is an example of operator overloading. the “+” operator performs different functions of variables of different types

22
New cards

string repetition “*”

produce a string that is the result of repeating a string multiple time, also overloaded

ex. In [20]: dashes = “-” * 50

23
New cards

unicode charaters

  • encode every character into an integer number

  • defines over 100,000 characters

  • designed to be able to encode text essentially all written languages


24
New cards

conversion between unicode and character

the ord() function returns the number used to represent a given character

the chr() function returns the character associated with a given code

25
New cards

round function()

round(number, ndigits=None)

round a number to a given precision in decimal digits


return value is an integer if ndigits is omitted or None. otherwise the return value has the same type as the number

26
New cards

methods (in object-oriented programming)

  • a collection of programming instructions ot carry out a specific task

    • like a function but only applied to an object of the type for which is was defined

    • are specific to a type of object

  • apple the upper () method to any string

    • ex.

name = “John Smith

uppercaseName = name.upper() #Sets uppercaseName to “JOHN SMITH”


27
New cards

input

read a user input from the console, input() function


name = input (“Please enter your name: “)

28
New cards

numerical input

the user’s input will always be returned by input() as a string

  • this is wrong:

age = input (“Please enter age: “)

ageNextYear = age +1

  • to use the input as number, convert it using int() or float()

age = int (input(“Please enter age: “))

  • the above is equivalent to doing it two steps (getting the input and then converting it to a number):

ageString = input (“Please enter age:) #String input

age = int(ageString) # Converted to int


29
New cards

print output

using String format() method with positional arguments

String format() with keyword arguments

30
New cards

ways of defining placeholders

can be identified using named indexes { price }, numbered indexes { 0 }, or even empty placeholders { }

31
New cards

formatted output

  • outputting floating point values can look strange

  • to control the output appearance of numeric variables, use formatted output such as:

ln [18]: print (“Price per liter: {:.2f}”.format (price))

Price per liter: 1.22

32
New cards

format specifier

‘:10.2f’

<type> Subcomponent in a format specifier

33
New cards

using python f-string

the string has the f prefix and uses {} to evaluate variables

34
New cards

find(substring)

returns the index of the first occurrence of a substring, or -1 if it is not found.

35
New cards

count(substring)

counts how many times a substring appears in the string

36
New cards

startswith(prefix)

returns True if the string begins with the specified text.

37
New cards

endswith(suffix)

returns True if the string ends with the specified text.