All the function learnt this week - Week 2
if :
The if function is used when you are asking a question or statement and you want a specific reply to each answer. With this you have to be careful with the indentation making sure they line up with your programming, otherwise your coding wont work.
Example -
food = input("What food do you like? ") ----or-- raining = input("Is it raining (yes/no)? ")
if food == "cake": if raining == "yes":
print("Wow, I love cake too!") print("You should take the bus to work.")
print("Did I tell you I like cake?") if raining == "no":
print("You should walk to work.")
Nothing appearing :
When you want to make sure nothing appears other than the coding use empty quotations at the end of your code .
Example -
text= input("Text: ")
if text == "Marco!":
print("Polo!")
if text == "":
print("")
True/ False:
In python they use true and false with if functions and other parts of the coding.
Example -
x = 3
print(x > 10)
False
Else :
Else is better for simplifying if because you can use it to apply a certain answer to any other input other than the expected answer.
Example -
raining = input("Is it raining (yes/no)? ")
if raining == "yes":
print("You should take the bus to work.")
else:
print("You should walk to work.")
- Comparing :
In else you can use operations to compare. This is available because the program wont always make sense so with these you can make it conditional to the coding.
Example -
x = 5
if Charge <=5:
print("Connect your charger!")
else:
print("All good.")
Decisions with multiple options (elif):
The if function is used when you are asking a question or statement and you want a specific reply to each answer. With this you have to be careful with the indentation making sure they line up with your programming, otherwise your coding wont work.
Example -
food = input("What food do you like? ") ----or-- raining = input("Is it raining (yes/no)? ")
if food == "cake": if raining == "yes":
print("Wow, I love cake too!") print("You should take the bus to work.")
print("Did I tell you I like cake?") if raining == "no":
print("You should walk to work.")
Nothing appearing :
When you want to make sure nothing appears other than the coding use empty quotations at the end of your code .
Example -
text= input("Text: ")
if text == "Marco!":
print("Polo!")
if text == "":
print("")
True/ False:
In python they use true and false with if functions and other parts of the coding.
Example -
x = 3
print(x > 10)
False
Else :
Else is better for simplifying if because you can use it to apply a certain answer to any other input other than the expected answer.
Example -
raining = input("Is it raining (yes/no)? ")
if raining == "yes":
print("You should take the bus to work.")
else:
print("You should walk to work.")
- Comparing :
In else you can use operations to compare. This is available because the program wont always make sense so with these you can make it conditional to the coding.
Operation | Operator |
---|---|
equal to | == |
not equal to | != |
less than | < |
less than or equal to | <= |
greater than | > |
greater than or equal to | >= |
x = 5
if Charge <=5:
print("Connect your charger!")
else:
print("All good.")
Decisions with multiple options (elif):
When you need to make more than one option you can use else and if to create the other option but it can get a bit mess or instead you can use elif as a abbreviation
Example -
Using elif Not using elif
x = 5 else:
if x < 3: if x == 3:
print("x is less than three") print("x is equal to three")
elif x == 3:
print("x is equal to three")
else:
print("x is greater than three")
String within a string
Example -
msg = 'hello world'
print('h' in msg)
True
Sub string within a string
Example -
msg = 'concatenation is fun'
print('cat' in msg)
print('dog' in msg)
True
False
Making Decisions
Using if with a variable/string means you limit what you are looking for.
Example -
name = input('Enter your name? ')
if 'x' in name:
print('Your name contains an x!')
else:
print('No "x" in your name.')
String Methods
Changing to lowercase:
Example -
msg = "I know my ABC"
print(msg.lower())
or
msg = "I know my ABC"
newmsg = msg.lower()
print(newmsg)
Changing to uppercase:
msg = "I know my ABC"
newmsg = msg.upper()
print(newmsg)
or
For printing statements:
msg = "I know my ABC"
print("Original:", msg)
print("Lowercase:", msg.lower())
print("Uppercase:", msg.upper())
Original: I know my ABC
Lowercase: i know my abc
Uppercase: I KNOW MY ABC
Checking the case:
Example -
msg = "a lowercase string!"
print(msg.islower())
print(msg.isupper())
For first letter capital and rest not use name.capitalize():
name = input('Enter your name: ')
print('Name converted to:', name.capitalize())
Enter your name: XAVIER
Name converted to: Xavier
Example -
msg = 'hello world'
print(msg.replace('l', 'X'))
heXXo worXd
or
msg = 'hello world'
msg = msg.replace('hello', 'goodbye')
msg = msg.replace('o', 'X')
print(msg)
gXXdbye wXrld
Counting Characters in a string
This will check how many characters are in that string you an also use double letter such as ll.
Example -
msg = "hello world"
print(msg.count("l"))
3
String within a string
Example -
msg = 'hello world'
print('h' in msg)
True
Sub string within a string
Example -
msg = 'concatenation is fun'
print('cat' in msg)
print('dog' in msg)
True
False
Making Decisions
Using if with a variable/string means you limit what you are looking for.
Example -
name = input('Enter your name? ')
if 'x' in name:
print('Your name contains an x!')
else:
print('No "x" in your name.')
String Methods
Changing to lowercase:
Example -
msg = "I know my ABC"
print(msg.lower())
or
msg = "I know my ABC"
newmsg = msg.lower()
print(newmsg)
Changing to uppercase:
msg = "I know my ABC"
newmsg = msg.upper()
print(newmsg)
or
For printing statements:
msg = "I know my ABC"
print("Original:", msg)
print("Lowercase:", msg.lower())
print("Uppercase:", msg.upper())
Original: I know my ABC
Lowercase: i know my abc
Uppercase: I KNOW MY ABC
Checking the case:
Example -
msg = "a lowercase string!"
print(msg.islower())
print(msg.isupper())
For first letter capital and rest not use name.capitalize():
name = input('Enter your name: ')
print('Name converted to:', name.capitalize())
Enter your name: XAVIER
Name converted to: Xavier
Replacing String
Using this function will let you replace parts of the string.Example -
msg = 'hello world'
print(msg.replace('l', 'X'))
heXXo worXd
or
msg = 'hello world'
msg = msg.replace('hello', 'goodbye')
msg = msg.replace('o', 'X')
print(msg)
gXXdbye wXrld
Counting Characters in a string
This will check how many characters are in that string you an also use double letter such as ll.
Example -
msg = "hello world"
print(msg.count("l"))
3
No comments:
Post a Comment