working on exercises
This commit is contained in:
parent
02d3b0ebe9
commit
73822595d9
24
python/learnpython.org/Basics/chapter-05/README.md
Normal file
24
python/learnpython.org/Basics/chapter-05/README.md
Normal file
@ -0,0 +1,24 @@
|
||||
String Formatting
|
||||
|
||||
Here are some basic argument specifiers you should know:
|
||||
|
||||
%s - String (or any object with a string representation, like numbers)
|
||||
|
||||
%d - Integers
|
||||
|
||||
%f - Floating point numbers
|
||||
|
||||
%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
|
||||
|
||||
%x/%X - Integers in hex representation (lowercase/uppercase)
|
||||
|
||||
|
||||
|
||||
|
||||
Exercise
|
||||
|
||||
You will need to write a format string which prints out the data using: the following syntax: Hello John Doe. Your current balance is $53.44
|
||||
|
||||
|
||||
|
||||
|
||||
5
python/learnpython.org/Basics/chapter-05/exercise.py
Normal file
5
python/learnpython.org/Basics/chapter-05/exercise.py
Normal file
@ -0,0 +1,5 @@
|
||||
balance = 53.44
|
||||
firstName = "John"
|
||||
lastName = "Doe"
|
||||
|
||||
print("Hello %s %s. Your current balance is %.2f." % (firstName,lastName,balance))
|
||||
@ -0,0 +1,7 @@
|
||||
firstName = "John"
|
||||
lastName = "Doe"
|
||||
yourAge = 35
|
||||
|
||||
print ("Hello to you %s" % firstName, "%s!" % lastName, "\nYou are %d" % yourAge, "Years old")
|
||||
|
||||
print ("%s is %d years old!" % (firstName,yourAge))
|
||||
42
python/learnpython.org/Basics/chapter-06/README.md
Normal file
42
python/learnpython.org/Basics/chapter-06/README.md
Normal file
@ -0,0 +1,42 @@
|
||||
Basic String Operations
|
||||
|
||||
|
||||
Exercise
|
||||
|
||||
Try to fix the code to print out the correct information by changing the string.
|
||||
|
||||
|
||||
s = "Hey there! what should this string be?"
|
||||
# Length should be 20
|
||||
print("Length of s = %d" % len(s))
|
||||
|
||||
# First occurrence of "a" should be at index 8
|
||||
print("The first occurrence of the letter a = %d" % s.index("a"))
|
||||
|
||||
# Number of a's should be 2
|
||||
print("a occurs %d times" % s.count("a"))
|
||||
|
||||
# Slicing the string into bits
|
||||
print("The first five characters are '%s'" % s[:5]) # Start to 5
|
||||
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
|
||||
print("The thirteenth character is '%s'" % s[12]) # Just number 12
|
||||
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
|
||||
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
|
||||
|
||||
# Convert everything to uppercase
|
||||
print("String in uppercase: %s" % s.upper())
|
||||
|
||||
# Convert everything to lowercase
|
||||
print("String in lowercase: %s" % s.lower())
|
||||
|
||||
# Check how a string starts
|
||||
if s.startswith("Str"):
|
||||
print("String starts with 'Str'. Good!")
|
||||
|
||||
# Check how a string ends
|
||||
if s.endswith("ome!"):
|
||||
print("String ends with 'ome!'. Good!")
|
||||
|
||||
# Split the string into three separate strings,
|
||||
# each containing only a word
|
||||
print("Split the words of the string: %s" % s.split(" "))
|
||||
35
python/learnpython.org/Basics/chapter-06/exercise.py
Normal file
35
python/learnpython.org/Basics/chapter-06/exercise.py
Normal file
@ -0,0 +1,35 @@
|
||||
s = "Str thera! whatsome!"
|
||||
|
||||
# Length should be 20
|
||||
print("Length of s = %d" % len(s))
|
||||
|
||||
# First occurrence of "a" should be at index 8
|
||||
print("The first occurrence of the letter a = %d" % s.index("a"))
|
||||
|
||||
# Number of a's should be 2
|
||||
print("a occurs %d times" % s.count("a"))
|
||||
|
||||
# Slicing the string into bits
|
||||
print("The first five characters are '%s'" % s[:5]) # Start to 5
|
||||
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
|
||||
print("The thirteenth character is '%s'" % s[12]) # Just number 12
|
||||
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
|
||||
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
|
||||
|
||||
# Convert everything to uppercase
|
||||
print("String in uppercase: %s" % s.upper())
|
||||
|
||||
# Convert everything to lowercase
|
||||
print("String in lowercase: %s" % s.lower())
|
||||
|
||||
# Check how a string starts
|
||||
if s.startswith("Str"):
|
||||
print("String starts with 'Str'. Good!")
|
||||
|
||||
# Check how a string ends
|
||||
if s.endswith("ome!"):
|
||||
print("String ends with 'ome!'. Good!")
|
||||
|
||||
# Split the string into three separate strings,
|
||||
# each containing only a word
|
||||
print("Split the words of the string: %s" % s.split(" "))
|
||||
45
python/learnpython.org/Basics/chapter-06/strings.py
Normal file
45
python/learnpython.org/Basics/chapter-06/strings.py
Normal file
@ -0,0 +1,45 @@
|
||||
aString = "Hello World!"
|
||||
|
||||
print ("Use aString.index(\"LETTER\") to return the first time that letter appears")
|
||||
|
||||
print ("The first time o appears is at index : ", aString.index("o")) # returns what place in the string matches the specified character "o" in this case INDEX STARTS AT ZERO!!
|
||||
|
||||
print ("L appears : ", aString.count("l"), "Times") # returns how many times the specified character appears in the string
|
||||
|
||||
print ("\n\nTo return specific portions of a string use aString[start:stop]")
|
||||
print (aString[3:7]) # Will return from the index position 3-7 in the string.
|
||||
|
||||
print (aString[0:5:2]) #This is stepping, meaning you want to only see every 2nd character.
|
||||
|
||||
#
|
||||
print (aString[:-1:])
|
||||
|
||||
print ("\n\nTo print a string backwords use this syntax print (aString[::-1])")
|
||||
print (aString[::-1])
|
||||
|
||||
#to retrieve the last x amount of characters
|
||||
|
||||
print (aString[-1:5]) #This will fetch the last 5 characters
|
||||
|
||||
print ("\n\nTo retrieve a specified length, starting from the beginning of the file, use the syntax aString[:LENGTH]")
|
||||
print (aString[:5])
|
||||
|
||||
print ("\n\n to retrieve a specified length, starting from the end of the file, use syntax aString[-5:]")
|
||||
print (aString[-5:])
|
||||
|
||||
print ("\n\nTo retrieve the entire string, after a specified character, use syntax aString[4:] ")
|
||||
print (aString[4:])
|
||||
|
||||
|
||||
print ("There are also UPPER and LOWER functions for strings!")
|
||||
print (aString.upper())
|
||||
print (aString.lower())
|
||||
|
||||
|
||||
print ("\n\nWe can determine if a string starts or ends with a certain sequence by using: aString.startswith(\"\") and aString.endswith(\"\")")
|
||||
print (aString.startswith("Hello"))
|
||||
|
||||
print ("\n\n You can split strings into multiple strings by using aString.split(\" \")")
|
||||
newString = aString.split(" ")
|
||||
print (newString)
|
||||
print ("It creates a new table, with each string as its entry or index")
|
||||
60
python/learnpython.org/Basics/chapter-07/README.md
Normal file
60
python/learnpython.org/Basics/chapter-07/README.md
Normal file
@ -0,0 +1,60 @@
|
||||
Conditions
|
||||
|
||||
The 'is' operator
|
||||
|
||||
Unlike the double equals operator "==", the "is" operator does not match the values of the variables, but the instances themselves
|
||||
|
||||
x = [1,2,3]
|
||||
y = [1,2,3]
|
||||
print(x == y) # Prints out True
|
||||
print(x is y) # Prints out False
|
||||
|
||||
|
||||
The "in" operator
|
||||
|
||||
The "in" operator could be used to check if a specified object exists within an iterable object container, such as a list:
|
||||
name = "John"
|
||||
if name in ["John", "Rick"]:
|
||||
print("Your name is either John or Rick.")
|
||||
|
||||
|
||||
Boolean operators
|
||||
|
||||
The "and" and "or" boolean operators allow building complex boolean expressions, for example:
|
||||
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.")
|
||||
|
||||
|
||||
|
||||
Exercise
|
||||
|
||||
Change the variables in the first section, so that each if statement resolves as True.
|
||||
|
||||
# change this code
|
||||
number = 10
|
||||
second_number = 10
|
||||
first_array = []
|
||||
second_array = [1,2,3]
|
||||
|
||||
if number > 15:
|
||||
print("1")
|
||||
|
||||
if first_array:
|
||||
print("2")
|
||||
|
||||
if len(second_array) == 2:
|
||||
print("3")
|
||||
|
||||
if len(first_array) + len(second_array) == 5:
|
||||
print("4")
|
||||
|
||||
if first_array and first_array[0] == 1:
|
||||
print("5")
|
||||
|
||||
if not second_number:
|
||||
print("6")
|
||||
Loading…
x
Reference in New Issue
Block a user