comm 190 - week 9 python

0.0(0)
Studied by 1 person
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/38

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 8:05 PM on 4/1/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

39 Terms

1
New cards

what is python

  • high level programming language

  • easy to write and read (simple syntax for beginners)

  • used in web development, data science, ai, automation etc

2
New cards

What do variables do

  • used to store, manipulate and display info within a program

  • memory unit that can be accessed by typing the variable name

  • name cannot start with a number

variable (contains) value

ex: variable_name = value

ex: age = 16

3
New cards

int

integer, whole numbers

ex: 2

4
New cards

float

real numbers. (a decimal turns integer into a float)

ex: 1.3

5
New cards

str

string

  • consists of multiple chars (single characters)

  • str ( “variable” ) turns it into a sentence, displaying the variable as text not math

6
New cards

bool

boolean

  • only true/false

7
New cards

basic operations

arithmetic : + - & / %

comparison == != >. < >= <=

logical: and, or, not

8
New cards

conditional statements example

variable = value

if value>= 18:

  • print (“ you are an adult)

else:

  • print (“you are a minor”)

** dont forget colons and quotes

9
New cards

if statements

if condition:

  • statement1

  • statement 2

statement 3

if condition is valid, all indented lines will run. if not valid then execution will be skipped and continue after indent

10
New cards

nested blocks

blocks within other blocks

if x> 5

  • print (“x is greater than 5”)

  • if x>10

    • print (“x is also greater than 10”)

11
New cards

operators

= = does this equate? check/compare (boolean expression to evaluate if its true)

= this is equal to ___ (assignment)

! = is not equal to (returns the opposite of = = )

12
New cards

logical operators

and, &

  • when both A and B are true

or, l

  • either A is true or B is true

not, !

  • Not A (opposite of A) is true if A is false

13
New cards

else clauses

if condition:

  • codecodecode1

else:

  • codecodecode2

** anything besides the condition. execution moves to else given condition1 is false

14
New cards

Elif

Else- if

  • if none of the previous conditions are true. goes thru elif 1, elif 2, elif 3,… then ELSE:

  • used when there are multiple related conditions you when to check for and execute different code depending on which one is true

15
New cards

When would you assign None to a variable

when the value hasnt been decided yet. not the same as 0 zero

  • no input yet

16
New cards

while loop

  • called indefinite loops bc used to repeat code without knowing how many times it will be repeated before hand

  • while a condition is true, it will keep running. when its false, it stops

  • need to make sure condition is checked before the block of code is executed

  • need to make sure that the condition will eventually become false or loop with be infinite and never finish

17
New cards

what is a loop

  • allows programmer to execute certain sections of code multiple times

  • makes certain programs more simple

18
New cards

for loop

  • called definite loops bc they repeat a set number of times

19
New cards

while loop example

num = 1

while num <5

  • print (num)

  • num = num + 1

  • #### this is a counter that adds one then reloops. will continue while num<5 is true

print (“finished”)

20
New cards

how do comments work

  • not relayed in the code. just for reading

#single line comment

“ “ “ this is for

multiline

comments

“ “ “

21
New cards

for loop example

for x in range (5)

  • print (x)

will run 5x. starting from 0

22
New cards

what number does code/computer start counting from

zero

23
New cards

nesting loops

where a loop contains another loop (more if structures) in itself

for outer_variable in outer_iterable:
    for inner_variable in inner_iterable:
        <body>

24
New cards

what does print (“ “ ) mean

print("") inside a for loop, outputs a single empty line during each iteration

for i in range(2):

print("Iteration", i)

print("") # This prints an empty line

print("Continuing...")


output——

Iteration 0

Continuing...

Iteration 1

Continuing...

25
New cards

what does end = mean

  • used to get output on the same like

  • can do end= “ - “ or to end= “ “ get dashes or spaces between # on same line

  • by default, a print statment goes to the next line so an end = prevents the new line

26
New cards

nested for loop with end= example

knowt flashcard image
27
New cards

how to convert/capitalize word to title

variable.title()

  • All First Letters Are Capitalized

variable.capitalize()

  • Only first word (first letter) is capitalized

28
New cards

how to remove leading/trailing spaces

both sides

  • variable.strip()

remove whitespace only from the left

  • variable.lstrip()

only right

  • varibale.rstrip()

29
New cards

how to remove whitespace in multiple lines

word = “ ___hello world.___ “

cleaned_word = word.strip()

output: “hello world”

30
New cards

Modulo operator

%

  • tells you whats left over after dividing one number by another REMAINDER

  • result = 10%3

  • result = 1

31
New cards

what do you use a modulo operator for?

checking if # is even

= number%2

if x%2 == 0 ** then even

check if # is odd

= number%2

if x%2 == 1 ** then odd

if the dividend is smaller than the divisor: result is just the dividend

ex: 5%8 = 5

32
New cards

what is a range

this_is_a_range= range(3)

  • a sequence of numbers

  • often used in loops

  • start at 0

33
New cards

how to check what type is a variable (weather it is ___ or not)

age=10

print(isinstance(age, float))

: False

** here shows that age is not a float, it is an integer (whole #)

34
New cards

how to check the class type

is_sunny= True

print(is_sunny, type(is_sunny)

: <class str>

35
New cards

what data types are immutable

immutable - cant be modified or altered once declared

  • can reassign them (will be most updated assignment)

  • ex: string, integer, float, boolean, range

36
New cards

how to combine multiple strings together

  • aka string concatenation

  • add things together using + operator

  • need to convert all data to strings. can add int to string

    • print(name+str(age))

37
New cards

convert all characters to uppercase

.upper()

variable= “hello”

print(variable.upper())

  • HELLO

38
New cards

convert all characters to lowercase

.lower()

variable=”Hello World”

print(variable.lower())

  • hello world

39
New cards

count substring

count(substring)

  • returns # of times a substring appears in a string

word = 'hello world'

o_count = word.count('o')

print(o_count)

output —— 2

Explore top notes

note
Chapter 4: Forces and Energy
Updated 1218d ago
0.0(0)
note
Unit 8: Ecology
Updated 95d ago
0.0(0)
note
Day 3
Updated 499d ago
0.0(0)
note
Chapter 13: Illicit Drugs
Updated 1092d ago
0.0(0)
note
excretory system notes
Updated 1176d ago
0.0(0)
note
Chapter 4: Forces and Energy
Updated 1218d ago
0.0(0)
note
Unit 8: Ecology
Updated 95d ago
0.0(0)
note
Day 3
Updated 499d ago
0.0(0)
note
Chapter 13: Illicit Drugs
Updated 1092d ago
0.0(0)
note
excretory system notes
Updated 1176d ago
0.0(0)

Explore top flashcards

flashcards
Flashcards pro zeměpis
80
Updated 316d ago
0.0(0)
flashcards
VET213 LAB END PRACTICAL SP2023
33
Updated 1064d ago
0.0(0)
flashcards
Criminology Unit 3
20
Updated 1234d ago
0.0(0)
flashcards
Econ Exam 2
66
Updated 1121d ago
0.0(0)
flashcards
W27 vocab (Klinh + Btram)
45
Updated 418d ago
0.0(0)
flashcards
El tiempo/ Que tiempo hace?
21
Updated 1204d ago
0.0(0)
flashcards
confirmation
32
Updated 980d ago
0.0(0)
flashcards
Speedtest Fische
25
Updated 62d ago
0.0(0)
flashcards
Flashcards pro zeměpis
80
Updated 316d ago
0.0(0)
flashcards
VET213 LAB END PRACTICAL SP2023
33
Updated 1064d ago
0.0(0)
flashcards
Criminology Unit 3
20
Updated 1234d ago
0.0(0)
flashcards
Econ Exam 2
66
Updated 1121d ago
0.0(0)
flashcards
W27 vocab (Klinh + Btram)
45
Updated 418d ago
0.0(0)
flashcards
El tiempo/ Que tiempo hace?
21
Updated 1204d ago
0.0(0)
flashcards
confirmation
32
Updated 980d ago
0.0(0)
flashcards
Speedtest Fische
25
Updated 62d ago
0.0(0)