Python Syntax - Beginner Flashcards

1
Q

Symbol used to render following symbol exactly.

A

\

Ex. To get a quotation mark in a string you would use: (“This guy said"Go" so I went.”)

Called the: escape character.

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

How do you insert a new line into a string?

A

\n

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

How do you concatenate a string?

A

+

Ex.

Print (“dogs” + “ rule”)

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

.upper & .isupper

A

.upper capitalizes a string
.isupper Checks to see if string has caps. (Checks original value, you can print in all caps with .upper without changing original value of variable.

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

Function that counts letters in a string.

A

len

Ex. print(len(variableA))

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

How do you extract a single character from a string?

A

[ index value goes here ]

Ex. String = Me

To get M

print(variableName [0])

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

How do you see where in the index a character is located in a string?

A

print(variable.index(“a”))

If more than one letter in a word, only returns index for first one.

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

How do you replace characters in a string?

A

variable.replace(“string to be replaced”, “new string that will replace it)

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

How does order of operations work in python?

A

Parenthesis work like typical.

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

What is this and what does it do? Print(10 %3)

A

Modulus operator. Divides first number by second number and gives you remember.

Say it 10 mod three, good for finding even numbers.

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

How do you turn a number into a string?

A

Print(str(variable or number))

Allows you to print numbers and strings together, otherwise they do not concatenate.

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

How do you get the absolutely value of a number?

A

abs

Ex.
print(abs(num_variable))

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

What does the function pow(2, 4) do?

A

Raises 2 to the fourth power.

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

How do you find the highest value number in a set?

A

max

Ex. print(max(4,6,2))
Returns 6.

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

What does the min function do?

A

Prints the smallest number.

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

What is the function for rounding a number?

A

round - follows normal rounding rules.

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

How do you import the math library in python?

A

from math import *

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

What does print(floor(3.7) do?

A

Always rounds down

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

What function do you use to always round up?

A

Ceil

Ex.
print(ceil(3.2) returns 4.

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

What is the function to return a square root?

A

Sqrt()

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

How do you allow the user to give the program information?

A

Use function input(prompt for user)

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

What is the default data type for user input in python?

A

String

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

How do you set user input to a number?

A

Add int (variable) or float(variable)

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

Syntax for creating a variable that is a list?

A

variable_name = [value1, “item2”, “item3”

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

Can python store different data types in the same list?

A

Yes.

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

Syntax to call a specific item from list if index value is known?

A

Ex. print(friends[index value])

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

What does a call for a negative index number mean when pulling data from list?

A

Starts counting from back of list. -1 is last item in the list.

28
Q

How do you retrieve all the values after a certain index in a list?

A

List_variable[2:]

Prints the second list value to the end of the list.

Inclusive of value before the colan.

29
Q

What will friends [:2] return?

A

All values before but not including index position 2.

30
Q

How would you return indexed values 3-6 from a list?

A

list_variable[3:7]

31
Q

How do you change the value of a single item in a list?

A

After variable has been defined:

variable_name[index position] = new value

32
Q

What function do you use to combine two lists?

A

list1_name.extend(list2_name)

33
Q

How do you add individual items to a list?

A

variable.append(“string”, integer)

34
Q

What function do you use to add a value to the middle of a list?

A

variable_name.insert(index position it will go in, “value in quotes if a string”)

35
Q

How do you remove a value from a list?

A

variable_name.remove(value to remove)

36
Q

How do you completely clear a list?

A

variable_name.clear()

37
Q

What does list_name.pop() do?

A

Returns and removes the last item on a list. Can also specify an index value to do that with a different part of the list.

38
Q

How do you find the index for a value in a list?

A

list_name.index(value)

39
Q

What does list_value.count(value) do?

A

Returns the number of times a value shows up in a list.

40
Q

What function puts a list in alphabetical or numerical order?

A

.sort

41
Q

How do you flip the order of a list?

A

. reverse

42
Q

What are the four built-in data types in Python?

A

Tuple, List, Set, Dictionary

43
Q

What are the characteristics of the Tuple data type?

A

Is ordered, unchangeable, allows duplicate members.

44
Q

How do you create a Tuple?

A

tuple_name = (value 1, value 2,)

45
Q

Match each bracket type with it’s data type:
()
[]
{}

A

() - Tuple
[] - List
{} - Set or Dictionary

46
Q

Syntax for creating a function in Python

A
def descriptive_name () :
     Indented code is in function
Code to the left is not part of function
47
Q

How do you call a function?

A

function_name ()

48
Q

What Is a parameter?

A

A piece of information we pass into a function.

49
Q

How do you get a function to give you information?

A

return

Ex.

def function_name (num):
       return  num*num*num  (must be the end of the function)

Can put it in a variable

result = function_name (4)
print(result)

50
Q

What is a condition in the context of an if statement?

A

Something that can be true or false:

If I’m hungry.

51
Q

How do you create a basic if statement with boolean values?

A
is_cloudy = True
is_week_day = False

If is_cloudy and is_week_day:
print(“Read a book!”)

else:
print (“Go to work!”)

52
Q

What does elif do in an if statement?

A

It is an Else If command, it is a second or third condition checked before the exit else statement.

53
Q

How do you create a second condition check in an if statement that checks for variable_1 to be false, and variable_2 to be true?

A
elif not(variable_1) and variable_2:
       function
54
Q

What are Comparison operators?

A
== Checks if two values are equal
<= Less than or equal to
>= Grater than or equal to
!= Not equal to
> Grater than
< Less than
55
Q

How do you retrieve a value from a dictionary and specify a default value?

A

Ex. print(dictionaryName.get(“potentialKey”, “default value”)

56
Q

What is a while loop?

A

It is a set of code that runs over and over again untill a certain condition is met.

57
Q

What kind of loop can excite trough an array or a range?

A

For Loop

58
Q

How do you create a For loop?

A

for thing in array_name:
print(thing)

Also don’t forget:

for index in range(10) or range(3, 25)
print(index)

59
Q

What is a try except block good for?

A

Using on things that could break the program like a user entering a letter in stead of an integer.

60
Q

How do you use try/except?

A

try:
Code
except YouCanPutAErrorTypeHere:
Code

61
Q

What is the danger of an broad except clause?

A

Will exicute the same way with any error in the try block of code. Use caution.

62
Q

How do you get a try/except statement to print the error the code throws?

A

try:
Code

except ErrorTypr as err: 
#stores the error type in variable err
    print(err)
63
Q

What modes can you open a file in in python?

A

Read - “r”
Write - “w”
Append - “a” # add info to end of file.
Read/Write - “r+”

64
Q

Syntax for opening a file in python:

A

Variable_tostorinfo = open(“filename&path.txt”, “mode” - ex. r for read)

Variable_tostorinfo.close()

65
Q

What is pip?

A

A program/ installation utility that is usually pre-installed with python these days, that allows you to install modules easily. Is a package manager.

66
Q

What, broadly and conceptually is a Class in python?

A

A home made data type. I.e. how would you represent a phone or a person?

67
Q

What, broadly and conceptually is an object in python?

A

An object is a particular instance of a class. For example the class might be Librarian and the object Megan.