Learn Python Quickly Flashcards

learn python 3.8 quickly

1
Q

exit()

A

exited with the quit command or depending on the version you are running quit()

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

help()

A

all the various commands

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

> > >

A

The primary prompt. Python is waiting for a command when you see this.

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

A

Python is waiting for your input, but this means it’s the second line of input

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

IDE

A

Most programming done in the IDE (integrated development environment). They auto complete commands, phrases, and functions, track bugs, highlight syntax errors and overall make programming and seeing the output of your programs easier.

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

Book recommends which IDEs?

A

PyCharm, Spyder, PyDev, Idle, and Wing.

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

writing comments in python

A

use # at the start of a line or
Triple single quotes at the start and end of your multi-line statement:
‘’’
comment 1
2
3
‘’’

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

variables: how to assign string variables?

A

use “
stuff = “cloudy”

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

Python Operators

A
  • Operators are symbols that perform functions and are used to manipulate data in different ways
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example operators

A

Operators:
- assignment is the = for assigning variables
- R = 12
- S = 5
- T = S
- ^^ assigning variable to a variable

  • Addition: +
    • R + S = 17
  • Subtraction: -
    • R - S = 7
  • Multiplication: *
    R * S = 60
  • Division: /
    • R / S = 2.4
  • Floor division: //
    • This divides and then rounds down to the nearest whole number
    • R // S = 2
  • Modulus (mod): %
    • Modulus operator produces the remainder of the division operation
    • R % S = 2
  • Exponent: **
    • R ** S is R to the S power
    • R ** S = 248,832

R = 12
S = 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  • Floor division:
A

//
- This divides and then rounds down to the nearest whole number
- R // S = 2
R = 12
S = 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  • Modulus (mod):
A

%
- Modulus operator produces the remainder of the division operation
- R % S = 2
R = 12
S = 5

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

**
- R ** S is R to the S power
- R ** S = 248,832
R = 12
S = 5

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

Can you combine operators?

A

Yes. Specifically this way:
Short handing R = R * 2
shrinks to R * = 2

Same for the other operators

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

Variables are stored as what primary Data Types in Python?

A

Primary Data types:
- integers
- floats
- strings

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

What are the the different data structures used for the primary data type variables?

A
  • lists
  • tuples
  • dictionaries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Integers

A
  • same as math class
  • whole numbers
  • positive or negative
  • assigns normally to a variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

floats

A
  • Numbers that contain decimal parts
  • positive or negative
  • assigns normally to a variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

strings

A
  • text data
  • assign them using “ “ or ‘ ‘
  • string_1 = “string”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What order should nested quotes be used inside strings?

A
  • Use “ “ first, then ‘ ‘ inside the quoted space
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How are numbers treated in a quote do when assigned to a variable?
A = “12”

A

Python sees them as a string.

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

String Manipulation

A
  • you can combine them the same way we combine numbers:
    str_1 = “Words “
    str_2= “and “
    str_3= “more words.”

str_4 = str_1 + str_2 + str_3
print(str_4)

Running this =
Words and more words.

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

changing letter case using built in functions. Explain upper and lower case usage with string variables.

A

Using the following functions will change the letter case to upper or lower:
my_up_string = “all MY letters”.upper()
my_dn_string2 = “ all MY letters”.lower()

print(my_up_string + my_dn_string2)

running this =
ALL MY LETTERS all my letters

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

String formatting example using %

A

Using mod operator % on strings allows you to specify values / variables you would like to insert into a string.

string = “With the mod op, you can add %s, integers like %d, or even floats like %2.1f.”%(“strings”, 25, 12.34)

print (string)

running this =
With the mod op, you can add strings, integers like 25, or even floats like 12.3.

where every % gets its variable / value piped in from the entries at the end of the variable assignment line. special notes here:
- %s is a string
- %d is an integer (digit?)
- %f is a float, but in this instance we used %2.1f
- %2.1f took the 12.34 and read it as needing 2 digits before the decimal place and one digit after the decimal place resulting in 12.3

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

Automatic formatting example using {}

A

There’s more on this later in the chapter, but using the format function for strings instead of using the modulus operator.
- Inside the brackets goes:
- data type tag
- position of the value in the collection of values you want in that spot
- it is possible to leave the brackets blank. Python will automatically fill them in from left to right with the values listed left to right.

“The string you want to format {} “.format(values you want to insert)
- curly braces denote the location where you want to insert the value.
- to insert multiple values, you need to create multiple braces then separate the values with commas

string = “with the mod operator, you can add {0:s}, integers like {1:d}, or even floats like {2:2.2f}”

print (string.format(“strings”, 25, 12.34))

will print =
with the mod operator, you can add strings, integers like 25, or even floats like 12.3

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

Python starts with 0 or 1?

A

Unlike some other programming languages, python is a zero-based system when it comes to positions. First position is ZERO, second is ONE.

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

what is Type Casting used for?

A

Converting data from one type to another type.
e.g. String to Integer

28
Q

int()

A

convert to integer
- going FLOAT to INTEGER means it will drop the decimal values and always round down to the whole number value
- strings to integers obviously need to be numbers

29
Q

float()

A

convert to float
strings to floats obviously need to be numbers.

30
Q

str()

A

convert to string
basically anything can become a string.

31
Q

Data Structures?

A

How data is stored.

32
Q

what are the different data structures in python?

A

Lists
Tuples
Dictionaries

33
Q

Lists

A

Data structure
Collections of data
quick and easy storage and retrieval of items
Functionally this is done by saving the list as a variable that’s declared as a list

34
Q

List: syntax example of a list

A

Fruits = [“apple”,”pear”,”coconut”]

35
Q

List: Can you declare an empty list to append items to later?

A

yes
Fruits = []
append() to add more later

36
Q

List: How do you access a specific item in the list?

A

when the list is:
Fruits = [“apple”,”pear”,”coconut”]

then the first item is:
apple = fruits[0]

37
Q

List: call something from the end or second to last of the list

A

Fruits = [“apple”,”pear”,”coconut”]

last = Fruits[-1]
seclast = Fruits[-2]
print(last)
print(seclast)

Using slicing.
a[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items

38
Q

List: How do you call multiple items from a list?

A

Using slicing.
where a = a list
a[start:stop:step]
start through not past stop, by step

a[start:stop]
items start through stop-1
a[1:5], will print items 1 through 4

a[start:]
items start through the rest of the array

a[:stop]
items from the beginning through stop-1

a[:]
a copy of the whole array

39
Q

List: Reversing the order of items in a list

A

a[::-1] # all items in the array, reversed
a[1::-1] # the first two items, reversed
a[:-3:-1] # the last two items, reversed
a[-3::-1] # everything except the last two items, reversed

40
Q

List: in what ways are lists modified?

A

add, remove, and values can be altered

41
Q

List: How to append lists?

A

list_to_append.append(value)
where
value = item number (starting at 0)

42
Q

List: How to remove items from lists?

A

list_to_append.remove(value)
where
value = item number (starting at 0)

43
Q

List: How to remove items from lists?

A

To remove a single entry:
list_to_append.remove(value)
where
value = item number (starting at 0)

to remove multiple entries at once:
del List_Name[2:4]
which will delete everything from the 3rd item through the 4th item

44
Q

list: how to edit the 2nd through the 4th items in a list

A

Fruits[1:4] = [“pear1”,”coconut1”,”riot1”]

45
Q

list: how to insert items into a list

A

list_name.insert(Where,Inserted_Item)

a.insert(1,”outland”)
Inserts outland string before list item 1, but after list item 0 and pushing everything else after it down the list

46
Q

Whats the difference between lists and arrays?

A

Lists are basically the same as other arrays outside of python.

Python do have arrays too, they are more functional than lists if you want to say divide everything in the array by 4. Doing this with lists will result in an error.

47
Q

Tuples: What are tuples?

A

Similar to lists, but can’t be modified

48
Q

Tuples: when would Tuples be beneficial?

A

Whenever you have an unchanging list of items. Examples include the days of the week, Months, etc.

49
Q

Tuples: How do you define Tuples in py?

A

MyTuple = (“Mon”,”Tue”,”Wed”)

Using parenthesis instead of brackets.

50
Q

Tuples: How are Tuples called / accessed in py?

A

Same as lists

51
Q

Dictionaries: what are they?

A

Dictionaries hold data that can be retrieved with reference items or keys

They contain pairs of keys and values associated with those keys

52
Q

Dictionaries: Can you have 1 key open multiple box

A

No that would be problematic. e.g. 2 keys both named cyfer1

53
Q

Dictionaries: How do you create a key in py?

A

Use curly braces containing the key on the left side and the value on the right side.
separated by a colon.

dict_example = {“key0”:39, “key1”:50}

or by using the dict() method
dict_example2 = dict(key0 = 39, key1 = 21)

54
Q

Dictionaries: How do you access the items in one?

A

dictionary[‘key’]

(Bracket, single quote surrounding key value)

example:
dict_example2 = dict(key0 = 39, key1 = 21)

dict_example2[‘key0’]

would resolve to 39

55
Q

Dictionaries: Can you define an empty one? If so how?

A

dict_example2 = {}

To add data to the dictionary, just assign a value:
dict_example2 [“key1”] = 60

56
Q

Dictionaries: how do you add entries?

A

To add data to the dictionary, just assign a value to key
dict_example2 [‘key1’] = 60

57
Q

Dictionaries: how do you remove entries?

A

del dict_example2[‘key0’]

Would remove the value associated to key0

58
Q

Inputs: How do you solicit and accept input from the user?

A

the input() function
similar to read -p command in bash

59
Q

Inputs: How to store a variable using input()

A

food = input(“What food do you want?:\n”)

60
Q

Variables: Best way to print two variables using f-string formatting?

A

(f-string formatting is available in Python 3.6)
example:
name = “fine”
score = 20
print(f’Grand total for {name} is {score}’)

61
Q

Print: How can you print out less wide blocks of text without using /n?

A

example:
use triple single quotes and enter between lines to space it however you wish:

longstring = ‘'’Using triple quotes will allow us to
divid our statements onto multiple lines This makes
it easier to read As you can see this shrinks up the
output…’’’
print(f’{longstring}’)

62
Q

Print: how to use raw string formatter as an escape on an entire string?

A

escape character r or R

example:
This applies to the entire string:
print(r”hey hey \t we’re the monkeys.”)
print(R”hey hey \t we’re the monkeys.”)

normal print
print(“hey hey \t we’re the monkeys.”)

63
Q

Print: how to avoid a long string from running across the screen?

A

Use triple ‘ to surround the string and apply normal text editing with new lines and tabs.
example:
print(‘’’hey there
How’s it going?
Oh that’s good!‘’’

64
Q

Delete

A
65
Q

Print: how do you print more than one string in a print or input function?

A

You can using the format function.
Example:
name = “B”
nametwo = “john”
foodtwo = input(“What food does {0} want and what kind does {1} want?:\n”.format(name,nametwo))

66
Q

Print: how to use an escape character on a specific item WITHOUT using raw string formatter on the entire string?

A

example:
use \
print(“hey hey \\t we’re the monkeys.”)