Chapter 1: Warm-Up Time
Well, first of all, my heartiest congratulations to you for picking up this book. If you have already completed the previous book, where I explained Python programming, double the wishes for yourself. The journey to Python is indeed one that is riddled with lines and lines of codes, waiting to be explored, understood and executed correctly.
We have gone through various chapters in the previous book and discovered so much about Python, starting from its history, all the way to the modern-day interpretation of automation, artificial intelligence and how things so advanced use Python.
We went through individual aspects of the language, such as the syntax, the variables, the data types, loops and functions, to count a few. All that is good, but the problem is we still don’t know if we are ready to take on more advanced courses and learn things far beyond the scope of the previous book.
Fortunately, I already gave this some thought, which is why I will be providing you with various methods to fine-tune your skills further I am eager to get started, are you? Then let us dive into the exercises straight away and find out just where we stand.
Solutions to all exercises and questions are within the last chapter. Only consult them when you have tried everything possible to come up with the answer and somehow failed to do so.
Sea of Questions!
These questions, while they may sound easy, are designed to revisit some basic elements of python. Some of these may have options, while others may not. Do not be intimidated by these questions. Try and answer as many of these as possible.
Q-1: From the options given below, identify which of these is written in Python?
Code-1:
using system;
var username = console.readline("Please enter your name: ");
console.write("Hello " + username);
Code-2:
<html>
<head>
<title>"Why write Python?"</title>
</head>
<body>
<p>
print("Hello World!")
</p>
</body>
</html>
Code-3:
import turtle
def my_function(name):
print(f"Hello {name}")
my_function("Sam")
Code-4:
var name = "Mr. Marvel";
console.log("My name is " + name);
I am sure you see some familiar things here. Take your time and analyze each of these closely. The answer is right there, all it needs is a keen eye to pick it out! It’s quite interesting to see that most of these seem to be using a familiar setup. It does take time for one to be fully familiar with the syntax, but once you are familiar, you should have no trouble figuring this out.
Visit any of the old exercises we did in the previous book. Try and match the way the code was written to the ones presented here. You should have your answer shortly!
Moving on to our next question, I promise it will be more questions and less of me, but I need to ensure I provide some explanations were needed to help those who may have picked up the book after a while, just to refresh their concepts.
Let us now begin a series of questions to test your understanding.
Q-2: How can you check if you have Python 3.8.x installed on your system?
- Check if you have PyCharm installed on your system. If so, you have the latest Python version installed.
- Run the command python --version
in PyCharm to check for the version.
- Run the command python
in the command prompt for Windows to check the version. Run python -v
on Mac and python3
on Linux to get the version.
- Visit the Python website to see if it can identify your version of Python.
Q-3: What is the language named after?
- Monty Python’s flying circus
- The reptile Python
- To honor an endangered species of Python
- Just a random name that caught on
Q-4: How does each line in Python end?
- With a colon ‘:’
- With a semicolon ‘;’
- With a full stop/period ‘.’
- None of the above
-
Q-5: What does the acronym IDE stand for?
- International Day for Electronics
- Integrated Developing Environment
- Integrated Developer Environment
- Integrated Developing Engineering
Q-6: How is a string represented in Python?
- With a single quotation mark ‘’
- With a double quotation mark “”
- With either of the above.
- None of the above.
Q-7: What is a variable?
- It’s a function in Python.
- It’s a method in Python.
- It’s a user-created container holding immutable values.
- It’s a user-created container holding values that can be modified.
Q-8: How would you print a string that says He said, “Yes!”?
- print(“He said, “Yes!”)
- print(f“He said, “Yes!”)
- print(‘He said, “Yes!”’)
- print(He said, Yes!)
Q-9: Running the code as shown, what will the output be?
num = '5' * '5'
print(num)
- 25
- 5, 5, 5, 5, 5
- ‘5’ * ‘5’
- TypeError: Can’t multiply sequence by non-int of type ‘str’
Q-10: If you run a code that ends up with an error, it will cause PyCharm to crash.
- True
- False
- Depends on the type of code written
- None of the above
Q-11: Which is the correct method to set the value for a bool ‘is_married’?
- “True”
- True
- ‘True’
- true
Q-12: Which of the following is a formatted string?
Code-1
:
name = "Jiovanni"
age = 41
print("Hi, I am name and I am age years old")
Code-2:
name = "Jiovanni"
age = 41
print(f"Hi, I am {name} and I am {age} years old")
Code-3:
name = "Jiovanni"
age = 41
print("Hi, I am " + name + " and I am " + age + " years old")
Code-4:
name = "Jiovanni"
age = 41
print("Hi, I am [name] and I am [age] years old")
Q-13: To name a variable called first name, which method is correct? You can choose more than one answer to the following question.
- firstname
- FirstName
- first.name
- first_name
Q-14: Choose one or more answers which apply. Clean-code practice is:
- To keep our workstations clean.
- To name our variables and functions appropriately.
- To improve readability.
- To ensure the code is not breaking any laws.
Q-15: Which of the following is the correct way to create a variable named ‘test’:
- def test():
- test.create
- test = “”
- print(test)
Q-16: What is a concatenation of strings?
- To merge two or more strings into a new string object
- To separate two strings
- To convert an integer into a string
- None of the above
Q-17: Choose the correct answer(s). Python is:
- The successor of ABC language
- Only operable through PyCharm IDE
- Used for automation and Machine Learning
- All of the above
Q-18: What is OOP?
- Object-Oriented Python
- Object-Oriented PyCharm
- Object-Oriented Programming
- Only On Python
Q-19: How can you acquire user input and store it in a variable called income, for calculation purposes?
- income = bool(“Enter your income:”)
- income = int(“Enter your income:”)
- income = input(“Enter your income:”)
- income = int(input(“Enter your income:”)
Q-20: Which of these is/are true regarding Python?
- There are two data types.
- Variables can be called, modified or removed.
- Python can work without PyCharm.
- Python is a case-sensitive language.
That was quite a little warm-up, wasn’t it? We have only just begun. You may wish to check how many questions you got right by referring to the last chapter and seeing where you stand. These 20 questions were random, and most of them did not involve many technicalities.
Let us reflect on where you stand as a beginner:
If you achieved:
20 –
Bravo! You answered each one of these brilliantly. That goes to show you were paying close attention to the book and the questions above. This is exactly the kind of result you should expect if you know your basics.
15 – 19
– So close to perfection, but do not let that bring you down. You did a fabulous job at answering these questions. Just revisit the ones you got wrong, and you should soon be polishing your skills to make it through with the finest results.
10 – 14 –
There is room to improve. You did answer some of these questions rather well, but you have missed out on a few critical topics. It is best to revisit the book and focus solely on the ones you got wrong. Go back to the concepts you missed and try to understand how the knowledge was applied here.
Below 10 –
I have no other way to put it besides saying you need to work on your skills. It is quite likely that you were distracted during the test or when you were reading the book. Do not be disappointed though, failure is a part of the learning curve. As long as you have the will to learn, and a passion for pursuing, you will soon understand the issues and be writing your programs like anyone else.
I cannot stress enough that it is only through practice that you will be able to become the kind of programmer you aim to be one fine day. Just going through the book and answering every question right, only to stop practicing right after, will not serve you with any purpose or advantage.
There are a few things you should always remember as a programmer:
●
Programming languages are constantly being updated. If you are out of action for a little while, you will soon be holding on to outdated knowledge.
●
You are only as good as your coding skills. Anyone who is better will replace you instantly.
●
Accuracy and speed of coding will differentiate between a good programmer and a struggling programmer, even if the latter knows everything.
Be sure to check out Python’s official website for any updates that may arrive in the future. Currently, these codes were developed using Python 3.8.0, and they might have already been updated by the time you read this book. Always stay current with the latest version of Python and PyCharm.
Now that we have had a little stretch, it is time to dive into some technicalities and immerse ourselves deeper into the world of Python.
Is This Correct? - Part 1
This section will test out your skills in understanding the scenario and identifying whether the code will work or if it needs to be modified. I will be presenting you with various scenarios in each chapter. They will grow in complexity and length, which is why it is a must that you read through each of the lines properly. Try not to copy and paste the code before you have come to a conclusion about the program’s ability to work or fail. Let your brain to do the work first. Train yourself and your mind to think, analyze and resolve issues efficiently and effectively. Relying too much on the IDE will never allow you to explore your potential talent and problem-solving skills truly
Q-1: The program shown below was created to display a concatenated string. Do you think that the following will work? Will this deliver the required output of “This will be added With this!” or would it produce completely different output? If so, why?
string1 = "This will be added "
string2 = "With this!"
print("string1 + string2")
Q-2: The following is a program, written by a skilled programmer for a local business called “Pete’s Garage” which should provide the business with a more reliable way of dealing with things. The program is written as shown below:
print('Welcome to Pete's garage)
name = input('Please enter your name: ')
job_number = int(input('Please enter your job number: '))
repair_cost = 100
discount = 15
total = repair_cost - discount
print("{name}, the total for {job_number} is ${total}")
print('Thank you for your business')
Will this program work? If not, can you identify the error that might cause this program to crash or stop responding?
Q-3: A university student decided to piece together a program that will allow potential online students from abroad to fill out the form and seek out further information regarding courses they’re interested in. The form looks like this:
#Online Registration Form
print("Welcome to ABC Uni!")
print("Please enter the required information to begin.")
s_f_n = input("Enter your name: ")
phone = int(input("Enter your phone number: ")
em = "Enter your email: "
crs = "Choose your course:
The student has asked you to review the program and find out if there are any issues that need addressing. Find out what is wrong with the code and correct the issues.
Q-4: A student has created a program for a login page at a library for the new batch of users that have just joined. After a brief introduction, all users are asked to create their username and password pairs. After the users have entered their passwords, each password is compared to ensure that it matches. The program looks like this:
username = input("Username: ")
password = input("Password: ")
print("You have entered the following:")
print(username.lower())
print({password.lower()})
print(password==password.lower)
What seems to be the problem here? Why do you think the code will not work? What can be done to ensure that the program starts functioning?
Q-5: A programmer, with intermediate experience and knowledge, was asked to type out a program that would print out Boolean values for every grade a student acquires. For grades from A to B, the prompt was to print out True, while others would be considered False. Have a look at this code and see if this will work:
grades = ["A", "A", "B"", "U", "F", "E", "D"]
for x in grades:
if x == A or x == B:
x = True
if x == True:
print("Pass")
else:
x = False
print("Fail")
The student has claimed that the code worked exceptionally. It is now up to you to analyze the code without testing it first to deliver your first impressions.
Bonus: Try and modify the same code with your own values and come up with something even better to practice.
Q-6: A string named initial_message contains the following message:
“
Hi, I have just taken part in the course. I hope that I will be a programmer one day.
”
Without the quotation marks, a programmer was asked to find out the length of the string. She used the following method to do so:
String.length(initial_message)
Is this the correct method to check the length of a string? If not, what is the correct method applicable here?
Once you are done with these exercises, I do believe you will be in a better position to understand and analyze your understanding of the basic data types, the syntax, and the way variables work. I did add a few situations which were not exactly dealing with data types and variables; however, expect such questions and problems to come up in the future as well. Now, you should cross-check your answers with the last chapter to find out how much were you able to get and if you solved them correctly.
For those who have managed to score more than 70%, good job! For those who are struggling, I would recommend going through the first book and revisiting the data types, variables and input methods to refresh what you may have forgotten. Once again, there is no shame if you were not able to grasp the concept the first time.
Programming does take time and sometimes, even the best programmers spend days trying to figure out what is causing their program to crash, just to find out they may have missed out a single quotation mark, a comma, or misspelled a variable or a function. This is genuinely the case and happens almost every day.
It is with the introduction of PyCharm that our lives have been made so much easier. Imagine having to write entire programs, as long as 1000 lines, on notepad, which does not even come with the capability to check the spelling, let alone identify errors for any language after typing.
Use the power of IntelliSense, a technology used within PyCharm, that allows us to complete our codes with the click of a button. While that is time-saving, I would still recommend that you type out the complete code. If you make a habit of using PyCharm’s IntelliSense, you might find yourself struggling should you join a firm that relies on Microsoft’s VS Code IDE. It is always best to write the codes completely and proof-read the same where possible before you decide to execute them.
Now, we shall move on to our second chapter. We have revised and revisited the most basic concepts and principles of Python within this chapter. It is time to take the next step and start practicing with user-inputs, storing values and recalling them. We will also look into matching information, and lastly, we will come across the first project that I would recommend every reader to go through and complete on their own.
All projects within the book have no definitive solutions. Apply the knowledge that you have gained so far to add to these projects and make them more complex and interactive.