Python

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

after we've added options (in parser)

(options, args) = parser.parse_args()

argv

- argument variable - variable holds arguments passed to script when running it script, first, second, third = argv (line 3) - script = name of python script - first, second, third = 3 variables arguments assigned to

open()

- function opens a file - Required argument is filename - Default access_mode is read(r) - Does not return actual content; creates/reads fileObject -

close()

- method flushes unwritten information and closes file object - Not necessary, but important best practice

read()

- method reads a string from an open file - fileObject.read([count]) - Count = # of bytes to read, reads as much as possible if not given

Boolean tests should be..

...simple. If complex, move calculations to variables earlier in function and use a good name for the variable.

Treat if statements like...

..paragraphs. Each if, elif, and else grouping is like a set of sentences. Put blank lines before and after.

Never nest if-statements more than..

..two deep. Try to do one deep (put inside another function).

How many characters to a line?

80 characters

compound data type

A data type that is itself made up of elements that are themselves values.

lambda

A piece of code which can be executed as if it were a function but without a name. (It is also a keyword used to create such an anonymous function.)

\b

ASCII Backspace (BS) - Erases last character printed

\a

ASCII Bell -may cause receiving device to emit a bell or warning of some kind

\r

ASCII Carriage Return (CR) - Resets position to beginning of a line of text

\f

ASCII FormFeed (FF) - ASCII Control character. Forces printer to eject current page and continue printing at top of another.

\t

ASCII Horizontal Tab (TAB) - 8 horizontal spaces; tab

\n

ASCII LineFeed (LF) - Goes to next line -newline escape

\v

ASCII Vertical Tab (VT) - 6 vertical lines; 1 inch

append()

Adds input to the end of a list

input()

Assumes input is valid python expression, returns evaluated result

int()

Convert a string to an integer int(raw_input(> ))

%s

Converts String (converts any Python object using str())

%d

Converts a signed integer decimal

def

Defines a function def function1(): print "this is function 1"

truncate()

Empties the file

%F

Floating point decimal format (UPPERCASE)

%f

Floating point decimal format (lowercase)

%e

Floating point exponential format (lowercase)

%E

Floating point exponential format (uppercase)

universal import

Imports all functions and variables from a module - Can cause conflicts with user defined functions and vars - Better to import only necessary functions from module import *

my_list.insert(4, "cat")

Inserts the string "cat" at the 4th position in a list

%d

Integer format character

{}.format(something)

Is the new string.format in Python 3. This is how indexing works: "My first name is {0} and my last name is {1}. You can call me {0}".format("John","Doe").

What is sys.argv?

It allows you to input parameters from the command line.

split()

Method splits a string into separate phrases - Default is to split on whitespace split(str, num) str = separator (optional) numb = number of separations (optional)

aliases

Multiple variables that contain references to the same object.

The first input operation is called the _____, and its purpose is to get the first input value that will be tested by the validation loop.

Priming read

raw_input()

Raw input prompts the user for an input and then turns that input into a string. In between "(" and ")" the programmer writes the prompt that will prompt the user. When you set raw_input() equal to a variable, that variable becomes what the user inputs.

raw_input('prompt:')

Reads a line of input from user and returns as string

readline()

Reads one line of text file

len(input)

Return the number of items from a sequence or characters in a string

exists()

Returns TRUE if file in argument exists, FALSE if not

%G

Same as "E" if exponent is greater than -4 or less than precision

%g

Same as "e" if exponent is greater than -4 or less than precision

A(n) ?? is a special value that marks the end of a sequence of items.

Sentinel

%c

Single character -accepts integer or single char string

sorted()

Sorts a list from smallest to highest or a string alphabetically sorted(str, reverse=True) <- Sorts backwards

Cardinal Numbers

Start at 0

Ordinal Numbers

Start at 1; First, second, third

%s

String -Converts any python object using str()

%r

String -converts any python object using repr()

%r

String (converts any Python object using repr())

%s

String format character; use for user formatting

len[2] = 3

The 2nd term of the list is now equal to 3

trace

To follow the flow of execution of a program by hand, recording the change of state of the variables and any output produced.

%X

Unsigned Hexadecimal (uppercase)

%x

Unsigned hexadecimal (lowercase)

<>

Value of two operands not equal? -Similar to !=

function parameter

Variable name for passed in argment def function(parameter):

write(stuff)

Writes stuff to file

list_name.append("")

appends thing to lists

lower()

converts a string to lowercase

upper()

converts a string to uppercase

raise

create a user defined exception

%r is used for...

debugging and display

del

deletes objects

Else must have a...

die function that prints out an error message, in case the else doesn't make sense. Shows errors.

%s is used for

display

pass

does nothing

Every if statement must have a(n)...

else

\

escape; tells python to ignore following character, or puts difficult characters into strings when used with specific *escape sequences*

exec

executes Python code dynamically

len(my_list)

finds the length of a list

After the execution of the following statement, the variable sold will reference the numeric literal value as a(n) _____ data type: sold = 256.752

float

from

for importing a specific variable, class or a function from a module

my_list[:]

gives you the entire list

my_list[-1]

gives you the last term in the list

my_list[1:3]

gives you the list starting at the 1st position and ending at the 2nd position

my_list[0:]

gives you the whole list, starting at the 0 position

as

if we want to give a module a different alias

The _____ built-in function is used to read a number that has been typed on the keyboard.

input()

Syntax to index 2 nested lists?

list[x][y]

\b

matches a word boundar

\d

matches any numeric digit

^

matches the beginning of a string

$

matches the end of a string

pop()

method removes and returns the last object from a list

\n

moves whatever's after it to a new ling

%

no argument converted, results in "%" in the result

adding parser options

parser.add_option('-n, '--new')

.remove()

removes items from list

%d

signed integer decimal

%i

signed integer decimal

my_list.sort()

sorts a list from lowest to highest, or alphabetical

\t

tab character

%u

unsigned decimal

%o

unsigned octal

assert

used for debugging purposes

continue

used to interrupt the current cycle, without jumping out of the whole cycle. New cycle will begin.


Set pelajaran terkait

Chapter 15 ~ Aggregate Demand and Aggregate Supply

View Set

1999 ap government test and answers

View Set

CYBR1.CAB-6 Troubleshooting Physical Connectivity (N10-008)

View Set

Anatomy and physiology Chapter 1 midterm review

View Set

Week 4: Chapter 5 : Physical Agents

View Set

APES UNIT 4: Earth Systems and Resources

View Set

ATI - Preterm Labor, Preeclampsia, and Gestational Diabetes

View Set

AP Calculus Second Semester Exam

View Set