Python Flashcards

1
Q

Loop through a dict

A

for key, value in original_dict.items():
reversed_dict[value] = key

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

Filter

A

adults = filter(myFunc, ages)

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

map

A

map(function, iterable)

org_list = [1, 2, 3, 4, 5]
# define a function that returns the cube of num

def cube(num):
return num**3

new_list = list(map(cube, org_list))

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

Generators

A

to print all

def gen_numbers():
for i in range(1, 11):
yield i
for i in gen_numbers():
Print i

to go through them 1 by 1:
numb = gen_numbers()
print(next(numb))

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

While Loop

A

while i <= n:
print(i)
i = i +

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

for loop

A

for x in range(0, 3):
print(“We’re on time %d” % (x))

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

Range

A

range(min, max_not_included, step)
range(6) will produce 0-5
range(1,11) will produce 1-10
range(1,6,2) will produce 1,3
range(10, 0, -1) will produce a countdown

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

Exponent

A

x**3 (cubed)
x**10

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

Define string that prints when you look at a class

A

class Elevator:
def __str__(self):
return “Current floor: “ + str(self.current) + “. returned when you call print(elevator) on an instance”

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

Recursion Example

A

def sum_positive_numbers(n):
# The base case is n being smaller than 1
if n < 1:
return 0
# Recursion Case
return n + sum_positive_numbers(n - 1)

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

Help

A

help(function_or_class)

Returns doc string

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

Get Values only from a dict

A

for package in self.packages.values()

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

Random number

A

import random

random.randint(1,10)

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

Get date

A

import datetime

bd = datetime.date(20223, 5, 30)
hbd = bd + datetime.timedelta(days=162.5)
hbd.strftime(‘%Y-%m-%d’)

now = datetime.datetime.now()
now.year()

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

Remove an item from a dict

A

dict.pop(‘key’)

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

Sort lists

A

list.sort()

new_list = list.sorted()

defaults to alpha, you can specify sort(key=my_sort_function) to determine what sorts.

17
Q

Sets

A

Set items are unordered, unchangeable, and do not allow duplicate values.

set1 = {‘Python’, ‘R’, ‘SQL’}
set2 = set()
set2.add(‘Python’)

18
Q

Polymorphism

A

Overwriting parent attribute or function
class Animal:
def age(self):
self.age = 5

class Rabbit(Animal):
def age(self):
self.age = 5

19
Q

Super

A
class Emp():
    def \_\_init\_\_(self, id, name, Add):
        self.id = id
        self.name = name
        self.Add = Add
 
# Class freelancer inherits EMP
class Freelance(Emp):
    def \_\_init\_\_(self, id, name, Add, Emails):
        super().\_\_init\_\_(id, name, Add)
        self.Emails = Emails
 
Emp_1 = Freelance(103, "Suraj kr gupta", "Noida" , "KKK@gmails")
20
Q

Python

Subprocess Popen

A
import subprocess

Create a subprocess object
p = subprocess.Popen(["ls", "-l"])

Get the output of the subprocess
output = p.communicate()[0]

Print the output
print(output)
21
Q

Subprocess Run

A
import subprocess
subprocess.run(["command", "arg1", "arg2"])
22
Q

Python - What does the single underline in front of a variable.

_my_variable = 10

A

this makes a variable private to the flass or function it’s in.

23
Q

What does the underscore her do

label, has_label, _ = text.partition(‘:’)

A

The underline is for a returned value you want to throwaway

24
Q

Python

How do you add or combine lists

A
list1 = [1, 2]
list1.append(3)
list1.extend([5, 6])
list2 = [7,8]
list2 = list2 + [9,10]
list1 += list2
print(list2)
25
Q

Python

what does 3 * [0,0,0] do?

A

[0, 0, 0, 0, 0, 0, 0, 0, 0]
Nine 0’s