Loading document…
Opening in Pages for Mac...
Your browser isn’t fully supported.
For the best Pages for iCloud experience, use a supported browser.
Learn More
Cancel
Continue
PYTHON - A
SIMPLE
INTRODUCTION
1
Contents
Contents
INTRODUCTION
•
What is Python?
•
Why Python?
•
How to learn to code?
•
Why should you read this blog?
•
Where do you write Python code?
Chapter 1
•
Incorrect code
•
Syntax Highlighting
•
The Print Function
•
The Input Function
•
Variables
Chapter 2
•
Strings and Integers
Contents - Continued
2
•
Float
Chapter 3
•
If, Elif and Else Statements
INTRODUCTION
What is Python?
Python is a multi-purpose, programming language. It is
easy to learn and is quite useful for beginner
programmers. Python is easier to understand and
comprehend, because the syntax is more like English
and the indentation is legible. Adding to its simple
code, Python has some features that can enable you to
show simple animations. Those can be used to make
games.
Why Python?
Adding to its facility,
According to TIBOE Programming
Index, Python is ranked 3rd in the most used
programming languages
. If you want to see how to
make a game on Python or something like that,
when
you search it up on Google, you will most probably fi nd
a
t least many
suitable tutorial
s on the fi rst Google
search page
, as Python is a popular language. Python
has won Language of the Year in 2007, 2010 and 2018
which makes it more fun to learn.
3
How to learn to code?
If you have this blog, learning Python will be quite easy
for you. Luckily for you, this blog will start with the
basics and gradually get harder. Just follow the steps
and you will be an expert in no time.
Why should you read this blog?
Anyone who likes computer programming should read
this, whether that's a child or an adult. If you want to
learn how to write your own software rather than using
someone else’s, this blog is the perfect blog to read. In
the next few chapters, you will learn the basics, like
where to write your Python code, to a bit more harder
things, like the different kinds of loops and if
statements, and how to use them.
Where do you write Python code?
The fi rst step to producing a fabulous software is
fi nding a suitable place for writing your code. There are
many free IDEs that you can use.
One of them is Python IDLE.
When you install Python,
IDLE is also installed by default. This makes it easy to
get started in Python. Its major features include the
Python shell window(interactive interpreter), auto-
4
completion, syntax highlighting, smart indentation, and
a basic integrated debugger.
IDLE is a decent IDE for
learning as it's lightweight and simple to use. However,
it's not for optimum for larger projects.
Another one is Atom.
Atom is an open-source code
editor developed by Github that can be used for Python
development
.
Atom is highly customi
s
able. You can
install packages as per your need. Some of the
commonly used packages in Atom for Python
development are autocomplete-python, linter-flake8,
python-debugger, etc.
The one I prefer to use is
repl.it
, it is used for small and
big projects and has most of Python IDLE functions.
5
Chapter 1
Incorrect code
To start writing your fi rst line of the code, click the fi rst
line of code. Now write whatever you want to write on
the line and press
run.
Luckily for you, if you write a
code which doesn't make sense, you will see an error
like
SyntaxError : invalid syntax
in red in the console.
There are other types of errors like the
UnboundLocalError
, but the most common error right
now will the
SyntaxError
.
Syntax Highlighting
Syntax Highlighting is a feature of text editors that
make it easier to read the code. So different code parts
will have different colours.
The Print Function
The most basic function in Python is print.
It is a simple
function that just writes whatever is in the brackets in
the console. For instance, the way to say
`
Hello World!
`
in Python is
`
print(
"
Hello World!
"
)
`
. If you write that in
code, the output would be
`
Hello World!
`
.
Have a go at
saying `Hello World!` to Python in your code . Try
6
running your code now by pressing the run button. The
output should look like the picture below.
### picture
If it does, then well done! You have written your fi rst
line of proper Python code!
The Input Function
Input is another common function in Python. It is used
to ask the person testing out your code a question. For
example, if you want to ask, 'How old are you?', you
would write, 'input("How old are you?")'. The picture in
the next page shows how the console would look like
after you ran the code.
The picture doesn't have the orange triangle because the
console is waiting for an answer, which means that the
code hasn't fi nished. The picture below will show the
code after it has fi nished.
After you answer the input code, then the line of code is
offi cially fi nished.
Variables
7
Variables are words that have values assigned to them.
The values could be numbers, lists, strings, etc. An
example of a variable is the picture below. In the
picture's case, the variable is hello, and the value is
'Hello World!'. Now if you want to print 'Hello World!',
you can just write the code in the picture below and
`print(hello)`, bearing in mind that because hello is a
variable, you don't put speech marks around it.
Now if you run the code, the result will be the same as
the time you wrote the code 'print("Hello World!")'.
Another example of a variable is 'ask = input("How old
are you?")'. If you type up that code, the result will be
the same as your fi rst input statement. As I said,
variables can also be numbers, so if you want to assign
the variable 'one' the value of the number 1, you will
have to do the code in the picture below.
After you copy the code, if you have copied it correctly,
you shouldn't see any errors, and the console should
look like the picture below.
8
You didn't do anything like 'print(one)', so the console
should be empty like the picture above.
Chapter 2
Strings and Integers
Strings are the code inside the speech marks in Python.
Integers are whole numbers, without the speech marks.
Let's carry on from the statement in the previous
chapter, 'one = 1'. If the statement would have been 'one
= "1"', the variable one would have been a string, as it
was in the speech marks, but since 1 wasn't in the
speech marks, 'one' is an integer. A simple way to fi gure
out if the variable is an integer or not is by writing the
code 'print(type(one))'. If the output is '<class 'int'>',
the variable is an integer. If the output is '<class 'str'>',
the variable is a string. Other output mean other stuff,
but we will talk about that later. Now if you want to
9