What is string concatenation?
Things we can do with string concatenation.
What is AND OR logic?
String concatenation is the same as appending one string to another. When we want to concatenate two strings together we use the plus symbol like this:
fruits = "apple "+"banana"
# output is 'apple banana'
When using string concatenation spaces are not put in between strings for you. To add a space between two strings you have to do it yourself like this:
first_name = "Bill"
last_name = "Murray"
full_name = first_name + " " + last_name
print full_name + " ain't afraid of no Ghost!"
# output is 'Bill Murray ain't afraid of no Ghost!'
Let's take a look at how we can apply string concatenation to things we already know:
Creating a single string from a list of strings:
word_list = ["How"," are"," you"," today?"]
string = ""
for word in word_list:
string = string + word
# outputs 'How are you today?'
In python we use AND and OR logic when we want to have multiple conditionals inside of one if statement. For example, let's say we only want people to be able to ride a roller coaster if they are taller than 4 feet and shorter than 7 feet. In Python, we can use the and keyword, as shown below:
if height > 5 and height < 7:
print "You can ride."
print "You cannot ride."
When we use and we are saying that if both the left conditional and the right conditional are true, the entire conditional is true. Now let's say we want a vending machine to only accept 1 dollar bills or 5 dollar bills. To do this, we use the or keyword as shown below:
if money == 1 or money == 5:
print "Payment Accepted!"
print "Payment Rejected!"
When we use or we are saying that if either the left conditional or the right conditional are true, the entire conditional is true.
Sometimes we would like to check that something is not true. To be president in the United States you must not be younger than 35 years old. In Python we can use the not keyword, as seen below:
if not age < 35:
print "You can be president!"
print "Sorry, wait a few more years."
Dealing with with and, or and not logic can sometimes get complicated and confusing. To make determining the result of a conditional statement easier, we can use truth tables. Let us say that the variables A and B can be either true or false. The truth table for and is found below:
|A||B||A and B|
The first two columns of the table represent the truth values of the variables. We need to make sure we have every combination of true and false. Try and fill out the empty truth table for or, found below:
|A||B||A or B|
Now think about the not table. What would it look like? Hint: you only need one variable to negate.
Bonus time! In logic there is a rule called De Morgan's law which states not (A and B) = (not A) or (not B). Don't worry if it is unclear why this is true, that is what truth tables are for! Below is an empty truth table. (Note that there are intermediate columns to help you along the way.)
|A||B||not A||not B||A and B||not (A and B)||(not A) or (not B)|
Do you notice anything about the last two columns? What does that mean about the above equality? If you would like to place one of these tables on your webpage you can copy that portion of this page's HTML code, paste it to your webpage, and then fill in your answers for the table.