Python Coding Competition - Week 1 Part 1


What Is This Coding Competion


This competition that I have enrolled in is called Groklearning, and it is essentially a competition throughout different schools to compete in coding. There are 3 levels, newbie, beginner, and intermediate. I have chosen the intermediate comp as I want to challenge myself and the language chosen is Python.


What I Learnt


In this first week, it introduced us to the basics, strings and integers, different functions, for loops, range, if statements, and operators. I will be going through each of them individually to grasp a deeper understanding and not so much of a broad explanation.

The Basics

This section is meant to teach you the fundamentals concepts such variables, inputs and outputs

word = input('Enter a word that rhymes with "free": ')
print('Now for a short poem.')
print(word)
print('free')

As you can see, we create a variable called word, and we make this variable equal to an input. This input takes in an input from the user. After we put brackets and apostrophes. When writing text called strings in Python, you always need the apostrophes around. Then we add our own text, such as Enter a word that rhymes with "free":

Next, we use print . Whenever you want to display something on the console, print is what we use. Once we define print, we add the brackets and apostrophes again since we are typing a string.

In this next line, we do something different. Instead of typing a string, we define a variable. We do this by just adding the variable name inside the brackets. What this will do is, since we take an input, the code will store the user's input and since we called the variable, it will now display whatever the user has written.

Finally, we write another string that says free

If we run this code, we can see that it asks for an input. Once we give it the input, it will now run the rest of our code

F-string

Another important thing to note is f-string. F-string allow you to combine normal string text and variables

fruit = input("What's your favourite fruit? ")
print(f'Would you like some {fruit} juice?')

We create a variable called fruit and take an input. Then print a string, but instead of just apostrophes around the text, we add an f before the apostrophes. This allows us to now add a variable. We do this by adding curly braces and then adding the variable name in between the curly braces

As a result it adds the two text together.

String Indexing

In Python, string indexing is a very easy but confusing subject. Essentially, string indexing is the fancy term used to number characters in a word.

As you can see, we have the word rex. To number each character you have to start with 0 meaning the first character. Here is an example:

dog = 'Rex'
print(f'The first letter is {dog[0]}.')
print(f'The last letter is {dog[2]}.')

The first thing in this code is creating a variable called dog which is equal to a string called 'Rex'.

Next, we print an f-string, which allows us to use variables. Inside the curly braces, we add the variable dog, but next to it inside the curly braces, we add square brackets followed by the number 0 in them. What this does is it takes in the variable and reads it, then the square brackets with 0 in it means that it will take the first character inside of the variable word.

We do the same thing on the next line but instead of the number 0, we use the number 2, which takes the last letter.

Running this code we can see that the results explain just that.

Another addition to String Index is that you can also do it backwards. Yes you heard me, instead of using 2 for the last character in the word, you can use -1 instead.

Upper And Lower Case

If you want to make a word all upper case or lower case or even an entire sentence, this method is perfect for you. Python offers a method to change cases.

loud = "No, I'm louder than you!"
louder = loud.upper()
print(loud)
print(louder)

As you can see, we have a variable that contains a string. We also have another variable that uses the other variable, loud and next to it is a method .upper(). This method turns all of the words into upper case.

Next we print the loud variable and then print the louder variable

As a result we can see that the second line is in all upper case.

For loops

For loops are an easy way to display individual text from a string and read text from a list.

An example of this is if you want to display the individual characters of a word.

print('s')
print('a')
print('m')
print('e')

Instead of displaying and printing your text on different lines, making your code messy, inefficient, and hard to read, you can use for loops instead, which is a lot easier, cleaner, and efficient way to do so

for letter in 'same':
  print(letter)

Here we use the keyword for, and any variable to call this. In this case, it is letter. Then we write in then 'same'

Then we will print letter in the following line. Something that you should notice is that the print isn't on the same line as the function, we increment it to the second line. This is very important, as Python picks up on any syntax errors as it is very sensitive to them.

As a result, we print the exact same thing except in a much shorter and cleaner way.

For loops can be used for many different reasons, for example. To create a shopping list, list out names and overall make things look cleaner and tidy.

Looping Over A Range

Looping over a range is a variable that allows us to print code in a range. If you don't get what that means then this example should clear things up.

Using the same concept as the for loop, we can just add in a range.

for index in range(3):
  print('Hey!')

This code lists a for and a variable called index, then we use the keyword range. In this scenario, we use the number 3 and what this code should do is then print 'Hey!' 3 times in the console

As we can see, it does.

Mini- Challenge

At the end of ever Section, there will either be a big-challenge or mini-challenge. These challenges are to see whether you learnt what I just taught you in a short and fun way.

Prompt:
You're the school's cheer leader, and want to practise. Write a program that takes in the team name, and prints it out as a cheer as shown below.

Your program should ask the user to input the team name, print out the team name in lower case, and then print each letter in upper case on a new line after the words 'Give me a '. After printing the letters, your program should print 'What does that spell?' and then the team name in upper case as shown below.

Example Output:

Team: Dolphins
dolphins
Give me a D!
Give me a O!
Give me a L!
Give me a P!
Give me a H!
Give me a I!
Give me a N!
Give me a S!
What does that spell? DOLPHINS!

Lets see if you can solve this problem.

Solution:

Team = input(f"Team: ")

print(Team.lower())

for letter in (Team):
  print("Give me a " + (letter.upper()) + "!")

print("What does that spell? " + (Team.upper()) + "!")

Here in this code, the first step is to create a variable that asks for the Team that they wish to cheer for. The next step is printing that team but in lower-case as it asks us to do so, and then we create a for loop. Inside this for loop, we ask for each letter in the variable, Team, and then we print out ("Give me a " + (letter.upper()) + "!") which just does exactly that and gives us each letter of that team name.

Finally we print out the team name at the end.


Conclusion


In conclusion, this blog has aimed to help not just me but you as you advance into python and learn new things on the way. In this blog, I have listed examples such as for loops,the basics, f-strings, upper and lower case, range and string indexing. Next part, I will teach you things about, strings and intergers, if statements, operators and more so stay tuned into learning in the next part.