Python Beginners
Python Beginners
Introduction
Chapter 1 Introduction to Python
Chapter 2 Understanding the Basics
Chapter 3 Coding 101
Conclusion
Introduction
So you want to be a programmer? You have been looking all over the web and even in
stores and see all of this great software that allows you to do a lot of cool stuff such as
games, office applications, popular web sites such as Facebook, Twitter and a slew of
others.
You start to use these programs and start to think to yourself that, wow I wish that this
did this, or that I wish this program could do that.
When you start to think like this that is when you have a desire to write your own code
and use that code to make improvements and eventually develop something that is totally
customized that it is a unique application in itself.
This can be a great thing and if you get really good at it there will be a lot of people who
will seek you out to develop programs and applications for them.
So where do you start? Well there is a lot of programming languages out there that will
allow you to do different things.
Some languages will allow you to develop games while others are more designed for
desktop applications. There may be a time when you want to develop a web application or
site or even a phone app that you can sell.
No matter what the end program is that you want to create there are a few fundamental
lessons and skills that you will need to learn.
When you learn these basic skills you will be able to move on to different languages,
apply these fundamentals to these languages and advance faster allowing you to create
better applications faster.
In this book we will be exploring the language of Python. For many developers or people
looking to become developers Python is a great language to learn.
With other languages you are required to learn a long set of syntax statements, learn how
to link complicated files together and as a result many people get frustrated and decide to
quit before they even get started.
In this book we will be targeting the basic programmer. We will walk you through the
basics so that you can grasp the concepts quickly.
We will also give you some examples and even locations where you can go to expand your
knowledge of the Python language as well as other languages you will want to explore.
So if you are ready lets jump in and start learning Python.
Chapter 1 Introduction to Python
Python is a great language to get started with. There are a lot of different people using
python to create amazing applications that will do some amazing things. For example
Industrial Lights and Magic used Python to create lighting effects for their movies.
The most recent movie that they worked on was the new Star Wars movie. There was
even a high school student who used Python to connect to Twitter and scan for specific
keywords in tweets. When a tweet was discovered the dinosaur would roar!
What is Programming?
Before we jump in and get too crazy with Python and what you can do with it I wanted to
first give you some basic information about what a program is and what being a
programmer really means.
To start out with a program is a piece of software that was written by someone to solve a
specific problem or perform a specific task. Different examples of a program would be
Microsoft Office, QuickBooks or even a computer game such as Need for Speed or Super
Mario Brothers. All of these examples are things that with coding experience as well as a
bit of creative flare can be created by a program.
What is a programmer?
A programmer is a person or even a group of people who know these skills. They are
people who find a problem that needs to be solved, break down the process and with the
different pieces of code assemble a program to perform that task.
What is a program?
A program is a collection of text files that are written by programmers that follow a
specific sequence or logic. These files are then converted into a machine language that a
computer or the end device can read and will perform the desired actions.
So if you wanted to write a program that sent you a message every time someone tweeted
a specific phrase you would write code that would search for this action and when it was
found perform your desired action.
Other Resources
Once you have downloaded and installed Python on to your computer you may feel lost
and unsure. What you will want to do is visit one of these two sites and look for code
examples that you can use in your programs.
Github http://www.gitbub.com
On Github you will find a repository of software code and examples that are yours to look
at and model your own applications with. This site is used by programmers to share what
they have developed and will allow you as a developer to add the functionalities they have
already done in your code.
When it comes to coding in any language there is a process of keeping your code clean
and not using or writing the same code over and over again. With a site like Github you
will be able to find code and save a lot of time in developing your applications.
Stack Overflow http://www.stackoverflow.com
On this site you will also find a lot of examples of code in a lot of different languages.
You can interact with others who supply code and when you learn how to do something or
even if you can do an improvement on existing code you can share what you have done
with the world.
Chapter 2 Understanding the Basics
When it comes to learning programming or developing software in general you need to
understand the basics. When it comes to the basics these are what all programs are started
on.
Your program can be a simple application that keeps track of customers and their order or
it can be as complex as a site like Facebook that has tons of users doing different things all
the time.
When it comes to the basics it all comes down to understanding your end goals as well as
the process in which you want to achieve that goal. One of the biggest mistakes new
programmers make is not have a set goal for their program and thinking too big too fast.
For example if you want to develop the next Facebook or Microsoft Office you wont do it
simply because you dont understand the basics and are trying to build something so
complex without first understanding the basics.
The next thing that people try to do is build the entire application in one big file. This is
not how programming works. When it comes to programming you want to break up your
program into as many small files as possible.
The reason for this is that each small file will be written to perform a specific task. These
tasks can then be connected together to form the entire program.
The reason you want to do this is two fold. The first is if you have your program in small
parts it will be easier for you to look into these pieces to find mistakes that you have
made.
When programming you will find a lot of mistakes. If you are a seasoned programmer
you will find a lot of mistakes. So to fix this issue we break down our programs into
smaller parts.
The second reason we want to break down our programs into smaller parts is so that we
can reuse the code in different applications. When we start out programming it is very
important to start keeping your code that you write.
You want to save this code so that one you can refer to it later to refresh your memory on
how things were done and how things have changed and two you can easily assemble
these pieces and parts together to build a foundation for another application that has the
same features you have already written and then just customize the new application as
desired.
Test, Test, Test
When it comes to programming it is gong to be a long road. The main thing that you will
want to do on a constant basis is to test your software that you are creating.
You dont want to start writing a huge application and even a small one that has lot of
parts and features without first testing the features you already have. This also applies to
code that you have already written.
When you start a program you want to work on one specific set of features at a time. So if
you are working on a login system for a web site you want to get that login system
working first before you start working on the areas the log in system are protecting.
Avoid Repeating Yourself
When it comes to programming one of the biggest things that happen is that people will
begin to repeat themselves. This will be writing the same block of code over and over
again perform the same or similar actions. What you will want to do is write the code
once and put it somewhere in order to access it later.
Functions
The next thing that we are going to talk about is functions. Earlier in this book we talked
about reusing code that you have already written or have used in the past to make our jobs
easier and keep us from writing the same things over and over again. The best way to do
this is by writing functions.
When we look at functions we looking at a something that similar to a variable but has a
lot more power to it. A function in essence is a block of code that performs a specific set
of commands and can be called anywhere in the program.
So for example you first need to give the function a name. For example if you want to
save information to a database that was collected from a web form you may want to call
this function something like ClientInfo.
When you name your functions you will also want to name them just as you would with a
variable. You dont want to use weird names or names that have nothing to do with the
purpose of the function. So for example you were collecting information for a client you
dont want to name your function CakeRecipies.
Modules or Classes
The final thing that I want to talk about are Modules or Classes. Depending on the
programming language that you are working with a module and a class are very similar.
A module or a class is a collection of functions. These collections relate to a specific area
of a program such as the Database class or any customized class that you feel will relate to
your specific program. For example if you are writing a game you may have a class that
deals with all of the animations. Then you may have another class that deals with user
input.
When it comes to putting all of these features together you can see how developing a very
complex piece of software can become simplified and with these features you can easily
write something once and use what you have written to create a wide range of different
programs that reuse the same features with one block of code.
Now that you have a basic understanding of code and coding in general we are going to
jump into chapter three with a clear purpose and learn the ins and outs of Python and
what you can actually do with it and actually start writing your first Python program.
Chapter 3 Coding 101
When it comes to coding there is a very basic concept that you need to understand. This
concept is each action in your program will require input and a result. So no matter what
type of program you are writing be it a simple math program or a complex multi-user web
based environment the basic foundation will be that you will need to give the computer or
program some form of input and that input will give you a result.
To begin with this we are going to write our very first program called Hello World.
Now for many of you who have programmed in any language before know that the Hello
World program is the initiation program that we all write when we first start
programming.
The reason that we write this stupid little program is two folds. The first is you will learn
how to write a basic program that will work giving you a result and even a sense of self
confidence that you can become a programmer and second it will test your system to
ensure that everything is working correctly and that if you do have a problem with your
code it can be looked at as a code issue and not a foundation issue.
To write the hello world program you will need to use the print() function. In the previous
chapter we talked about functions before and the print() function is a perfect example of
writing some code and reusing it in your program.
If you want to learn more about how the print() function works you can read more
documentation on the python.org web site. To use the print function you will need to load
up your python editor that you just installed and start a new project. Once this is
completed you will want to type in the following line of code.
Print(Hello World)
Now depending on the programming languages that you have written in the past this
syntax may be different than others. For example in C# or C++ you would have to end
that statement with a semi-colon. So instead of having it written like it is above you
would have written it like this.
Print(hello world);
Another thing that you will notice is that the text HELLO WORLD has a double quote in
one example and will have a single quote in the other . The reason for this is that the
Python language and interpreter does not require this in order to function. Another thing
that you will notice is that the statement does not end in a semi-colon. In languages such
as C# you will be required to end statements with a ; for it to work. If you dont then
these languages will crash since the statements that you create are invalid.
This is another reason why the Python language is such a nice one to start learning since
you are not bothered with these small details and can focus on having fun and developing
some cool software.
Now that you have written this code you will want to run the program. You can do this by
hitting the F5 key on your keyboard or clicking the green play button at the top of your
editor.
When this is done your program will compile down and run. When it runs you will see
the words Hello World on your screen. From this point forward you know that your
program works and you can have some fun by changing the text that you place on the
screen.
Commenting Code
The next thing that we want to go over when it comes to writing code is the ability to
comment your code. For many people starting out learning to comment your code is a
great thing to do. It will help you to remember what you were trying to do and even keep
samples of others peoples code in your code that will not be read by Python but can be
used to help you develop your own code.
When it comes to commenting code you can never comment enough. In fact the ore you
comment the easier it will be for others to read and understand your code. To make a
comment in Python you will wan tot use the hashtag. #.
So this is what a comment would look like.
# Program Name The Amazing Program