Scope:
Convention is to use four spaces (and no tabs) for each level of indentation
Console output:
Command line arguments:
String literals:
String literals can be enclosed in matching single quotes (') or double quotes (").
String variables:
How to check whether string str is empty?
String concatenation:
Type conversions:
print "i = " + i fails: TypeError: cannot concatenate 'str' and 'int' objects. i needs to be converted to string first:
Logical operators:
not, and, or. (a and b) is evaluated only if a is True. (a or b) is evaluated only if a is False.
Function return values:
Functions can return a single or multiple values.
Return statement is return.
Example when function returns a pair:
How to terminate application and return error code:
How to make application to wait for user to press Enter?
Defining and using a class:
Enumerations
Links and references:
Python v2.7.2 documentation
Dive into Python
Python coding guidelines:
Style Guide for Python Code
Convention is to use four spaces (and no tabs) for each level of indentation
Console output:
print "Hello, world!"
Command line arguments:
import sys
for arg in sys.argv:
print arg
if len(sys.argv) != 5:
print 'Usage: example.exe '
sys.exit(1)
print "First argument: ", sys.argv[1]
print "Second argument: ", sys.argv[2]
print "Third argument: ", sys.argv[3]
print "Fourth argument: ", sys.argv[4]
for arg in sys.argv:
print arg
if len(sys.argv) != 5:
print 'Usage: example.exe '
sys.exit(1)
print "First argument: ", sys.argv[1]
print "Second argument: ", sys.argv[2]
print "Third argument: ", sys.argv[3]
print "Fourth argument: ", sys.argv[4]
String literals:
String literals can be enclosed in matching single quotes (') or double quotes (").
String variables:
How to check whether string str is empty?
if str
String concatenation:
name = 'Bojan'
print "Name:"+name
print "Name:",name
sys.stdout.write(name)
sys.stdout.write("\n")
name += '\n'
sys.stdout.write(name)
sys.stdout.write('\"' + name + '\"')
sys.stdout.write('\n')
Output:print "Name:"+name
print "Name:",name
sys.stdout.write(name)
sys.stdout.write("\n")
name += '\n'
sys.stdout.write(name)
sys.stdout.write('\"' + name + '\"')
sys.stdout.write('\n')
Name:Bojan
Name: Bojan
Bojan
Bojan
"Bojan
"
Name: Bojan
Bojan
Bojan
"Bojan
"
Type conversions:
print "i = " + i fails: TypeError: cannot concatenate 'str' and 'int' objects. i needs to be converted to string first:
str(i)
- converts integer i into stringLogical operators:
not, and, or. (a and b) is evaluated only if a is True. (a or b) is evaluated only if a is False.
Function return values:
Functions can return a single or multiple values.
Return statement is return.
Example when function returns a pair:
drive, tail = os.path.splitdrive(path)
How to terminate application and return error code:
sys.exit(4)
How to make application to wait for user to press Enter?
raw_input('Press Enter...')
Defining and using a class:
class CPerson:
def __init__(self, name = ""):
self.name = name
def print_name(self):
sys.stdout.write(self.name + "\n")
...
person = CPerson()
person.name = "Bojan"
person.print_name()
def __init__(self, name = ""):
self.name = name
def print_name(self):
sys.stdout.write(self.name + "\n")
...
person = CPerson()
person.name = "Bojan"
person.print_name()
Enumerations
class EnumType:
Value1 = 1
Value2 = 2
...
x = EnumType.Value2
[source]Value1 = 1
Value2 = 2
...
x = EnumType.Value2
Links and references:
Python v2.7.2 documentation
Dive into Python
Python coding guidelines:
Style Guide for Python Code
Python Coding Guidelines
Some questions I found interesting as I've just started experimenting with (diving into) Python:
Is it worth learning Python?
Are there any good reasons why I should not use Python?
Some questions I found interesting as I've just started experimenting with (diving into) Python:
Is it worth learning Python?
Are there any good reasons why I should not use Python?
No comments:
Post a Comment