Basic operators strings Python supports concatenating strings using
Basic operators - strings • Python supports concatenating strings using the addition operator: #!/usr/bin/python helloworld = "hello" + "world” print helloworld • Python also supports multiplying strings to form a string with a repeating sequence: lotsofhellos = "hello" * 10 print lotsofhellos
Basic operators - lists • List can be joined by addition operator: #!/usr/bin/python even_numbers = [2, 4, 6, 8] odd_numbers = [1, 3, 5, 7] all_numbers = odd_numbers + even_numbers print all_numbers • Python also supports multiplying strings to form a string with a repeating sequence: print [1, 2, 3] * 3
String formatting • Python uses C-style string formatting to create new, formatted strings. #!/usr/bin/python name = "John" print "Hello, %s!" % name % is the operator to format a set of variable in a tuple ame = "John" age = 23 print "%s is %d years old. " % (name, age) mylist = [1, 2, 3] print "A list: %s" % mylist • • %s … string %d … integer %f … float %. <N>f … <N> is precision after comma tuple
String operations #!/usr/bin/python astring = “Hello world!” print astring print len(astring) returns length of astring print astring. index("o") returns the index of o in astring print astring. count("l") returns how often l occurs in astring print astring. split(”o") splits astring at o’s into lists of strings print astring[3: 7] returns the 3 rd to the 6 th letter of astring print astring. upper(), astring. lower() returns all letters of astring as upper/lower case print astring. startswith("Hello") returns true/false if astring starts with “Hello” print astring. endswith("Hello") returns true/false if astring ends with “Hello”
Conditions • Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated: x=2 print x == 2 # prints out True print x == 3 # prints out False print x < 3 # prints out True • The "and" and "or" boolean operators allow building complex boolean expressions: #!/usr/bin/python name = "John" age = 23 if name == "John" and age == 23: print "Your name is John, and you are also 23 years old. " if name == "John" or name == "Rick": print "Your name is either John or Rick. "
in, if, is, not • The "in" operator could be used to check if a specified object exists within an iterable object container, such as a list. #!/usr/bin/python name = “John” if name in ["John", "Rick"]: print "Your name is either John or Rick. ” • if statement if <statement is true>: <do something>. . . . elif <another statement is true>: # else if <do something else>. . . . else: <do another thing>
in, if, is, not #!/usr/bin/python x=2 if x == 2: print “x equals 2!” else: print “x does not equal 2!” • Unlike the double equals operator "==", the "is" operator does not match the values of the variables, but the instances themselves. #!/usr/bin/python x = [1, 2, 3] y = [1, 2, 3] print x == y prints out True print x is y prints out False • not inverts a boolean expression #!/usr/bin/python print not False prints out True print (not False) == (False) prints out False
loops • for loops iterate over a given sequence. #!/usr/bin/python primes = [2, 3, 5, 7] for prime in primes: print prime • for loops can iterate over a sequence of numbers using the range and xrange functions #!/usr/bin/python for x in xrange(5): # or range(5) print x # Prints out 3, 4, 5 for x in xrange(3, 6): # or range(3, 6) print x # Prints out 3, 5, 7 for x in xrange(3, 8, 2): # or range(3, 8, 2) prints in range 3 -8 in steps of 2 print x primes = [2, 3, 5, 7] for prime in primes: print prime
- Slides: 8