Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic operators - strings

Similar presentations


Presentation on theme: "Basic operators - strings"— Presentation transcript:

1 Basic operators - strings
Python supports concatenating strings using the addition operator: Python also supports multiplying strings to form a string with a repeating sequence: #!/usr/bin/python helloworld = "hello" + " " + "world” print helloworld lotsofhellos = "hello" * 10 print lotsofhellos

2 Basic operators - lists
List can be joined by addition operator: Python also supports multiplying strings to form a string with a repeating sequence: #!/usr/bin/python even_numbers = [2,4,6,8] odd_numbers = [1,3,5,7] all_numbers = odd_numbers + even_numbers print all_numbers print [1,2,3] * 3

3 String formatting Python uses C-style string formatting to create new, formatted strings. %s … string %d … integer %f … float %.<N>f … <N> is precision after comma #!/usr/bin/python name = "John" print "Hello, %s!" % name ame = "John" age = 23 print "%s is %d years old." % (name, age) mylist = [1,2,3] print "A list: %s" % mylist % is the operator to format a set of variable in a tuple tuple

4 String operations #!/usr/bin/python astring = “Hello world!”
print astring print len(astring) print astring.index("o") print astring.count("l") print astring.split(”o") print astring[3:7] print astring.upper(), astring.lower() print astring.startswith("Hello") print astring.endswith("Hello") returns length of astring returns the index of o in astring returns how often l occurs in astring splits astring at o’s into lists of strings returns the 3rd to the 6th letter of astring returns all letters of astring as upper/lower case returns true/false if astring starts with “Hello” returns true/false if astring ends with “Hello”

5 Conditions Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated: The "and" and "or" boolean operators allow building complex boolean expressions: x = 2 print x == 2 # prints out True print x == 3 # prints out False print x < 3 # prints out True #!/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."

6 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. if statement #!/usr/bin/python name = “John” if name in ["John", "Rick"]: print "Your name is either John or Rick.” if <statement is true>: <do something> .... elif <another statement is true>: # else if <do something else> else: <do another thing>

7 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. not inverts a boolean expression #!/usr/bin/python x = [1,2,3] y = [1,2,3] print x == y prints out True print x is y prints out False #!/usr/bin/python print not False prints out True print (not False) == (False) prints out False

8 loops for loops iterate over a given sequence.
for loops can iterate over a sequence of numbers using the range and xrange functions #!/usr/bin/python primes = [2, 3, 5, 7] for prime in primes: print prime #!/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) # Prints out 3,5,7 for x in xrange(3, 8, 2): # or range(3, 8, 2) primes = [2, 3, 5, 7] for prime in primes: print prime prints in range 3-8 in steps of 2


Download ppt "Basic operators - strings"

Similar presentations


Ads by Google