Chap 8 - programing (python) - conditions, loops, string manipulation, arrays Flashcards
commenting on code
this symbol must be added for each line of comment
rules for variables
-has to start with a character
-no special char and spaces - will change program (except underscore)
-case sensitive
Poop & poop are 2 diff variables
note: explain each thing when explaining assignment
eg. c = a + b
- c: variable
- +: addition of a and b
- =: assignment of variable
name 6 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
how do you find input for diff data types
str(input(“ “))
int(input(“ “))
float(input(“ “))
print function
print(“ “)
how to print i in a single line in a loop
print( i, end=” “)
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)
what does \t do
a tab of space
what does \n do
a new line
what must you put when using \t and \n
quotations because they’re strings
arithmetic operation for
-addition
-subtraction
-multiply
-divide
-remainder
-power
-squared
- +
- -
- *
- / (float), // (integer)
- %
- **
- **(1/2)
relational operation for:
-greater than
-greater than or equal
-smaller than
-smaller than or equal
-equal (comparison)
-not equal (comparison)
- >
- > =
- <
- <=
- ==
- !=
difference btw = & ==
= - assignment of variable
== - comparison
logical operators in conditions
- and
- or
- not
eg. if not hungry:
and & or conditional statements
eg. if ( age> 10 and age< 20):
eg. if ( age> 10 or age< 20):
conditional statement to get something in between value
eg. if (0 < username <10):
when checking for discounts, always put bigger no. first
if total > 1000;
discount = total *0.2
elif total > 500:
discount = total *0.1
if, elif, else conditional statement
if ( ):
action
-indentation
elif ( ):
action
-indentation
else:
action
-indentation
nested ifs
if ( ):
if ( ):
-indentation
match case statements
match (variable):
case “something”:
action
case _:
action (only run when none of others match)
-use like if statements
use of loops
use same algorithm for diff. inputs
types of loops
-for (count controlled) - knows how many times to run - doesn’t check condition
-while (pre condition) - checks iterating value before running - use for validation