Você está na página 1de 23

Python:

Python Programming For Beginners - The


Ultimate Crash Course To Learning Basics
Of Python In 24 Hours Or Less!
Table of contents

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.

What language should I choose?


Well for this book we will be exploring the language of Python. The reason we are doing
this is because it is a very basic language that has a low of power and will give any new
programmer the basics of what they need to do to build a program that will actually have
some use and functionality.
Besides Python there is a slew of other languages that you can look at and learn. There
are languages such as JavaScript, PHP, Visual Basic, C++, C# and hundreds if not
thousands of others.
When deciding on a language you will want to look at what these languages have to offer
and what will be the best for you and your project. But before you do this you will want
to learn the basics and Python will get you there.

Where to get Python
When it comes to Python there are a few places that you can get it. The main place that
you will want to go is here - https://www.python.org/. When you visit this site you will
have everything you need to know about Python at your fingertips. You will be able to
download the Python program, have access to documentation and even access updates and
code has been written by other developers.
When you visit this site you will want to click on the Download link. There you will want
to locate the specific version of the software and install it on your computer. From there
you will be able to launch the program and start developing your very own Python
applications.

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.

Foundations of a programming language


Now that we have talked about the basics lets get our hands dirty. In this section we are
going to talk about the foundations and parts of a programming language in general and
then we will jump into specifics for Python. In the next chapter we will start to develop
some programs with Python and put these skills to work.

Programming Pieces and Parts
When it comes to a program and any programming language there are some basic features
and functionality that are built into the languages themselves.
When the compiler or interpreter for this language reads your code it turns it into machine
language that you as the end user can interact with.
When it comes to programming there are several main parts. The first is the variable.
The next is a function, a class and then modules. From here there are several other parts
and pieces but once you learn these basic parts you can start to develop programs.


A Variable
When it comes to variables these are open pockets that can be filled with information or
assigned specific information by the user or programmer. A variable can be anything that
you want it to be but should reflect the type of information that you want to contain in it.
When it comes to variables many people will use variables to store things like a customers
name, address, city, state, phone number, e-mail address or anything that will be changed
from user to user. Another variable can be assigned by the programmer to keep a static
piece of information in the program that cant be changed by the user due to it being hard
coded.
For example a programmer can create a variable such as $MainNumber. This
$MainNumber variable is something that is internal to the program and can be maniputed
within the program depending on actions and conditions or can remain the same
throughout the entire process.
In many languages a variable is started with either a $, _, or letter. In Python when you
create a variable it will start with a letter and can contain anything from letters numbers or
_.
When creating a variable you want to have some logic to it. For example if you want to
have a variable that will be used to store the first name of a client you dont want to name
your variable something like ddh889 or something like Joke. You want ot name your
variables for what they are. So for this example FirstName or ClientName would be a
great variable to use.

Assigning a variable
When working with variables you can assign information to that variable. So for example
if you wanted to assign the name Steve to the customer name you would write something
like this.
CutomerName = Steve
When assigning variables you can do it in a number of different ways. For example if you
were going to do a math calculation you can do something like this.
Answer = a + b
Now in this example the variable name is Answer. The letters A and B are also considered
variables as well. So if A = 5 and B =6 then you would get Answer = 11.

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

What can you comment?


When it comes to commenting you can comment on anything and should. Some of the
main reasons to comment are to,
Document ideas that you want to put into your program
Take credit for the programs that you are writing
Keeping track of positions in your program where you want to add a feature
Hide code that you have written in order to try something new and not have to
rewrite the same code over again
And many other different reasons. The reason to code is broad and should be used
everywhere and anywhere you can.

Importing Modules, Classes and Functions
Now that you have a basic idea about how stuff works and how we will want to work with
different files in our code it is time to talk about how to connect these files together and
work with the information contained within.
When it comes to working with files and functions within these files you need to tell
Python as well as any other language where to look. To do this you will want to use the
import command. When you use the import command you will be pulling in the
information or at least giving python access to this information to use.
When dealing with python and other languages you will have a set of preset function.
These preset functions are usually the most common to the language. There is also the
ability to write your own functions which I spoke of earlier and you will be able to call
those as well in the same manner.
So lets say that you wrote your own function called customers. In Python you will use
the import command to call this function.

Import Customers
Now this needs to be written at the top of your code in the file you wan to use it in. So for
example at the top of the screen you need to type these import command before you start
writing any other code. If you start writing code and then try to import a function it wont
work because you will not have it loaded into memory.
In the modules and functions you will have specific pieces of code that you will want to
use in your program. So lets say that in the customers module you have a function called
products. When this block of code is run you will be able to pull data from a database and
display information.

Accessing Functions in Modules


When working in these modules and blocks of code you will need to access a specific
piece of content. So for example lets say that there is a function called add in the
customers module.
To be able to access you need to create a variable, define what you are looking to have the
variable filled with and then write your code.
Result = customer.add


Print result
So in this block of code you are creating the variable result. The variable result will be
populated with the results that you get when the add function is run from the customer
module. So if the add function counts all of the times that the customer placed an order
the result variable will reflect that number.
When it comes to using modules and connecting them together it is very easy and the
power that you can get from it is pretty amazing. Another thing with modules it is a great
way for you to learn how to reuse code.
When you go into your python directory you will see all of these modules that you can use
to do a lot of different functions. You will also be able to see how specific tasks can be
completed as well as play with different ideas to make more efficient modules and
functions.

Dictionaries
The next thing that we are going to talk about are dictionaries. If you are familiar with
other programming languages a dictionary may resemble what is known as an array.
The difference between a dictionary in Python and an array in other languages is that
dictionaries do not relay on placement of information and will show information in
random orders where an array is specific to ordering and will return the value starting at 0.
So lets write a standard dictionary.
To start you will need to give the dictionary a name. The name can be anything that you
want it to be. So for this example we are going to call the dictionary products.
Next once you have a name for your dictionary you will want to give it some information.
The information in a dictionary is setup like this. Name:variable
So for example if you wanted to keep track of the amount of product you could write your
dictionary like this.
Product = {productName:amount, productName:amount}
So lets add in some data.
Product = {apples:23, oranges:55, peaches:45}
Now you are not limited to the number of items you can have in a dictionary. You can
have as many items that you want.
The only thing that you will want to remember is that each item in the dictionary has to
have a unique name and a unique value. So you cant have two entries with the name
apple or they will cancel themselves out.

Reading from the dictionary
When reading from the dictionary you simple need to use the print command that we used
earlier in this book. So if you wanted to find out how many apples were in your inventory
you would write the following code.
Print(dictionaryName[item])
Print(products[apples])
If everything worked out right you would be returned with the number of 23.
When working with dictionaries you can start to do a lot of different things. With
dictionaries you can contain other dictionaries within them, add in calls to functions and a
lot of other cool stuff that will make working with python and information a lot of fun.

Getting User Input
The final thing that I wanted to talk to you today about Python was how to get user input.
When developing a program you will generally be writing it to get information or input
from a user. You will then be able to use that information and supply the user with some
form of interactivity.
To create a simple program to gather input from a user you will need to create variable and
then use the input command to gather that information. Once you have that information
you can then use it in your program. This is what you will want to write.
Variable = input(question)
So for example if you wanted to get a persons name you would write code that would
look like this.
Name = input(What is your name? )
Now if you look at the code you will see that there is a space after the ?. This is done so
that you get a space at the end of the question in order to type your response. If you did
not have this space the cursor will be right on the ? and it would not look good. So just
keep that in mind.
To display the information on the screen you will want to use the print command to
display the text. So it would look like this.
Name = input(What is your name? )
Print name
Once you do this the user will enter in their nam
e and it would print out their response.
Practical exercise
In this practical exercise I want you to go back through the entire book and use the
information to create yourself a small game.
The game I want you to create is a mad libs type game where you will store information in
dictionaries and then using variables and the other information contained create user input
so that you can ask questions, call up information and write a fun story that prints on the
screen.
I want you to sit back and with a pen and piece of paper map out your application. I want
you to think of what you will want to do, the problems that you will as well as possible
solutions.
Then I want you to let your creative juices flow and create a cool story that people will
want to read. Now the story doesnt have to be a huge novel but something long enough
to enjoy and have some fun with.
Conclusion

In this book we tried to introduce you to the world or programming and the world of
Python. From this point forward you should have a strong foundation in which to draw
from and build upon.
As I wrote this book I tried to give you the reader a foundation and an understanding of
what programming is and why you would want to start with Python.
From here you should start looking at other functions and other possibilities. You will
want to think of different types of programs that you can create with python and explore
the code that others have written.
When it comes to programming with Python and any other language it will take time to
learn all of the pieces and parts that make it work. So take your time, learn one command
at a time see what you can do with it.
Dont jump into a thousand different things and learn it all in one day. Have fun, see what
you can do and what Python can do for you.