Chap 8 - programing (python) - conditions, loops, string manipulation, arrays Flashcards
(50 cards)
how do you find input for diff data types in python
str(input(“ “))
int(input(“ “))
float(input(“ “))
2 ways you can print multiple messages on print function
-comma btw words
-concatenation (adding) = join tgt only strings using +
(space in comma, no space in concatenation)
how to comment on code
must be added for each line
do a tic tac toe symbol, like a hash for each line
data types in python
-integer
-string - “ “
-boolean
-float
-character (char) - ‘ ‘
-date
features of data types
-all chars are strings, not all strings are chars
-integer be stored in float because it requires less memory than float. it cannot work the other way around
-integer & float can be stored as string but lose their properties = can’t do mathematical operations on them, string cannot be stored in integer or float
arithmetic operation for
-addition
-subtraction
-multiply
-divide
-power
-squared
-remainder
-rounding
-give random int
- +
- -
- *
- / (float), // (integer)
- **
- **(1/2)
- %
-round.(num, decimal place)
-import random
random ()
difference btw = & ==
= - assignment of variable
== - comparison
logical operators for:
-greater than
-greater than or equal
-smaller than
-smaller than or equal
-equal (comparison)
-not equal (comparison)
- >
- > =
- <
- <=
- ==
- !=
Boolean operators in python
- and
- or
- not
eg. if not hungry:
if, elif, else conditional statement
if ( ):
action
elif ( ):
action
else:
action
nested ifs
if ( ):
if ( ):
note: for loops in python, last value is not included
eg. 0 TO 4 = 4 runs
so always start with 0
for loop
-for i in range (__,__,_if you want__):
-for i in (variable/ list/ str) = (i changed to elements in variable/ list/ str)
-third one changes the steps between the loop
-don’t need to initialize iterating variable at start
(auto counter update)
how does range function work
eg, (0, 5) - no.s - 0, 1, 2, 3, 4 (last value not counted) - total 5
(0, 20, 2) - no.s 0-19, with 2 step in btw (eg, 0, 2, 4…)
(variable/ list/ str) = no.s in that variable, list
while loop
i = 0
while i< 10:
action
i +=1
-initialize iterating variable at start
(manual counter update)
update counter
i = i+1
i +=1
how to do nested for loops
-2 counters for inner and outer loops
-outer loop only loops after inner loop is finished
eg. for j in range (0,3):
for i in range (0,2):
action
loop/if/elif with a list as its range
n= [“A”, “B”]
-for i in n:
-while i in n:
-if variable[i] in n:
note: i changes to elements in list, list can be any data type
index values in list
-all char has an index value
-first element = 0 - positive
-last element = -1 - negative
-space also has an index value
finding length of characters
-len function
len(“hello”)
len(poop)
finding substring of characters
num = [6, 7, 8, 9]
-print(num[ 0 ]) = 6
-print(num[ 0: ]) = 6, 7, 8, 9
-print(num[ 0:2]) = 6, 7 (index after : not included)
-print(num[:2]) = 6, 7 (index after : not included)
-print(num[-1]) = 9
-print(num[i:i+ n ]) = value with i index & n no.s after it
uppercasing all string
“hello”.upper( )
poop.upper( )
lowercasing all string
“hello”.lower( )
poop.lower( )
create a blank 1D array
-reserving memory in ram
-variable = [ None ] * (number of times)