0% found this document useful (0 votes)
60 views

Learn Programming-By-Marinsborg

This document provides an introduction to programming concepts for beginners. It includes a table of contents, sections on who should read the book and how to think like a programmer. It then covers key programming topics like algorithms, flowcharts, sequence, branching, loops, variables and data types. Finally, it provides instructions on installing Python and using it to solve sample tasks to practice programming concepts. The overall document serves as a guide for learning basic programming fundamentals.

Uploaded by

jpark112105
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Learn Programming-By-Marinsborg

This document provides an introduction to programming concepts for beginners. It includes a table of contents, sections on who should read the book and how to think like a programmer. It then covers key programming topics like algorithms, flowcharts, sequence, branching, loops, variables and data types. Finally, it provides instructions on installing Python and using it to solve sample tasks to practice programming concepts. The overall document serves as a guide for learning basic programming fundamentals.

Uploaded by

jpark112105
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

2

Table of contents
Table of contents
Introduction
Who should read this book?
How to think like a programmer?
Introduction to programming
Algorithm
Flowchart
Sequence
Branching
Loops
Variables
Data types
Arithmetic operators
Relational operators
Boolean operations
Introduction to Python
Installation instructions for Windows
Installation instructions for macOS
Installing a code editor
Writing code with Python
Solving Task 1 with Python
Solving task 2 with Python
Solving task 3 with Python
Tasks for practice

marinsborg.com
3

Introduction
Do you want to learn to program but have no idea how to start? You saw that there are so
many programming languages and don’t know which one to pick? You are a bit afraid
because this looks overwhelming to you?

Don’t worry, this book will help you.

The IT industry is growing rapidly over the last decade and it affects our lives on a daily
basis. Most people are using smartphones and social networks. Communication is now
done with the internet. Devices are getting their "smart" versions - TV, watch, fridge, car, etc.
Almost every brand has its website. Some clothing brands decided to reduce the number of
physical stores and do business only through webshops.

Because of that, there is an increase in demand for people in the IT industry. Programming is
related to a lot of those jobs.

marinsborg.com
4

Who should read this book?


This book has one goal and it is to give you a quick introduction to programming
fundamentals.

After you finish reading this book, you will be able to write your first program without any
trouble. We guarantee that or we are giving your money back :)

This book is for everyone who is interested in programming. It does not matter if it is a
career choice, a new hobby or you are curious about how things work.

There is no prior knowledge required. It does not matter where you are from, how old you are,
or what your level of education is.

Programming is a skill like any other. It takes some time till you get the basics and then the
more time you invest, the easier it is. We will explain things in the simplest possible way. We
are using layman's terms to not bother you with terminology. We are also going to skip
details that will distract you.

This book is free and it always will be. You will only need to install Python and a code editor
which are free.

marinsborg.com
5

How to think like a programmer?


This is a common question that people have when they become interested in programming.
First, we need to define what computer programming is.

Computer programming is the process of designing and building a computer program. A


computer program is a collection of instructions that are executed by a computer to perform
a specific task.

In layman's terms, programming is telling a computer what it needs to do. And to tell the
computer what it needs to do, we use algorithms. An algorithm is a set of step-by-step
instructions used to solve a specific problem. The most common use of algorithms is in
computer programming and math.

We are sure that you have already used or even written some algorithm in your life - if you
ever prepared a meal based on some recipe. In that analogy, making the meal is a problem
that you need to solve. The recipe is an algorithm that defines a set of instructions. And you
are the computer that follows and performs instructions from the recipe. Next time you see a
cookbook, you will know that it is a book of algorithms.

Now let's practice. Open your favorite text editor or take a pen and paper and write down all
the steps you need to do to make a bowl of cereal (like a breakfast). After that, you will
compare your solution with ours. Continue reading this only after you write down your steps.

marinsborg.com
6

OUR SOLUTION:

● go to the fridge and open its door


● take out a bottle of milk and put it on the table
● close the fridge door
● go to a cupboard (or cabinet) and open its door
● take out a cereal box and bowl and put those on the table, next to the milk (this step
depends on where you keep bowls and cereals)
● close the cupboard door
● go to the drawer with spoons and open it
● take one spoon and close the drawer
● go to the table, put the spoon next to a bowl
● grab the cereal box and take the plastic bag out of it (let's assume that cereals are
inside a plastic bag)
● pour cereal out of the plastic bag into a bowl until the bowl is half-full or one-third full
● put the plastic bag back into the cereal box (or throw it away if it is empty)
● grab the bottle of milk and open it
● pour the milk over the cereal that is inside the bowl until the bowl is half full
● close the bottle of milk and put it on the table

And that is it, the breakfast is ready. Now, compare your solution with ours. If you have fewer
steps than we do, then you probably missed something. And even if it seems obvious, you
need to write it down because that way you will have a precise and detailed set of
instructions to make breakfast.

So now it is time to answer a question from the beginning of this chapter - How to think like a
programmer? When programmers face a challenge or a problem, they try to split that
problem into smaller, already familiar problems.

marinsborg.com
7

Introduction to programming
In the previous chapter, you learned what is programming and what is an algorithm. In this
chapter we will define how to use an algorithm, what is a flowchart, and its building blocks.
After that, we will solve some simple tasks using both algorithms and flowcharts. At the end
of this book, we will show you how to implement all those algorithms using the Python
programming language.

Algorithm

The advantage of learning to write algorithms is that the algorithm is independent of any
programming language. That means that it is easy to understand even if a person does not
have any programming knowledge.

If you know how to solve a problem or a task using the algorithm, you will be able to
implement that solution using most programming languages.

The algorithm has the following characteristics:

● An algorithm can be written in any language, but people usually write it in English.
● Most algorithms have some kind of input. Usually, it is the user’s input or an output of
some other algorithm.
● Most algorithms produce some kind of output. It can be text, file, video, etc.
● Algorithms must be clearly defined (unambiguous) - every instruction must have only
one meaning.
● Every algorithm must finish its execution after a finite number of steps.

marinsborg.com
8

Flowchart

A flowchart is a graphical representation of an algorithm. Each flowchart consists of its


building blocks. The flowchart shows steps as blocks or boxes of various shapes. Those
boxes are connected using arrows (also called flowlines). Flowcharts are good if you are a
visual type of person.

Let’s define the building blocks of flowcharts.

Some other symbols exist, but these are the most important ones. Let’s learn how to use
them by example. For additional clarity, every example will be solved with both a flowchart
and an algorithm. When solving your own tasks, you can always choose one or the other
based on your preference.

marinsborg.com
9

Sequence

Instructions in programs are executed in the sequence in which they are written. Let's create
a flowchart and an algorithm for a simple task. The user will input two numbers and the
program will print out a sum of those two numbers.

Branching

If some part of the algorithm needs to be executed only in case some condition is fulfilled,
then branching is used. With branching, you can split the algorithm into two or more paths.
One path will be executed in the case that the condition is true and the other path will be
executed only if the condition is false. To show an example of branching, let's create a
flowchart for a program that lets the user input a number and then check if the number is
positive. The program needs to print "Positive" or "Negative" based on the number.

marinsborg.com
10

Loops

Sometimes in code, there is a need to do the same thing several times. There are two
choices. One is to write the same code several times and the other is to use a loop. There are
numerous problems with writing the same code several times. First of all, it is
time-consuming. Apart from that, the code becomes more error-prone, harder to understand,
and harder to change. Also, most of the time you don’t know how many repetitions you need
in advance. That is why loops are used.

In computer programming, a loop is a sequence of instructions that is repeated until a


certain condition is reached. Most of the time a loop is used with some kind of a counter.
This lets the loop know how many times it needs to execute the same code and when to
stop. Let's create a flowchart for a program that prints all numbers between 0 and 10
(including both numbers).

In the flowchart below, you can see that a piece of code is repeated as long as the counter is
less than 11. As soon as the counter becomes 11, the program will end. Always remember to
increase the counter when you are creating loops.

Variables

In previous tasks we always mentioned variables, but we never explained what they are.
Variables are locations in memory that have some name and in which the computer saves
data from the input. The value of each variable can be changed during program execution.
To access the value of a variable, we only need to write its name.

Each variable has a name, a value, and a type. We will talk about data types a bit later. To
assign a value to a variable, we need to write the name of the variable, then the equals sign
'=' and then the value.

marinsborg.com
11

For example:

To assign a value of 10 to a variable with the name 'age' we just need to write age = 10.

If we want to change the value of the variable 'age' we can do it in the same way age = 30.
This is called re-assigning.

It is always a good idea to name variables in a descriptive way instead of using just one
letter like 'A' or 'x'.

Data types

In computer science and computer programming, a data type or simply type is an attribute of
data that tells the computer how the programmer intends to use the data. We will not bother
you with the details, you just need to remember these five common data types:

● Integer (int) - this data type is used for whole numbers. For example, int age = 20
or int size = 10
● String - this data type is used for text or sequence of characters. For example,
string name = "John Doe" or string sentence = "Today is a sunny day." A
string is always surrounded by quotation marks.
● Character (char) - this data type is used for a single letter. For example,
char letter = 'a'
● Float - this data type is used for numbers that contain a decimal point. For example,
float number = 3.14
● Boolean (bool) - this data type is used for True or False only (yes or no, 0 or 1). For
example bool isNegative = True

As we mentioned before - Each variable has a name, a value, and a type. When you write

int age = 10

int is the type of the variable, age is the name of the variable, and 10 is the value of that
variable.

Arithmetic operators

In programming, you can use arithmetic operators between variables or some concrete
value. Addition, subtraction, and multiplication are the same as in math, but the division is a
bit different.

For example, you can write this:

sum = a + b => this will save the sum of values a and b in the variable sum

c = d - 7 => this will save the result of the subtraction in the variable c

result = 15 * 3 => this will save 45 in the variable result

marinsborg.com
12

There are three 'types' of division:

x = a/b => this is regular division

y = 13 DIV 5 => this is integer division and it will save 2 in the variable y

z = 13 MOD 5 => this is the remainder of a division and it will save 3 in the variable z

Relational operators

In computer science, a relational operator is a programming language construct that tests


some kind of relation between two entities. These include numerical equality (e.g., 5 == 5)
and inequalities (e.g., 4 >= 3).

The result of the evaluation is either true or false. Relational operators are used for
branching, which we explained above.

Operators are: equal to (==), not equal to (!=), greater than (>), less than (<), greater than or
equal to (>=), less than or equal to (<=).

Note: '=' is an assignment operator, which saves a value in a variable; '==' is a comparison
operator, which compares two values for equality.

Boolean operations

Boolean operations come from Boolean algebra, in which the values of variables are either
true or false (1 or 0). We don't want to bother you much with Boolean algebra, but there are
three main operations you need to know about:

● AND - conjunction - the result of this operation is true only when both conditions are
true, otherwise false
● OR - disjunction - the result of this operation is true when either of the conditions is
true
● NOT - negation - this operation inverts the value of the condition. If the condition is
true then negation will result in false and vice versa.

Boolean operations are also mostly used for branching and can be combined with relational
operators. For example, if you need to check if the number is lower than 50 and it is not 7,
then you would do that in a flowchart like this:

marinsborg.com
13

Introduction to Python
In the previous chapter, we covered all fundamentals that you are required to know to start
programming. In this chapter, we will show you how to apply that knowledge by solving a few
tasks using the Python programming language.

Python is one of the most popular programming languages in the last decade, especially for
beginners. It has a large number of libraries that can be used for various branches of the
industry. It's also extremely popular for QA automation tasks. It's a very easy language to
learn: it uses indentation instead of curly braces and it does not need to use semicolons.
Since it's easy to learn, it's often the language of choice for people that are not programmers
- like scientists, mechanical engineers, accountants, etc.

Installation instructions for Windows

Now it is time to install Python. But first, you should check if you have it installed on your
computer. Open Command prompt (by clicking start and writing Command prompt). Inside
the Command prompt write 'python' and press the Enter key. If you have Python installed,
then its version will be printed. You’ll get something like this:

If you get something like this then, you can skip this chapter and go to the next one. If you
don’t have Python installed, then go to Python's official website and download the latest
version. Once you downloaded the exe file, just double-click it to start the installation.

Make sure you check "Add Python to PATH" like this:

marinsborg.com
14

Now go back to the Command prompt and enter "python" again. You should get the result
from the image above. That is it, you have Python installed!

Installation instructions for macOS

macOS comes with Python 2.7 pre-installed. However, Python 2 is obsolete and should not
be used. To install Python 3, visit Python's official website and download the latest version.
Once you downloaded the pkg file, just double-click it to start the installation and follow the
steps in the wizard.

After the installation, open Spotlight (Command + Space) and type "Terminal". Once the
Terminal is opened, type "python3" (no spaces) and press enter. You should see something
like this:

This means that Python 3 was installed successfully, so you can close the Terminal and
continue with the next chapter.

Installing a code editor

You can use any code editor to write Python code and we recommend using Visual Studio
Code. If you don’t have it, go to the official web page, download it and install it. After you
installed Visual Studio Code, install the Python extension for it. To do that, open Visual
Studio Code, and on the left-hand side, menu click on 'Extensions'. In the search bar write
'python'. Click on install on the extension with that name.

That is it, you are ready to start programming!

marinsborg.com
15

Writing code with Python


So far you learned programming basics with flowcharts and algorithms, you installed Python
and you installed a code editor. Now it is time we teach you some Python syntax before we
start solving tasks with Python.

The first thing you should do is open your code editor and create a new file. Visual Studio
Code will give you the option to create a text file or a Python file. Select Python file. If you
don’t have that option, don’t worry, you can always convert the text file into a Python file by
changing its extension to .py.

The next step is to save the file. Go to File > Save As… Then select any folder on your
computer where you would like to save that file. Also, set the file name to something and
make sure there is a .py extension at the end of the file name.

Now that you have the Python file ready, it is time to do a programming tradition and print
Hello World! first. To print something with Python you just need to type the function print()
and inside brackets write what you want to print. So to print "Hello World!", you would type
print("Hello World!")

After you typed that, press CTRL+F5 on your keyboard to run the program. In the terminal
below the code, you will see that Hello World! is printed. You will notice that it did not print
quotation marks. That is because in programming quotation marks are used to define
strings. Otherwise, the program would try to find two variables Hello and World!. This would
fail because we have not defined them. The quotation marks tell the program that these are
not variables, but a string of characters.

I’ll give you a couple more examples of the print function’s capabilities:

● print(10) - this will print the number 10.


● print(10 + 5) - you can do arithmetic operations inside print. This example will
print 15.
● print(test) - if you have a previously defined variable called test, then this will print
its value; otherwise, you will get an error.
● print("Hi" + "There") - this will print HiThere. You can use the '+' sign to
concatenate (connect) two strings together.

Another function you need to know about is input()

The input() function waits for the user’s input on the keyboard until the user presses the
Enter key. Users can type text and numbers, but the input() function treats everything as a

marinsborg.com
16

string. Once the Enter key is pressed, that input will be saved to a variable. To save the user’s
input into variable A, you simply need to write A = input()

If you want to print a message to the user before his input, you can do that inside brackets of
the function. So you will have this: A = input("Enter your name:")

NOTE: Everything that the user enters will be converted to a string. This is a problem when
you only expect that the user will enter numbers. When you are sure that the user will enter a
number, you will need to convert that string into an integer. I will show you an example in the
task below.

Solving Task 1 with Python

It is finally time to solve a task with Python. You can return to the "Sequence" section and
check the algorithm of task 1. After that, try to implement it with Python. Did you notice
something weird? Here is the solution for task 1:

A = input("Please enter the first number: ")


B = input("Please enter the second number: ")
SUM = int(A) + int(B)
print(SUM)

As you can see, there is an int() function. That function converts a string to an integer.
Since we know that the input() function will save the user’s input as a string, we need to
convert that input into a number before doing any mathematical operations.

Solving task 2 with Python

To solve this task, you need to know how branching is done in Python. The most common
way to do branching in Python (and many other languages) is using the if and else
statement. Every if statement needs to have some condition. Under the if statement you
write a block of code that is going to be executed only when the condition is true. If you also
need to execute a block of code when the condition is false (like in task 2), then you need to
use the else: statement.

A block of code is several lines of code grouped together. To create a block of code in
Python you use indentation (white space). Usually, you press TAB to create an indentation in
the code editor which will shift the code to the right. If you need to have a block of code
inside a block of code, you can do that by adding more indentation.

marinsborg.com
17

Now when you know all of this, try to solve task 2. The algorithm will help you. Here is the
solution:

A = input("Please enter a number: ")


if int(A) < 0:
print("Negative")
else:
print("Positive")

Solving task 3 with Python

There are two loops in Python - for loop and while loop. We will show you how to use the
while loop. As we already explained, loops are used when you need to execute the same
block of code multiple times. Loop will continue to execute as long as the condition inside
that loop is true.

Here is an example of a while loop:

counter = 0
while counter < 5:
print(counter)
counter += 1

The loop above will execute the block of code as long as the variable counter is less than 5.
This block of code prints the value of the counter variable and increases the counter value by
one. If the counter value is not increased every time, then the loop would run forever.

NOTE: counter += 1 is a shorter version of counter = counter + 1 . It just increases the


value by one. You can also do a counter -= 1 to decrease the value by one.

marinsborg.com
18

Sometimes you need to finish (or terminate) loop execution regardless of the condition. In
that case, you can simply write a break statement. In that case, a block code in the loop will
stop executing and the next line outside of the block will be executed.

There is a special statement that will always evaluate to true: while True. This is used
instead of a comparison with a counter when you have no idea how many times the loop
should run. Since you don’t want it to run forever, you will have to create some end condition
and use the break statement to exit the loop.

while True:
user_input = input("Enter a number:")
if int(user_input) == 13:
print("You've guessed my lucky number!")
break

This program will keep asking for input until 13 is entered. After that, the program
encounters the break statement and exits. Since all user input is saved as a string, we need
to use the int() function again.

Now try to solve task 3 by yourself. Check the algorithm, it will help you.

Here is the solution:

counter = 0
while counter < 11:
print(counter)
counter += 1

Tasks for practice

Let’s solve some more tasks for practice. This time we will not write algorithms, but you can.
These tasks will be simple and can be solved using the knowledge from the previous three
tasks.

Practice task 1 - Write a Python program that prints all the numbers from 1 to 100 (including
both) in descending order (100, 99, 98, etc).

Practice task 2 - Write a Python program that allows the user to input two numbers. If the
sum of those two numbers is bigger than 50, then the program should print "BIGGER",
otherwise it should print "SMALLER".

Practice task 3 - Write a Python program that is a guessing game. Your program will
generate a random number and save it to a variable. It will allow you to input numbers and it
will tell you if your guess was lower or higher than the generated number. Once you guess it
right, it will congratulate you and finish. Here are some tips for this task:

marinsborg.com
19

● to generate a random number in Python, you need to use two things: at the top of
your file, you need to write import random. This will import a module. Modules in
Python are files that contain functionalities that you can use when you need them.
The function to generate a random number in some range is
random.randint(1, 10) - this will generate a random number between 1 and 10.
Don’t forget to save it to a variable.
● use break inside your loop once your guess is correct

These are practice tasks that you can solve once you are done with this book. Practice task
3 is a bit complex but you can still solve it. Create a flowchart and algorithm for it and make
sure all cases are covered. If you run into any problems or you have a question, you can
always use Google to find answers or you can ask us.

Solutions for these three tasks are hosted on our GitHub. Have fun!

Next steps
This book gave you the knowledge required to start programming. Now it is time for you to
be curious and explore the rest of the programming world. If you like Python and want to
continue learning it, there are so many free courses on YouTube available.

Programming is not as hard as it might seem. You need to be patient with yourself, and
invest some time and effort in understanding the basics. No matter what programming
language you choose, try to solve some simple tasks first. After that, progress towards some
harder tasks. You might get inspired to create your own projects.

That is it for now, don't forget to follow us on social media to stay updated.

For more similar and free content you can visit our website.

marinsborg.com

You might also like