Chap 8 - programing python - print, if/elif/else, (nested) loops, len, string manipulation, arrays Flashcards
name 6 data types
-integer
-string
-boolean
-float
-character (char)
-date
how do you use the print function
print(“ “)
2 ways you can print messages on print function
-comma
-concatenation (adding)
meaning of concatenation
join together
how do you use comma when printing
use comma between words
how do you use concatenation when printing
use ( + ) to add the strings
what is the difference between using comma and concatenation
space in comma, no space in concatenation
can you add strings
yes
can you add integers
no
what must you put when using \t and \n
quotations because they’re strings
what does \t do
a tab of space
what does \n do
a new line
what does = do
assignment of variable
what does != mean
not equal
3 rules for variables
-has to start with a char
-case sensitive
-no special char and spaces - willc hange program (except underscore)
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 (Equality (eg, 2 obj, same 2 content))
-not equal (Equality (eg, 2 obj, same 2 content))
- >
- > =
- <
- <=
- ==
- !=
logical operators
- and
- or
- not
eg. if not hungry:
note: explain each thing when explaining assignment
eg. c = a + b
- c: variable
- +: addition of a and b
- =: assignment of variable
equality vs identity vs memebership
equality - checks if obj has same contents (2 obj, same 2 content)
identity - check if obj are the same by refer back to same memory (2 obj, 1 content)
membership - check if value exists in a sequence
what does = & == do
= - assignment of variables
== - comparison
how do you find input for diff data types
str(input(“ “))
int(input(“ “))
float(input(“ “))
1)can integer be stored in float, why
2)can float be stored in integer, why
1)yes, integer requires less memory than float
2)no, float requires more memory than integer
what is the drawback of integer and float stored as string
they’ll be used as a string - lose their properties (can’t do mathematical operations on them)
1)can integer and float be stored in string
2)can a string be an integer/ float
1)yes
2)no
how do you use if, elif, else conditional statement
if ( ):
, start here
-indentation
elif ( ):
, start here
-indentation
else:
, start here
-indentation
how do you use nested ifs
if ( ):
if ( ):
-indentation
how do you use and & or conditions in conditional statements
eg. if ( age> 10 and age< 20):
eg. if ( age> 10 or age< 20):
how do you use conditional statement to get something in between value
eg. if (0 < username <10):
nota: % - remainder, // - integer, / - float (for division)
note: all chars are strings, not all strings are chars
Iteration
looping
why are looped required
use same algorithm for diff. inputs
types of loops
-for
-while
basic structure of loops
-initializer- variable which controls if loop is done or not
-action
-stopping condition
-updating counter/ no. loops
how to write for loop
-for i in range (__,__,_if you want__):
-for i in (variable/ list/ str)
-third one changes the steps between the loop
-don’t need to write iterating variable at start
(i changed to elements in variable/ list/ str)
(auto counter update)
how to update counter
i = i+1
i +=1
how to write while loop
i = 0
while (____):
i +=1
(manuel counter update)