Statements in Python Flashcards

1
Q

Variable

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are python variables similar to variables in algebra?

A

-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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What ranges of values do integers have?

A

Negative really big to positive really big

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What ranges of values do bools have?

A

True and false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What ranges of values do floats have?

A

Negative really big to positive really big

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What ranges of values do strings have?

A

A really big set of value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we differentiate between a float and an integer value?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we specify a string value?

A
  • 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))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The type function

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Operators

A

Are special symbols that represent computations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which operators make sense for integers?

A
\+
-
*
**
/
// 
%
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Integer Division Operator

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Modulus Operator

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you use the same operators with integers and floats?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Logical operators you can use in bools

A

-these operators are and, or, and not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Operators for lists

A
  • append(object)
  • index(object, start, end)
  • count(object)
  • reverse()
  • clear()
  • copy()
  • extend(iterable)
  • insert(index, object)

And more?? I guess idk there’s a lot

17
Q

Operators for strings

A
Assignment operator: =
Concatenate operator: +
String repetition operator: *
String slicing operator: []
String comparison operator: “==“ & “!=“
Membership operator: “in” & “not in”
Escape sequence operator: \
String formatting operator: “%” & “{}”
18
Q

Assignment operator

A

-“=“

Ex.
String1 = “hello”

19
Q

Concatenate Operator

A

“+”

Ex. 
>>>string1 = “hello”
>>>string2 = “world “
>>>string_combined= string1+string2
>>>print(string_combined)
helloworld
20
Q

String repetition operator

A


-the same string can be repeated in python by n times using string
n

Ex.
»>string1 = “helloworld”
»>print(string1*2)
helloworld helloworld

21
Q

String slicing operator

A

“[]”

  • characters from a specific index of the string can be accessed with the string[index] operator
  • the index is interpreted as a positive index starting from 0 from the left side and negative index starting from -1 from the right side

Ex.
»>string1 = “helloworld”
»>print(string1[1])
e

H e l l o w o r l d
0 1 2 3 4

22
Q

String comparison operator

A

“==“ and “!=“

“==“ operator returns Boolean True is two strings are the same and return Boolean False is two strings are not the same

“!=“ operator returns Boolean True if two strings are NOT the same and returns Boolean False if two strings are the same

-these operators are mainly used along with if condition to compare two strings where the decision is to be taken based on string comparison

23
Q

Membership operator

A

“in” and “not in”

  • membership operator is used for searching whether the specific character is a part of/member of a given input python string
  • they are also useful to find whether specific substring is part of a given string
  • “a” in string: returns Boolean true if “a” is in the string
  • “a” not in string: returns Boolean true is “a” is NOT in the string

Ex.
»>string1 = “helloworld”
»>print(“w” in string1)
True

24
Q

Escape sequence operator

A

“\”

  • to insert a non-allowed character in the given input string, an escaped character is used
  • an escape character is “\” or “backlash” operator followed by a non-allowed character
  • an example of a non-allowed character in python string is inserting double quotes in the string surrounded by double quotes
Ex.
Non allowed double quotes in python:
>>>string = “Hello world I am from “India””
>>>print(string)
You would get a syntax error 

Now if you want to make this work use escape sequence operator
»>string = “Hello world I am from \”India\””
»>print(string)
Hello world I am from “India”

25
Q

Functions

A

-a named sequence of statements inside a function definition

Pg. 47, 53

26
Q

Function Definition

A
  • A statement that creates a new function, specifying its name, parameters, and the statements it executes
  • once we define a function, we can reuse the function over and over throughout our program
27
Q

Function call

A

A statement that executes a function. It consists of the function name followed by an argument list

28
Q

Function object

A

A value created by a function definition. The name of the function is a variable that refers to a function object

29
Q

How do functions relate to math?

A
  • they (optionally) take an input value (or multiple values) and return something
  • these inputs (or parameters or arguments) often need to be of a specific type
  • what they return is called a return value. Values must have types
30
Q

<>

A

-if values of two operands are not equal, then condition becomes true
-ex.
(a <> b) is true. This is similar to the != operator

31
Q

Do equality and inequality operators only work for bools??!

A

NOOO

they work for strings too!

32
Q

Ternary Operator

A
  • is a way of writing conditional statements in Python
  • there are three operands in a ternary operator including:

Condition: a Boolean expression that evaluates to either true or false
True_val: a value to be assigned if the expression is evaluated true
False_val: a value to be assigned if the expression is evaluated false

Ex.
>>>is_nice = True
>>>state = “nice” if is_nice else “not nice”
>>>print(state)
nice