Python Flashcards

(28 cards)

1
Q

variable

A

name for something so you can use the name rather than the something as you code.

ex.
cars = 100
print “There are”, cars, “cars available.”

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

string

A

” (double-quotes) or ‘ (single-quotes) around text.

ex.
print “Let’s talk about coding.”

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

format string

A

Putting a variable inside a string.

ex.
my_name = ‘Wealth Coding”

print “Let’s talk about %s.” % my_name
print “I said %r is better than %r.” % (jordan, kobe)

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

format character

A

% (percent) followed by the variable.

ex.
%d, %r, %s

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

\n (back-slash n)

A

Go to next line.

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

””” (3 double-quotes)

A

Print text on multiple lines

ex.
print """
I want to print
on multiple lines
so I will.
"""
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

escape sequences

A
\n go to the next line.
\\ just one back slash.
\t tab
\t* indent and asterisk
"I am 6'1\" tall" # escape double-quote
'I am 6\'1" tall.' escape singe=quote
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

raw_input()

A

How to get user to enter information.

ex.
print “How old are you?,
age = raw_input()

print “So, you’re %r old.” % age

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

raw input with a prompt

A

ex.

age = raw_input(“How old are you? “)

print “So, you’re %r old.” % age

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

script

A

another name for .py files

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

argument

A

When you type python ex13.py in terminal, ex13.py is the argument.

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

modules

A

features you import to make Python do more (also called libraries).

ex.
from sys import argv

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

argv

A

argument variable. This variable holds the arguments you pass to your Python script when you run it.

ex. from sys import argv
script, first, second = argv
print “The first variable is:”, first

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

unpack

A

“Take whatever is in argv (or something else), unpack it, and assign it to all of these variables on the left in that order.

ex.
script, first, second, third = argv

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

raw_input(prompt)

A

something like > prompt to get input from user. Similar to Zork or Adventure.

prompt = ‘> ‘

print “What do you like %s?” % user_name
likes = raw_input(prompt)

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

open(filename)

A

open a file.

ex.
from sys import argv
script, filename = argv
txt = open(filename)

note must: python ex15.py sample.txt

17
Q

txt.read()

A

reads text in python

ex.
print “Here’s your file %r:” % filename
print txt.read()

18
Q

close

A

Closes the file. Like file->Save in your editor.

19
Q

read

A

Reads the contents of the file, you can assign the result to a variable.

20
Q

readline

A

Reads just one line of a text file.

21
Q

truncate

A

Empties the file, whatch out if you care about the file.

22
Q

write(stuff)

A

Writes stuff to the file.

23
Q

w

A

use so you can write to the file

ex.
print “Opening the file. . .”
target = open(filename, ‘w’)

24
Q

target.close()

A

closes the file. “target” can be any other format

25
Functions
1. They name pieces of code the way variables name strings and numbers. 2. They take arguments the way your scripts take argv. 3. Using #1 and #2 they let you make your own "mini scripts" or "tiny commands".
26
def
How you make functions in Python. def is short for define.
27
*args
works like argv parameter but for functions. Has to go inside ( ) parenthesis to work. ``` ex. def some_name(*args): ```
28
return
ex. ``` def add(a, b): print "ADDING %d + %d" % (a, b) return a + b ``` age = add(30, 5)