Python General Code Syntax Flashcards
x = raw_input()
reads text from standard input and stores the entry into a variable
x = raw_input(‘Enter Value’)
prompts user with text in parentheses
argv
argument variable
var, var1, var2, var3, . . . = argv
unpacks the command line parameter into their respective variables. The first is always the program name (.py file). And the following parameter are loaded into their respective variables
import
imports a module into a python program
txt = open(filename)
opens a file and returns a file object which is stored in the variable txt.
txt.read()
Reads the text stored in the file object referenced by the variable txt.
print “some text”
prints text to standard out using double quotes
print ‘some text’
prints text to standard out using single quotes
print “print it’s text”
prints text to standard out including a single quote
python program_name.py
Executes a python program from the terminal
some text
An “octothorpe” also known as ‘pound’, ‘hash’, or ‘mesh’ or whatever name makes you chill out!
print “ some text %s” % variable_name
Format character for string, replaces the %s with the value stored in ‘variable_name’
print “some text %s and %s some more text” % (var_1, var_2)
Format characters for multiple variable, replaces the %s in the order found in the parenthesis.
%r
Format character, which returns a result which is valid python syntax using the repr( ) function.