Python Basics Flashcards

1
Q

what are some of the data types in Python?

A

int
str
float
set
list
tuple
dict
bool

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

what does “ * “ represent in python?

A

It is a symbol for multiplication

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

What does the “ / “ symbol represent in Python?

A

It is a symbol of division

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

What does the “ ** “ symbol represent in Python?

A

It is a symbol that means “ Raised to the power of”

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

What does this “ // “ symbol mean in Python?

A

It means divide and round it to the nearest integer.

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

What does the “ % “ symbol mean in Python?

A

The symbol is called a Modulo. The symbol returns a remainder value.

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

What are built-in functions?

A

Functions are “Actions” we (the user) can perform based on our preference.

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

What are some examples of Math functions?

A

“round” & “abs”

print(round(2.9))
Answer: 3

print(round(99.5)
Answer: 100

print(round(99.4))
Answer: 99

abs, returns the absolute value. Used for solving complex math.

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

What is operator precedence?

A

Precedence in general means “take priority over”..
In Python “operator precedence” means the rule of “PEMDAS”.

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

Variables

A

Store information that can be used in our programs.
It is like a container of information.

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

example of good and bad variables.

A

good variables: lower case, no numeric value in the beginning, not using all caps, not using names of built in functions.

bad variables: the opposite of good variables.

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

what are expressions and statements?

A

expressions are on the right side of the variables.
statements are the entire LINE of code. Just the line not the whole code with multiple lines.

Expression:
- Represents something
- python evaluates it
- results in a value
- Example: 5.6
- (5/3) +2.9

Statements:
- Does something
- python executes it
- results in an action
Example:
print(“hello user”)
import
return

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

Augmented Assignment Operator

A

it is a shorthand way to do math.use the math variable to add, sub, M, D, …

Example:

x = 5
x += 2
print(x) [Answer: 7]

x = x +2
print(x [Answer: 7]

the rule of PEMDAS can be used in AAO if needed.

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

String/str

A

It is a piece of text. It is written in double quotation marks

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

String Concatenation

A

Adding strings together. For example: (“hello”) + (“world”)

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

Type conversion

A

It is a process of converting a data type to another data type.

Example:
x = 100
x = str(100)
print(type(x))

Answer: str

We can do this conversion with int, float, str, etc.

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

Escape sequence

A

It is a way to add quotation marks in a string.

Example: “It’s always sunny”
Example: \n (#new line)
\t (#tab)

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

What are formatted strings?

A

its a function, invoked by (f {})

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

What is string indexes?

A

String index is 0 based, the next index is 1, and so on…

name = “Shehbaaz”
#comment: “01234567” is the string index for the variable (name)

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

What are some of the rules for using string index?

A

working string index requires the help of [] brackets.
[startpoint : endpoint]
[0:what ever the end is]

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

what is an example of string index?

A

“01234567”

Example1:
name = “Shehbaaz”
“01234567”
print(name[0:2])
#start on 0/S and at 2/e
answer: Sh

Example2:
print(name[0:7])
answer: Shehbaa

Example3: Stepover 1 time
print(name[0:8:1])
Answer: Shehbaaz

Example: 4 (0:8:2) means step over 2 times)
print(name[0:8:2])
answer: Seba

Example5: (negative sign and number will start counting from backward)
print(name[-1])
Answer: z

Example 6:
# [start:stop:stepover]
print(name[: :-1])
Answer: zaabhehS

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

What is Slicing?

A

It is used in string or list index, uses the rule of [start:stop] OR [start:stop:stepover]

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

What is immutability

A

Immutability means ‘Cannot Be Changed’

In python immutability means something that cannot be changed. string and tuple are unchangeable, once created, it exists in that form. It cannot be changed.

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

What are built in functions?

A

functions are actions we can perform.
so far we learned built in functions like abs() and round() for int.
There is len() for strings (len is short for length). #length starts count from 1. count from 0 is only in index.

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

What are built in methods?

A

It is similar to functions.
Used for strings only
it is invoked by a dot or a period .

Example:
.format() (#it formats strings)
.upper() (#capitalizes whole string)
.capitalize (# capitalizes the first alphabet only)

26
Q

what is list? give example.

A

to use list we invoke the [ ].
List are like a versatile container that allows us to store multiple data/items in a single variable.

In list we can store datatypes like, strings, integers, float, bool, etc.

Note: lists uses the rule of index. the counting starts from 0.

Example:
items = [“shoes”, “shirt”, “ball”, “bat”, “phone”]

print(items[0])
Answer: “shoes”

print(items[3])
Answer: bat

27
Q

What is a datastructure?

A

Datastructure are containers for different kinds of data.

There are 4 datastructures in Python.
1. List (#currently learning)
2. Set
3. Tuple
4. Dictionary

28
Q

What is list Slicing?

A
  • It is a process of excessing a part of data from a list.
  • They are similar to string slicing.
  • They are mutable (changeable)
29
Q

what is matrix?

A

Matrix is a list within a list.

For example: [[ ]]
#Note: our variable can be named anything, matrix is our variable here it doesn’t invoke any function or method.

Example2:
matrix= [
[1,2,3],
[2,4,6],
[7,8,9],
]

print(matrix[0] [1])
Answer: 2

print(matrix[1] [2])
Answer: 6

30
Q

What are list methods?

A

list methods are like functions/actions that are built into Python.

31
Q

What are some types of list methods?

A

.append (adds in the end of the list)
.insert (inserts data in the list based on our preference)
.extend (extends the existing list)
.pop (removes the item in the index / list that is in the end)
.remove (removes the value we want to get rid of)
.clear (clears/deletes whole list)
.index (specifies which index the value is in)
.count (counts the value in the list)

32
Q

Dictionary

A

Dictionary is an unordered “key”: “value” pair

  • Why unordered? Because when working with a large set of data, the key value structure is scattered in a disorderly fashion.

99% of the time keys are in the form of strings
keys have to be immutable (unchangeable)
keys can be strings, integers, bool.
They cannot be list, because lists are mutable

Values can hold any datatypes (int, float, bool, strings)

unlike a list, where we have the list structure in order.

33
Q

what are some Examples of the dictionary methods?

A

because in example 1, age was not defined in the key.

dict
# creates a dictionary for us.
It is not a common way of creating dictionaries, but its good for us to know about it.
dict example:
user2 = dict(name=”Bonbon”)
print(user2)
Answer: {‘name’: Bonbon}

.get

Example 1:

user= {
“basket”: [1,2,3],
“greet”: “hello”
}

print(user.get(“age”))
Answer: None

Example 2:

user= {
“basket”: [1,2,3],
“greet”: “hello”
“age”: 20
}
print(user.get(age,55))
Answer: 20
# our print .get function here prints out 20 because it was defined in the dictionary key. If there was no value in age, then it would print 55.
If neither the key was not mentioned then it would print out None.

34
Q

What are some types of dictionary methods?

A

dict #it is a function

.get # return the value of a dictionary entry for a specified key

.key # return a new view object that contains a list of all the keys in the dictionary #

.value# returns a new view object that contains a list of all the values associated with the keys in the dictionary #

.items # returns each item in a dictionary as tuples in a list#

.clear # erases all the elements present inside the dictionary#

.copy # returns a copy of the dictionary#

.pop # removes and returns an element from a dictionary#

.update # updates the specific items into the dictionary#

35
Q

what is a tuple?

How many methods does a tuple have?

A

It is a data structure.
it is invoked by () curve brackets

  • A tuple is similar to a list. But it is immutable/unchangeable.
    lists are changeable. Tuples are not.

A tuple has 2 methods.
.index # returns the index value of the first occurrence of the specified element in the tuple#

.count # returns the number of times a specified value appears in the tuple

len() # returns the number of elements in the tuple.

36
Q

What is a set?

What are some of the set methods?

A

It is another type of data structure.
it is invoked by {}
it is like a list, but it removes any or all duplicates.

set methods:

.difference #this method returns a set that contains the difference between two sets#

.discard # removes a specified element from a set#

.intersection # returns a set that contains te similarity between two or more sets

.isdisjoint # returns True if none of the items are present in both sets, otherwise returns False#

.issubset. #returns True if all items in the set exists in the specified set, otherwise, it returns False.

.issuperset # helps find out if a set is a superset or another set. ## A superset is a set that contains one or more subsets.

.union # returns a set that contains all items from the original set, and all items from the specified sets. ## we can specify as many sets we want.. separated by commas.

37
Q

What is conditional logic?

A

runs based on conditions:
if (primary condition)
# if statements can be used only once in a block of code.

elif (secondary condition)
# We can have as many elif conditions as we prefer.

else (when both if and elif fails, else runs its magic).
# else statements can be used only once in a block of code.

and # is a keyword. It joins two variables when running the #if# condition.

38
Q

What is Truthy & Falsy?

A

the number zero 0, empty strings, empty lists, tuple, and set are evaluated as false or falsy

non-zero numbers, non-empty strings, lists, set, tuple are evaluated as true or truthy.

39
Q

What is stack overflow?

A

stack overflow is a community for programmers to ask and answer questions.

The higher the mark, the more reliable the answer. people rate the answers, which makes it more reliable.

40
Q

What is Ternary Operator?

A

Practice this code as written here, it will make more sense when I use different approaches in enhancing the code.

The word ternary means composed of three items.

Ternary operator is used to shorten the code when needed to write if - else blocks.

example:

is_friend = True #or False

can_message = “message is allowed” if is_friend else “not allowed to message”

print(can_message)

Answer: message is allowed

41
Q

What is short circuiting?

watch video and practice again##

A

When the evaluation of a logical expression stops because the overall value is already known, it is called short-circuiting.

we can use (and or) key words for short-circuiting.

example:

if_friend = True
is_user = True

if is_friend and is_user:
print(“best friends forever”)

if is_friend or is_user:
print(“best friends forever”)

42
Q

What is logical operators?

A

We have learned about logical operators.
Some examples include:
and
or
>
<

== (# is equal to or is same as) # (single equal to sign = is used to assign data to a variable)

=<
>=
!= (# not equal to sign)

not (# it is a python key word that gives the opposite answer)
example:
print(not(1==1)
Answer: False

43
Q

what is # for #loops?

A

it is a control flow statement that is used to repeatedly execute a group of statements as long as the condition is satisfied.

In the programming world, it is also known as iterative statement
# The WORD iterative means doing something again and again.#

44
Q

is vs ==?

A

== sign means is same as or equal to.

is, it is a key word that verifies the same memory location of data.

a list or array are always located in different memory location, because list are data structure. The #is# rule is implied for sets, tuples, and dictionaries.

45
Q

what are iterable and iterate?

A

Iterable: an object which can be looped over with the help of a for loop
include lists, dictionary, set, tuple, and strings.

Iterate: one by one checks each item in the collection.

46
Q

for loops for dictionary?

A

printing a dictionary gives the result of just the “keys”

to get the key and values printed we must use the #.item:# method

to print just the values we use the # .values# method

to print the keys use the # .keys# method

when we just use the variable, the for loop for dictionary gives the result of only the keys values.

47
Q

What is range?

A

It is a built-in function in Python
useful in #for# loops.
using range in for loop gives the whole range of number. for example:

Example 1:
for number in range(0,100):
print(number)

Answer: 0,1,2,3,4…….99
#start at 0, end at 99 (don’t include 100)

Example 2:
for number in range(0,10,2):
print(number)
Answer: 0,2,4,6,8
#step over

Example 3:
for number in range(10,0,-1):
print(number)

Answer: 10.9.8.7.6.5.4.3.2.1
#reverse order
# to step over, I can do (10,0,-2)
# Answer would be (10,8,6,4,2)

48
Q

What is enumerate?

A

run this code to see how it looks in the terminal.

The meaning of the word is to “mention something one by one”

  • it is a key word in python.
  • it helps to count each object of a list, tuple, set, or any data structure that can be iterated.

Example 1:
for index, character in enumerate(“Hellooooo”)
print(index,character)

Answer:
0 H
1 e
2 l
3 l
4 o
5 o
6 o
…..

Example: 2
for index, character in enumerate([1,2,3,4,5]):
print(index, character)
Answer:
0 1
1 2
2 3
4 5 # index on the left, items from the list is on the right#

Example 3:

write a code to know where the index of 50 is in a range list of 100?

for i,char in enumerate(list(range(100))):
if char == 50:
print(f”index of 50 is: {i}”)
Answer: index of 50 is 50

49
Q

what is #while# loop? And when should we use it?

A

While loop is a control flow statement which repeatedly executes a block of code until the condition is satisfied.

While loop is used to repeat a specific block of code an unknown number of times until a condition is met.

50
Q

What is the trtick to knowing which loop to use?

A

when we know how many times we are going to be looping over something, we should use ##for loop

When we are uncertain about how many times we are going to be looping, or we want to loop over until a condition is met at random,,, we should use ##while loop

51
Q

what is break, continue, pass?

A

all these actions are built in functions in python.

break: stops the loop

continue: continues the loop to he next line of code

pass: doesn’t do anything except to pass over the next line.
pass is useful when we have an unfinished line of code, then we can just write pass and carry on with our code.
When we run the code, it doesn’t give a syntax error. Thanks to the power of pass

52
Q

what is def Function?

A

it is used to define a function.

Once we create a #def function, we can use it over and over again by invoking the function.
This helps coders to keep their codes clean and not re-write them over and over again.

53
Q

What are parameters and arguments in a def function?

A

A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called or invoked.

Look at it this way, parameter is step 1, which creates input.
the argument is step 2 which gives the result

54
Q

what are default parameters and keyword arguments?

A

keyword arguments are values we give to the function after the function has been created.
It is recommended to use the order that the function was written in, but keyword arguments can be used incase the coder wants to add some new value.

Default parameters is the parameters we can set as default.
So if we invoke the function and not define it, it will run the default function that was set in the beginning.

55
Q

What is the keyword #return?

A

The return keyword is used inside a function or method to send the functions result back to the coder.

56
Q

What is the difference between print and return

A

Print is to display the final output of a code

Return returns a final value of a function execution which may be used further in the code

57
Q

What are the 2 conditions to keep in mind when using def functions?

A

There is no right or wrong way to do this, but it is generally a good rule of thumb to keep the below points in mind:

A function should do one thing really well

A function should return something

58
Q

What is the difference between methods vs functions?

A

A function is a set of instructions or procedures to perform a specific task, and a method is a set of instructions that are associated with an object.

59
Q

What are Docstrings?

A

Docstrings are used to write comments in functions.

The common way of writing comments is using a #

However, writing comments inside a function we use:

’’’
bla bla bla bla bla bla
‘’’

Example:

def name(first,last):

’’’
This code is to determine the first and last names of the individuals.
‘’’

now to check the above comment, we use the
help(name)
It will print out the comment in the function

60
Q

What are args and kwargs?

A

The args stands for arguments that are passed to the function whereas kwargs stands for keyword arguments that are passed along with the values into the function.

Example:

def super_func(*args, **kwargs):
total = 0

for items in kwargs.values():
total += items
return sum(args) + total

print(super_func(1,2,3,4,5, num1=5, num2 =10)

61
Q

What is walrus operator?

A

Walrus operator # := # assigns value to variables as part of a larger expression.

Example 1a: #this example is without the walrus expression. We will use Walrus operator in example 1b to show the distinction.

a = “Helloooooooooo”

if (len(a)) > 10:
print(f”too long {len(a)} elements “)

Answer: too long 14 elements

Example 1b:

a = “Helloooooooooo”

if ((n := len(a)) > 10:

print(f”too long {n} elements”)

Answer: too long 14 elements

62
Q
A