WHAT IS PYTHON?
PYTHON BASICS:
Whitespace matters. Your code will not run correctly if you use improper indentation.#this is a comment.
BASIC PYTHON LOGIC:
if:if test:
#do stuff if test is true
elif test 2:
#do stuff if test 2 is true
else:
#do stuff if both test are false
while:
while test:
#keep doing stuff until test is false
for:
for x in aSequence:
#do stuff for each member of a sequence
#for example- each item in a list
for x in a range(10):
#do stuff 10 times(0 to 9)
for x in range(5,10):
#do stuff 5 times(5 to 9)
PYTHON STRINGS:
A string is a sequence of characters, usually used to store text.Creation: the_string ="Hello world!"
the_string ='Hello world!'
Accessing: the_string[4] returns'o'
Splitting: the_string.split(' ') returns['Hello','world!']
the_string.split('r') returns['Hello wo','ld!']
To join a list of strings together, call join() in C, uses % operator to add elements of a tuple into a string.
this_string = "there"
print "Hello %s!"%this_string returns "Hello there!"
PYTHON TUPLES:
A Tuple consist of a number of values separated by commas. They are useful for ordered pair and returning several values from a function.Creation: emptyTuple = ()
singletemTuple = ("spam",)
thistuple = 12, 89, 'a'
thistuple = (12, 89, 'a')
Accessing: thistuple[0] returns 12

No comments:
Post a Comment