So this week I attended Tejas Kumar's 1,2,3 show which had HackSultan as the guest. One of the tips for coders he talked was practise twice as much as you learn. So I'm going to be sharing the effect of this from a personal perspective. In this article I will talk about:
Two Python programs I wrote this week
The concepts behind the program
Importance of practice in coding
I learnt about the control flow, lists, dictionaries and for loops in Python this week and here's a brief:
Control flow: The control flow takes care of what code is run based on the conditions that are satisfied. So when using the control flow ,you are going to be using conditional statements like if, else, elif and some comparison operators too. For more details on the basics of Python, you can check here .
Lists and Dictionaries: The list data type allows you to a number of items all at once. It also lets you manage multiple strings in one group.
# Lists
countries = ["Nigeria", "Ghana", "Liberia"]
Dictionaries are used to manage groups of data . It makes use of key-value pair to form one element and are covered in curly brackets.
# Dictionaries
continents = {"Africa":"Nigeria", "Asia":"China", "Europe":"UK"}
For Loops: A for loop allows you to process elements of a list or dictionary with a temporary varible and apply some code to it. In for loops, elements of teh list or dictionary are assigned to the variable one by one and the code run with each assignment(iteration).
After learning about this concepts in Python, I decided to code hands on building a program for something I love which is cakes, it's actually good to start from things you love, there's a different kinda spice it comes with. Let's take a look at it here:
# Simple program that calculates cake prices with no. of cakes in console
#Price of cake is 10 dollars
cake_price = 10
money = 20
# Getting input for No. of cakes user wants
input_count = input("How many cakes do you want?:")
#Total price for no. of cakes
count = int(input_count)
total_price = cake_price * count
print('You will buy ' + str(count) + ' cakes')
print('The total price is ' + str(total_price) + ' dollars')
#Control flow
balance = total_price - money
if money > total_price:
print('You have bought ' + str(count) + ' cakes')
print('You have ' + str(balance) + ' dollars left')
elif money == total_price:
print('You have bought ' + str(count) + ' cakes')
print('Your wallet is now empty')
else:
print('You do not have enough money')
print('You cannot buy that many cakes')
The program basically helps a user calculate the cost of the number of cakes purchased. Using the control flow I was able to control what happens when the user doesn't have enough money to buy cakes.
Moving on I improved on the previous code with my knowledge of lists and dictionaries adding more feaatures. Take a look here:
# Create a dictionary with keys and values containing cake variety and price
money = 3000
items = { 'Velvet cake':500, 'Choclate cake':600, 'Vanilla cake':250 }
# Create a for loop that gets the keys of the items
for item_name in items:
print('Each ' + item_name + ' costs ' + str(items[item_name]) + ' naira')
# Allow user input number of cakes to buy using input() and assign to input_count
input_count = input('How many ' + item_name + 's do you want?:')
print('You will buy ' + input_count + ' ' + item_name + 's')
#Calculate how much for user's input by first assigning user input to count and converting to integer
count = int(input_count)
# Multiply the cost of each item and user input assigned to total_price variable
total_price = count * items[item_name]
print('The total price is ' + str(total_price) + ' naira')
# Control flow to compare the user's remaining money to the total price
if money >= total_price:
print('You have bought' + input_count + ' ' + item_name + 's')
money -= total_price
# Print money left in users wallet after buying cakes so modify the program to exit a loop
if money == 0 :
print('Your wallet is now empty')
break
else:
print('You do not have enough money')
print('You cannot buy that many ' + item_name + 's')
# Using the money variable and type conversion, print 'You have __ naira left'
print('You have ' + str(money) + ' naira left')
Looking at this program you can see the new features I was able to add using dictionaries, where it displays different varieties of cakes and different prices. Trust me it improves and gets better with time and practice. Now the jerk here is once I learn about a concept, I try as much to practice on with anything I love doing in this case I love eating cake so I coded a program that calculates cake prices.
The importance of practicing while learning cannot be overemphasized, here are some of the benefits:
You will grow and suppress all your previous achievements
Practice helps you understand the concepts better
It helps you improve on your mistakes
It also improves your ability to solve problems creatively
You should be convinced on how important it is to practice what you red or learn as you continue your tech journey. I will end with this quote
If you want to be the best, you need to practice which in return will give you better opportunities
- Micheal Garbade
For more details about why you should practice while programming, you can check here .
Thanks for reading, feel free to share your comments and tips!