Python Functions and Syntax

0.0(0)
Studied by 1 person
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/38

encourage image

There's no tags or description

Looks like no tags are added yet.

39 Terms

1
New cards

if/else function

if [condition]:
	[action]
else:
	[action]


2
New cards

if/elif/else function

if [condition]:
	[action]
elif [condition]:
	[action]
else:
	[action]


3
New cards

Gives type of item

type([item])


4
New cards

Assigns user input to variable

variable = input("[question]")


5
New cards

String

Type that user input returns

6
New cards

List

numbers = [1, 2, 3]


7
New cards

Array

numbers = np.array([1, 2, 3])


8
New cards

Place terms in order

list.sort()


9
New cards

Prints term in list

print(list[x])


10
New cards

Set value of term

list[x] = ...


11
New cards

Gives length of list

len(list)


12
New cards

Adds term at end of list

list.append([term])


13
New cards

Replaces value of term

list.insert([term], [replacing value])


14
New cards

Removes last term

list.pop()


15
New cards

Removes specific term

list.pop([term])


16
New cards

Clears list

list.clear()


17
New cards

Slices list

list[start:end:step]


18
New cards

Iteration

for [variable] in [list]:
	[action]


19
New cards

Iterator object

iter()


20
New cards

Gives numbers in range

range(start, stop, step)


21
New cards

Performs action for numbers in range

for i in range(start, stop, step):
	[action]


22
New cards

for loop

Executes a block of code for each element

23
New cards

while loop

Continuation of block of code until a certain criteria is met

24
New cards

Continues action for given condition

while [condition]:
	[action]


25
New cards

Terminates loop

break


26
New cards

Defines function

def function():
	[function]


27
New cards

Defines function with parameter

def function([parameter]):
	[function]


28
New cards

Generators integer between limits

random.randint([lower], [upper])


29
New cards

Import all functions from package

from [package] import *


30
New cards

Import specific function from package

from [package] import [function]


31
New cards

Import function with shorthand

import [package] as [shorthand]


32
New cards

Checks for type of argument

isinstance([variable], [type])


33
New cards

Shows error message

raise Value Error


34
New cards

Catches exception

try:
	[action]
except:
	[action]


35
New cards

Rounds to 2 decimal places

number:.2f


36
New cards

Rounds to 4 decimal places

number:.4f


37
New cards

User Friendly

Try/except error user friendliness

38
New cards

Not User Friendly

Raise error user friendliness

39
New cards

end=’ ‘

kwarg for print function that makes printing all on one line (rather than starting a new line each print instance)