Statements in Python Flashcards
Variable
- as you write programs you will make up your own words that have meaning to you called VARIABLES
- you can use any name as a variable in python EXCEPT FOR THE RESERVED WORDS
- we use this term Variable to refer to the labels we use to refer to this stored data
- in algebra and symbolic logic- a variable is some unknown that we want to use in either statement, equation, etc.
- (within reason) we can create as many variables as we want
- sometimes we create them because the code needs them. Other times we create them to help us understand (ex. Circumference is d*Pi, but d can also be thought of as 2r)
How are python variables similar to variables in algebra?
-just like the familiar variables x and y in mathematics, we use variables in programming to easily manipulate values
-ex.
»> x = 2
»>print(x)
2
(We call the way we used the = sign an assignment operator)
What ranges of values do integers have?
Negative really big to positive really big
What ranges of values do bools have?
True and false
What ranges of values do floats have?
Negative really big to positive really big
What ranges of values do strings have?
A really big set of value
How do we differentiate between a float and an integer value?
- floats have decimals
- integers are whole numbers
But we can check by using isinstance()
Isinstance(object, classinfo)
Ex.
number=25.9
»>Check_int = isinstance(25.9, int)
»>Print(check_int)
False
How can we specify a string value?
- put the sequence of characters inside either single or double quotes
- from there you can assign it to a variable
Ex. >>>single_quote_character = ‘a’ >>>print(single_quote_character) a >>>print(type(single_quote_character))
The type function
-the type() method returns class type of the argument(object) passed as a parameter
-mostly used for debugging purposes
-Type(object)
-ex.
»>print(type([]) is list)
True
Operators
Are special symbols that represent computations
Which operators make sense for integers?
\+ - * ** / // %
Integer Division Operator
-it always truncates it’s result down to the next smallest integer (to the left on the number line)
-ex.
If we use print(7 / 4)
We would just get the answer as it is. Which is 1.75.
But with the integer division operator,
Print(7 // 4)
It gives us the answer but just that whole number. It doesn’t matter if it was over .5 and could be rounded. It’s at the number it’s at. So our answer for this one would just be 1
Modulus Operator
-sometimes also called the remainder operator or integer remainder operator
-it works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second
-in Python, the modulus operator is a percent sign (%)
-ex.
quotient = 7 // 3
With this we would get 2.
But if we did
7 % 3
It gives us the remainder. 6\3 is 2. But with 7, that’s a whole number not accounted for. So we get 1 for the answer
Ex. 32 % 9
We would get 5. Because 9 times 3 is 27. 32-27 = 5. It gives us the remainder
Can you use the same operators with integers and floats?
Yes
Logical operators you can use in bools
-these operators are and, or, and not