Friday, April 17, 2015

Python Numbers Basics - Python Basics - Python Tutorial

Python NumbersPython Numbers


In this Python tutorial we be covering numbers in the Python Programming Language. If you have done any programming in the past, numbers in Python may look very familiar to you.  Numbers in programming languages really do not change much but they are one of the most important data types in programming, and this holds true in Python as well.

We use numbers in almost every program. We write with Python understanding the difference between integers and floating-point numbers is important. We are required to know how to use mathematical equations with our numbers so in this tutorial on numbers we are going to focus on number types and the math we can apply to each number type.


Integers


An integer is a number that has no decimal point associated with it.  These are integers 34, 5, 89, -45, -23 and so on. A number is considered an integer as long as it does not contain a decimal point.


Booleans


Integers have a subfamily called Booleans as well. Booleans return either True or False which may be confusing because they do not look like an integer. The reason why they are a part of the integer family is because True is actually equal to the integer value 1 and False is equal to the integer value 0.


Floating Point Numbers


Floating point numbers are numbers with a decimal point connected with them. Floating point numbers have more precision than an integer which makes floating point numbers more desirable when you are looking for precision in your programs. These are floating point numbers 5.5, 7.53423, 45323.1, -4523.9, -0.2 and so on.


Complex Numbers


Complex numbers are another number type in the Python Programming Language.  We will not cover complex numbers in this tutorial.  We will save them for a later chapter in our Python tutorial series.


Math With Python


What good are Python numbers without math? In the rest of our tutorial, we will look at how to perform math in Python. You will be required to open your Python interpreter in your terminal or command prompt.To start the interpreter follow the steps for your operating system. Mac users: Once you open the terminal type python3 and press enter. Windows users: Once in your command prompt type python.


Addition


Addition gives the ability to add numbers together. We use the plus(+) sign to indicate we want to add in Python. Let’s take a look at some examples of addition.


#integers
>>> 2 + 2
4
>>> 6 + -4
2
>>> -3 + 69
66
>>> 5673 + 43234
48907

#floats
>>> 5.6 + 3.4
9.0
>>> 67.56423 + 89.34
156.90422999999998
>>> -23.3 + 4.5
-18.8

#floats + integers
>>> 5.6 + 5
10.6
>>> 9 + -4.2
4.8
>>> 7.0 + 5
12.0

Addition in Python is pretty straight forward.  Addition works just like it did in first grade. Note if you add a integer and and a floating point number together then you will get a float.


Subtraction


Subtraction in Python gives us the ability to subtract numbers.  We use the hyphen(-) symbol to indicate subtraction in Python. Lets take a look at some subtraction examples in Python.


#integers
>>> 5 - 7
-2
>>> 6 - 4
2
>>> 6789 - 2345
4444

#floats
>>> 5.7 - 8.9
-3.2
>>> 4.5 - 2.0
2.5
>>> 65739 - 9834
55905

#floats - integer
>>> 4 - 4.5
-0.5
>>> 56 - 33.33
22.67
>>> 4.0 - 5
-1.0

Multiplication


Multiplication gives us the ability to multiply numbers in Python.  We use the star(*) symbol to indicate we want to multiply numbers together.  Let’s take a look at some examples of multiplication in Python.


#integers
>>> 5 * 6
30
>>> 76 * 4353
330828
>>> 235 * -9878
-2321330

#floats
>>> 56.43 * 989.02
55810.3986
>>> 45.1 * -43.1
-1943.8100000000002

#floats * integers
>>> 56 * 43.6
2441.6
>>> 434 * 9834.32
4268094.88

Division


Division in Python gives us the ability to divide numbers in Python.  We use the backslash(/) symbol to indicate that we want to divide numbers. Let’s take a look a few examples of division in Python.


#integers
>>> 56 / 45
1.2444444444444445
>>> 89 / 43
2.0697674418604652
>>> 9 / -3
-3.0
>>> 9 / 3
3.0

#floats
>>> 76.4 / 9.0
8.488888888888889
>>> 653.55 / 54
12.102777777777776
>>> 5.6 / 2.3
2.4347826086956523

When we divide integers Python will return a float. We will look at how to change a float to integer in our numbers chapter.

Now that we covered the four basic mathematical operators in Python we can safely move on for now.  We will have a chapter that will dive very deep into the numbers and the other mathematical operators in Python but for now we have covered the important ones.

This tutorial has covered the very basic Python numbers and Python mathematical operators. Do not forget to check out our chapter on numbers for more in depth information on Python numbers. If you have any questions please leave a comment below.



Thursday, April 16, 2015

Assign a Variable in Python - Python Basics - Python Tutorial

Assign a Variable in PythonAssign a Variable in Python


Before we can focus on Python basics we need to learn how to assign a variable in Python.  We will dive deeper into variables in a later tutorial.  This tutorial main focus is on assigning variables and calling variables after they been assigned which will make learning data types in this chapter easier.


Variable Creation


A variable in Python is created when it is assigned a value.  We assign a value to a variable with the equal sign(=).  Unlike other languages we do not need to assign a type or even declare it before a value is assigned to the variable.


Variable Syntax


Variable name must start with a letter and after first letter it contain numbers.  Variables can not start with numbers.


var = 3


var2 = “Cat”


Var3 = [123, 567, 987, 3456]

The above are examples how we create a variable by assigning it a value.


Call a Variable


To call a variable in python we simply just type the variable name and press return.  Check out the examples below for a better understanding.


Creating and Calling Variables in Python Examples


>>> a = 7
>>> a
7
>>> var = "This is our var"
>>> var
'This is our var'

This is a very simply overview of assign a variable in Python.  We will dig deeper into variables in a later tutorial.



Python Basics - Chapter 2 - Python Tutorial

Python BasicsIn the previous chapter we just gave you an overview of the Python Programming Language and we really did not get a chance to see of the language works. In chapter 2 of Learning Python, we are going to focus on Python Basics and actually get to practice typing some code. We will explore all of Python’s core data types with just an introduction to each of them. Our main focus in this chapter is cover why and how we would need a certain data type. Later in the tutorials we will really dive deep into each data type offering you even more knowledge.


Data Types To Be Covered


Numbers – 7534, 87432.33


Strings – ‘Hello World!’


List – [ 1, 89, 45, 5]

Dictionaries – ‘dog': ‘Maggie’, ‘cat': ‘Missy’


Tuples – (‘Tom’, ‘John’)


and more


Python Basics Tutorials List


  1. Assign a Variable in Python

  2. Python Numbers

  3. Python Strings

  4. Python List

  5. Python Dictionaries

  6. Python Tuples

  7. How To Install Sublime Text on Mac OS X

  8. How To Install Sublime Text on Windows

Wednesday, April 15, 2015

Mac OS X Terminal Commands - Getting Started - Python Tutorial

Mac OS X Terminal CommandsWhen it comes to learning Python there are several other tools you will need to learn as well to make you full fledge Python programmer. In today’s tutorial we will focus on Mac OS X Terminal Commands which we will need to know since we will spend a good amount of time in the Terminal.  Some of you that were around when computers only took commands from command line maybe thinking why are we going back in history but the truth is if you are familiar with the terminal you will be able to make things happen faster and easier than the graphical interface will allow you.


What Is The Terminal?


The terminal is an application that gives you ability to command your computer to perform a certain task by typing in a short command. When using the terminal, you are giving your computer a direct command similar to what some software would do if you were using software to perform the task. The terminal application is the same as back in the day when you would have to type in the command line for certain commands to put a game on your computer or to get the computer to do a simple task. The reason why the terminal is still available in the graphical user interface era is because it is one of the most powerful tools your computer has.


How to Open The Terminal?


The terminal application is located in the applications folder > utilities > terminal. We can either open the terminal through finder or we can be quicker or lazier however you want to look at and press command + space bar which opens spotlight and then we can type in the terminal.


How to Open The Terminal?
Command + Spacebar then type terminal

 


I suggest that you then pin your terminal to your dock so we can be even lazier and just click on the terminal as we go through our Python tutorials.


Entering The Terminal


Now that we have the terminal open.  You will not see much but your terminal will look a bit different than mine.


Open Terminal


Our commands start after the $ symbol.  In this tutorial, we are going show you the $ symbol in our examples since that symbol is universal but do not type it into your terminal when following along with us. Let’s get started with some basic Mac OS X terminal commands that we will be using in our Python tutorials.


Print Working Directory


pwd – Print working directory gives the location of the directory we are currently in.  This is a good way to find out where you are currently at within your files.


$ pwd
/Users/Tommy/desktop

List Information About File


ls – List information about file gives us all the files and folders in the containing folder.


$ ls
comments.py python wizardtut.py
django videos
projects web developement notes

Change Directory


cd – Change directory gives us the ability to navigate through our folders and files.


$ ls
Applications Downloads Pictures
Creative Cloud Files Library Public
Desktop Movies
Documents Music

Thomass-MBP:~ Tommy$ cd desktop

Thomass-MBP:desktop Tommy$

cd /foldername/filename – Move multiple directories at once.


$ cd desktop/python
Thomass-MBP:python Tommy$

cd .. – Move up to parent directory


python Tommy$ cd ..
Thomass-MBP:desktop Tommy$

cd ../.. – Move up to directories


Thomass-MBP:python Tommy$ cd ../..
Thomass-MBP:~ Tommy$

Create New Folder


mkdir – We can create a new folder using this command.


Thomass-MBP:desktop Tommy$ mkdir learnPython
Thomass-MBP:desktop Tommy$ cd learnPython
Thomass-MBP:learnPython Tommy$

Create File


touch – We can use this command to create a file


Thomass-MBP:desktop Tommy$ touch manage.py
Thomass-MBP:desktop Tommy$ ls
django projects web developement notes
learnPython python wizardtut.py
manage.py videos

Remove Files


rm – This command gives us the ability to remove files


Thomass-MBP:desktop Tommy$ ls
comments.py projects web developement notes
django python wizardtut.py
learnPython videos
Thomass-MBP:desktop Tommy$ rm comments.py
Thomass-MBP:desktop Tommy$ ls
django python wizardtut.py
learnPython videos
projects web developement notes
Thomass-MBP:desktop Tommy$

Remove Folder


rmdir – This command gives the ability to remove folders


Thomass-MBP:desktop Tommy$ ls
django python web developement notes
learnPython test wizardtut.py
projects videos
Thomass-MBP:desktop Tommy$ rmdir test
Thomass-MBP:desktop Tommy$ ls
django python wizardtut.py
learnPython videos
projects web developement notes

Make Computer Talk


say – This will change your text into audio.  This has nothing to do with our Python tutorials but I think it is fun and wanted to share it.


Thomass-MBP:desktop Tommy$ say "Learn Python Tutorial is the best Tutorial Site"

Conclusion of Mac OS X Terminal Commands


Now that we have covered some of the basic Mac OS X Terminal Commands we can move on with our Python tutorials.  Theses are only the basics if you want to learn more there are several resources on the internet do not forget to check them out. The commands that we covered in this tutorial will help you through the Python tutorials and if any others come up during the tutorials I will remember to share them.



Install Python 3 on Windows - Getting Started - Python Tutorial

Install Python 3 on WindowsIn this tutorial, we are going to look at how to install Python 3 on a Windows operating system. Unlike Mac, Windows has no default Python software installed on its computers. When we install Python on the Windows computer this will be the only Python available. I apologize that I do not have screenshots but for some reason my wife’s laptop will not take screen shots so if you need more help check out our video tutorial.


Download Python 3


Step 1 – Visit http://python.org this is the primary site for the Python Programming Language. If this is the first time you have visited the Python website after you’re done with this tutorial I suggest that you cruise their website. They have so much great information.


Step 2 – On the main menu hover over Downloads on the right side of the drop down click on Python 3.4.X. X will be a number currently at this time of this tutorial the current version is Python 3.4.2. As long as you click on Python 3 you are ready to go. Download will begin.


Install Python 3 on Windows


Step 1 – Locate your download.  Most likely in your downloads folder or if you use Google chrome it will be on the bottom of the browser.


Step 2 – Open File – Security Warning may open click on the run button.


Step 3 – Select whether to set up Python 3.4.X for all users on this computer.  Your choice here how you wish to install the software for all users or just for you. Then click next.


Step 4 – The Select Destination Directory I suggest that you leave it as current unless you know what you are doing. Then click next. If you change the location Python may not work on your computer.


Step 5 – Customize Python 3.4.X. Leave everything as is but scroll down to “Add python.exe to Path” click on the button and select “Entire feature will be installed on local hard drive” then click next.


Step 6 – The installation process will start. You may get a message “Do you want to allow the following program to install software on your computer” click yes.


Step 7 – You will see a screen that says Complete Python 3.4.X Installer click finish.


Test The Installation


Step 1 – Click on the start button on the bottom left hand corner


Step 2 – In the search type cmd this opens command prompt on Windows and hit enter


Step 3 – When your command prompt opens type “python” no quotes.


You should see the following in your command prompt.


C:\python
Python 3.4.2 (v3.4.2:ab2c023a9432)
Type "help", "copyright", "credits" or "license" for more information.
>>>

Yours should look similar to this. Please take notice of the Python 3.4.2 and below that >>> this means you’re in the interpreter.


If you get an error here please refer to the above video I will describe how to fix the issue. Try restarting your computer and if that doesn’t work the usual suspect when it comes to Python and Windows is the path and I will describe how to access your variables in your path and make the need changes.


Step 4 – Exit the interpreter using exit() or quit()


Awesome you have install Python 3 on your Windows computer.


Windows users for these tutorials I use a Mac so there is a couple differences I want you to be aware of.


  • When I say terminal that means Command Prompt to you

  • When I say open Python interpreter using python3 and means you use Python

  • When I say exit the interpreter using control + d that will not work for windows so will need to use exit() or quit() exit interpreter.

Other than those few issues above there really is no other differences. If you come across one please let us know since we use Macs here, but we want you Windows users to be able to follow a long as well.



Tuesday, April 14, 2015

Interpreter - Python Tutorial


An interpreter is a program that executes instructions which are normally in a coding language and are given by the user or by another program that has not been compiled into machine language yet.



Terminal - Python Tutorial

terminalThe terminal is a program that is included on the Mac OS X. The terminal provides a line interface that controls the UNIX based operating system. When using Python on a Mac we access the terminal very often to make commands to the Python software. 


To access the terminal on a Mac:


Via Spotlight – command + spacebar or click on magnifying class at the top screen to open spotlight. Once spotlight is open then type “terminal” to open the application.


Via Finder – applications > utilities > then terminal