L1 - StackingFlowing/CTProgramming GitHub Wiki
L1.1 Creating a message
To print a message to console, we can do:
print("Hello world!")
Alternatively, we can save the message to a variable.
The name of the variable can be anything we want, like message
, myMessage
or aVeryLongName
.
To create a variable, simply use =
.
message = "Greetings! Hello there."
print(message)
Remember to print()
the message so our users can see it!
We can let the user choose what message to be printed.
To do this, we will let the user input()
a message for us.
message = input()
print(message)
When you run it, you might be confused because nothing shows in the console. The user does not know what to do!
We have to tell the user to type something. We can write an instruction between the brackets in input()
.
message = input("Enter your message here: ")
print(message)
Make sure the sentence is a string (surrounded by two "
s), or you will get an error!
L1.2 Creating a key
We need to shift the message by a number. Let's create a variable called "key", and we will ask the user to input()
it for us.
key = input("Enter your key here: ")
We want a positive number for encryption.
if
the key is greater than 0 we will just print()
it out; else
we will warn the user it's not a positive number.
Writing this in Python is like writing in English:
if key > 0:
print(key)
else:
print("Your key is not greater than 0.")
What do you notice when you run the code altogether? For some reason, Python gives an error even when you type a positive number for key!
Apparently, input()
will convert what we typed into a string. We cannot compare a string with a number, so key > 0
looks gibberish to Python.
To solve the problem, we can simply convert key into an integer using int()
:
key = int(input("Enter your key: "))
Python will now know that key is an integer and can compare key > 0
for us now.
What will happen if the input for the key is:
a) 10
b) -6
c) 3.5
d) xyz
For a) we see 10
in the console. For b) the console tells us Your key is not greater than 0.
But for c) and d) we will still get an error, because 3.5
and "xyz"
are not integers. Python does not know how to convert them into an integer.
We can fix this later. For now, let's just warn the user by changing the text inside input()
:
key = input("Enter your key here. it should be a positive integer: ")
L1.3 Iterating through the message
Do you know that the letter E is the most common letter in the English language?
Let's say we want to check if the letter E is in our message. We have to look at each individual character in the message carefully.
To do this, we have to check for
each character in
the message:
information = "ELEPHANT"
for character in information:
print(character)
Run the code above. What do you notice?
In the above example, we created a variable called information
and stored "elephant"
in it. We then let Python look at each character separately and print()
them on different line.
Python can now compare each character
with the letter "E"
:
information = "ELEPHANT"
for character in information:
if character = "E":
print("Found the letter E!")
Of course, we can add more elif
to include all other characters, but luckily Python can find()
these for us.
find()
is a function that finds the position (index) of a specific character in a string. It's easier to understand if we test it out:
myMessage = "Hello world!"
myMessage.find("H") # This will return 0
myMessage.find("e") # This will return 1
...
You might wonder where the 0 comes from. We usually start counting from 1, but computer scientists start from 0.
In fact, computer scientists are fascinated by this number! For example, you may have heard that computers are made of 0s and 1s.
What will happen in each scenario?
e)
word = "shark"
print(word.find("r"))
f)
sentence = "Diet coke was invented in 1982."
print(sentence.find("i"))
g)
phrase = "Catchphrase!"
print(phrase.find("z"))
h)
status = "Deadline"
print(status.find("d"))
For e), the console displays 3
because "r" is the fourth letter in "shark".
For f), the console displays 1
. Although the letter "i" also appears in the word "invented", find()
only returns the index of the first "i" in the sentence.
For g), the console displays -1
. This indicates that "z" does not appear in the phrase at all.
For h), the console displays 3
. It is not 0
because the first letter is capitalised. Python is case-sensitive; UPPER CASE and lower case matter.
The reverse of using find()
is to use index to search the letter. Directly after a string, use the squarely brackets []
:
print("Hello world!"[0]) # will print "H"
fiction = "To Kill a Mockingbird"
print(fiction[0]) # will print "T"
string = "anything"
number = "0"
print(string[number]) # will print "a"