enumerate() function
you can iterate through the sequence and retrieve the index position and its corresponding value at the same time.
>>> for i,v in enumerate([‘Python’,’Java’,’C++’]):
print(i,v)
0 Python
1 Java
2 C++
String Format
Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables
name = "John"
age = 23
print("%s is %d years old." % (name, age)) #John is 23 years old
mylist = [1,2,3]
print("A list: %s" % mylist) #A list: [1, 2, 3]
data = ("John", "Doe", 53.44)
format_string = "Hello %s %s. Your current balance is $%s."
print(format_string % data) #Hello John Doe. Your current balance is $53.44
String Functions
astring = "Hello world!"
print(astring.count("l")) # 3
astring = "Hello world!"
print(astring[3:7]) #lo w
astring = "Hello world!"
afewwords = astring.split(" ") # ['Hello', 'world']
s = "Strings are awesome!"
print("a occurs %d times" % s.count("a")) # a occurs 2 times
print("The next five characters are '%s'" % s[5:10]) #The next five characters are 'gs ar'
# Convert everything to uppercase
print("String in uppercase: %s" % s.upper()) # STRINGS ARE AWESOME!
print("Split the words of the string: %s" % s.split(" ")) #['Strings', 'are', 'awesome!']
Range function:
For loops can iterate over a sequence of numbers using the "range"
# Prints out the numbers 0,1,2,3,4
for x in range(5):
print(x)
# Prints out 3,4,5
for x in range(3, 6):
print(x)
# Prints out 3,5,7
for x in range(3, 8, 2):
print(x)
Pdb(python debugger) can invoke as a script to debug the script:
No comments:
Post a Comment