Valid string:
bookstore = 'City lights'
name = "Moe's"
Invalid string:
bookstore = 'test"
name = 'Moe's'
Assign a variable and display it to the screen
greeting = 'Hello world!'
print(greeting)
greeting
If we assing new varaible below existing one and we try to print it then
it wont work without the print() function.
spanish_greeting = 'Hola.'
print(spanish_greeting)
arabic_greeting = 'Ahlan wa sahlan.'
String concatenation:
spanish_greeting = 'Hola.'
# We adding the word sir! to the variable and print it
print(spanish_greeting + ' sir!')
# We print the variable spanish_greeting 5 times.
print(spanish_greeting * 5)
Implement string methods:
# Set new variable name equal to Corey
name = 'Corey'
# Make the name to lowercase
name.lower()
# Make the name capitalize
name.capitalize()
# Make the name uppercase
name.upper()
# Count the occurence of the letter o in the value
name.count('o')
Types and Casting:
# Shows the type of the value 5 which is a string at the moment
type('5')
# Conver the 5 and 7 to integers and sum them up
int('5') + int('7')
The input() function
name = input()
print('Greetings {}'.format(name))
Activity 3: rate your day!
print("How is your day today from 1 to 10:")
score = input()
print("you said you score was: {}".format(score))
How is your day today from 1 to 10:
5
you said you score was: 5
Boolean variables
# Classify over 18 to true
over_18 = True
type(over_18)
#Classify over 21 to false
over_21 = False
type(over_21)
bool
Comparison operators:
age = 20
age < 13 # False
age >= 20 and age <= 21 # True
age != 21 # True
age == 19 # False
6 == 6.0 # True
6 == '6' # False
(age >= 20 and age < 30) or (age >= 30 and age < 40) # True
True
Excercise 16: using the if syntax
age = 25
if age >= 18:
print("At least you can vote.")
if age >= 21:
print("Poker is avaible!.")
At least you can vote.
Poker is avaible!.
Excercise 17: Using the if-else syntax
age = 20
if age < 18:
print('You aren\'t old enough to vote.')
else:
print('Welcome to our voting program.')
age = 17
if age >= 18:
print('Welcome to our voting program.')
else:
print('You aren\'t old enough to vote.')
Welcome to our voting program.
You aren't old enough to vote.
Activity: Finding the least common multiple
number1 = 24
number2 = 36
i = 1
while True:
i += 1
if i % number1 == 0 and i % number2 == 0:
print(i)
break
72
Excercise 18: Calculating Perfect Sqaures
user_answer = abs(int(input("Enter a number to see if it is a perfect square: ")))
i = -1
square = False
while i <= user_answer ** (0.5):
i += 1
if i*i == user_answer:
square = True
break
if square:
print("The square root of {} is {}.".format(user_answer, i))
else:
print("The number {} is not a perfect square.".format(user_answer))
Enter a number to see if it is a perfect square: 64
The square root of 64 is 8.
Excercise 19: Real estate offer
print("A one bedroom in the Bay Area is listed at $599,000")
offer = abs(int(input("Enter your first offer on the house: ")))
best = abs(int(input("Enter your best offer on the house: ")))
increment = abs(int(input("How much more do you want to offer each time: ")))
offer_accepted = False
while offer <= best:
if offer >= 650000:
offer_accepted = True
print("Your offer has been accepted")
break
else:
print("We are sorry your offer of {} has not been accepted".format(offer))
offer += increment
A one bedroom in the Bay Area is listed at $599,000
Enter your first offer on the house: 50000
Enter your best offer on the house: 690000
How much more do you want to offer each time: 50000
We are sorry your offer of 50000 has not been accepted
We are sorry your offer of 100000 has not been accepted
We are sorry your offer of 150000 has not been accepted
We are sorry your offer of 200000 has not been accepted
We are sorry your offer of 250000 has not been accepted
We are sorry your offer of 300000 has not been accepted
We are sorry your offer of 350000 has not been accepted
We are sorry your offer of 400000 has not been accepted
We are sorry your offer of 450000 has not been accepted
We are sorry your offer of 500000 has not been accepted
We are sorry your offer of 550000 has not been accepted
We are sorry your offer of 600000 has not been accepted
Your offer has been accepted
Activity 5: Building conversational bots using python
#First bot
question_1 = input("What is your name: ")
print("So your name is {}?".format(question_1))
question_2 = input("How human are you? 0 is none human and 10 is very human: ")
print("I think this courtship is over.")
#Second bot
question_3 = int(input("On a scale of 1 to 10, do you like this sesssion: "))
if question_3:
if question_3 == 1:
print("so, you dont like this hea.")
answer = input("Why not: ")
elif question_3 == 2:
print("Hmmmmm very disappointed in you.")
answer = input("What can i do to improve: ")
elif question_3 == 3:
print("Hmm can it not be better?")
answer = input("Pleae GIVE ME BETTER RATE! or you will fail: ")
elif question_3 == 4:
print("Seems going better")
answer = input("Why this rate: ")
elif question_3 == 5:
print("Close to a passing grade it seems")
answer = input("dont cheat me, why this rate: ")
elif question_3 == 6:
print("good good i passed")
answer = input("What can i do to improve: ")
elif question_3 == 7:
print("Wow did not expect this to happen")
answer = input("Improvement: ")
elif question_3 == 8:
print("Hmmmmm very disappointed in you.")
answer = input("What can i do to improve: ")
elif question_3 == 9:
print("so, you dont like this hea.")
answer = input("Why not: ")
elif question_3 == 10:
print("Hmmmmm very disappointed in you.")
answer = input("What can i do to improve: ")
What is your name: test
So your name is test?
How human are you? 0 is none human and 10 is very human: 0
I think this courtship is over.
On a scale of 1 to 10, do you like this sesssion: 2
Hmmmmm very disappointed in you.
What can i do to improve: 6
Please share and spread