Thursday, April 30, 2015

keyword.iskeyword() - Python Tutorial

keyword.iskeyword()


The is keyword.iskeyword() command is used with the keyword module to check if a word is a keyword within the Python Programming Language. We must import the module for this command to work . If a word is a keyword we will get a boolean as True and if a word is not a keyword in Python then we will get a boolean False.


keyword.iskeyword() Syntax


keyword.iskeyword(‘word’)


Note that the word needs to be in a string format.


keyword.iskeyword() Example


#keyword.iskeyword() Example

#import keyword module
>>> import keyword

#check if a word is a keyword with a True return
>>> keyword.iskeyword('def')
True

#check if a word is a keyword with a False return
>>> keyword.iskeyword('dog')
False

#remove keyword module
>>> del keyword

 


 



keyword.kwlist - Python Tutorial

keyword.kwlist


keyword.kwlist will display a list of Python keywords when the keyword module has been imported. If you ever need to know the list of reserved words in the Python Programming Language we can import the keyword module and call keyword.kwlist to display a list of keywords.


keyword.kwlist Example


#keyword.kwlist

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

#remove keyword module
>>> del keyword

If you have any questions about the keyword module or the keyword.kwlist command leave a message below we will help you out.



Wednesday, April 29, 2015

Python Keywords - Python Tutorial

There is 33 Keywords(reserved words) in the Python Programming Language.  Below you will find a list of the 33 Python Keywords.





































Python Keywords


andasassert
breakclasscontinue
defdelelif
elseexceptFalse
finallyforfrom
globalifimport
inislambda
Nonenonlocalnot
orpassraise
returnTruetry
while with yield

 



Keyword Module - Python Tutorial

Keyword Module



 


Python Keyword Module


The keyword module gives us the ability to list out Python keywords or check if a word is a Python Keyword.


Step 1 – Import Keyword Module


#import keyword module

>>> import keyword
>>>

Step 2 – List out Python Keywords


#List Out Python Keywords

>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Step 3 – Is Keyword?


#Is Keyword?

>>> keyword.iskeyword('not')
True
>>> keyword.iskeyword('dog')
False

Step 4 – Remove Keyword Module


#Remove Keyword Module

>>> del keyword

If you have any questions about the Python Keyword Module leave a comment below.



Tuesday, April 28, 2015

Python Keywords - Python Variables - Python Tutorial

Python KeywordsWhen we name our variables there are a few words we cannot use to name a variable. These words are known as Python Keywords. These thirty three words have some sort of purpose in Python. They are either built-in functions or methods or serve some other purpose. If we try to use one of the Python Keywords we will get an invalid syntax error.


Below you will find the full list of Python Keywords we will not explain what each one does in this tutorial but if one of the words is highlighted in blue you can either hover over the word or click on it for a better explanation.





































Python Keywords


andasassert
breakclasscontinue
defdelelif
elseexceptFalse
finallyforfrom
globalifimport
inislambda
Nonenonlocalnot
orpassraise
returnTruetry
while with yield

Trying To Assign A Variable a Keyword


Take a look at what happens when assign a variable a keyword. We will get an error. We will look at couple different examples here.


#Trying To Create A Variable using def
>>> def = 3
File "<stdin>", line 1
def = 3
^
SyntaxError: invalid syntax

#Trying To Create A Variable using else
>>> else = "Why doesn't this work"
File "<stdin>", line 1
else = "Why doesn't this work"
^
SyntaxError: invalid syntax

#Trying To Create A Variable using False
>>> False = 3
File "<stdin>", line 1
SyntaxError: can't assign to keyword

Access The Python Keywords


We can gain access to the keywords in Python by importing a module called keyword. We have not covered importing of modules yet, but we will in a later tutorial. For now just follow a long this module is easy to use and pretty straight forward.


First We Need To Import The Keyword Module


Open your Python interpreter and we are about import the keyword module. To import a module we simply type import and then the modules name. Follow the example below to import keyword module.


#Import Module

>>> import keyword
>>>
#note we do not get any response when we import a module

Get The Python Keyword List


Here we will use the keyword module to get a list of the Python Keywords in our interpreter. This command only works if you have imported the module.


#Get The Python Keyword List

>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Check If A Word Is A Python Keyword Using The Keyword Module


In this example we will use the keyword module again this time we can check is a certain word is a keyword.


#Check If A Word Is A Python Keyword Using The Keyword Module

>>> keyword.iskeyword('else')
True
#note the word needs to be a string

#if we do not use a string we get an error
>>> keyword.iskeyword(else)
File "<stdin>", line 1
keyword.iskeyword(else)

You do not need to memorize the Python Keywords as we move on with our Python tutorials you will pick up on the keywords as you learn Python.  If you have any questions or comments leave them below.


 



id() - Python Tutorial


The id() built-in function returns a unique integer which is the identifier of an object in the memory. We can use id() to check if two variables are using the same object.


Examples Of The Built-In Python Function Id()


>>> a = 2
>>> a
2
>>> id(a)
4297366528
>>> b = 3
>>> b
3
>>> id(b)
4297366560
>>> id(a)==id(b)
False
>>> c = 2
>>> c
2
>>> id(c)
4297366528
>>> id(a)==id(c)
True
>>> d = "string"
>>> d
'string'
>>> id(d)
4328793624
>>> e = "String"
>>> e
'String'
>>> id(d)==id(e)
False

 


 



Monday, April 27, 2015

How Python Variables Reference Objects - Python Variables - Python Tutorial

Updated: March 9th, 2015


How Python Variables Reference Objects


How Python Variables Reference ObjectsIn the previous two Python tutorials, we discussed how Python variables reference objects which contain the value, type information and reference counter. When we say reference a variable just points to the object which is only a block of memory that contains information. When a variable points to an object that connection between the two is referred to as a reference. In this tutorial, we are going to look at how the actual reference works. Let’s dive into how Python variables reference objects.


Python Reference With One Variable and One Object


a = 2


In this case, the variable would point to the object. The variable is “a” and the object is “2”. So, “a” points to the object that contains “2”. Take a look at our diagram below.


Python Signal Variable


 


Python Reference With Two Variables Same Object


a = 2


b = 2


Here we have two variables with the same value.  In this case, Python would use the same object since “2” is the same for both variables. Have a look at another diagram how Python would reference the objects.


Two Python Variables One Object


In the above case, you can see both variables are referencing the same object even tho they are different variables.


Variable Assigned To Another Variable


a = 2


b = a


In this case, we set a = 2 and then we assign the variable “b” to the variable “a”. In this case the variable “b” does not actually reference “a”. “b” references “a’s” object not the variable. We will use the same diagram from above since it is actually the same.


 Variable Assigned To Another Variable


Reassigning A Variable


a = 2


b = 2


The reassignment


b = 3


In this scenario, we have the variables “a” and “b” set to the same object then we reassign the variable “b” to a new object that contains the value of 3. How would this work? Let’s take a look at another awesome diagram.


Reassigning A Variable in Pyhton


Reassigning a Variable That Has An Assigned Variable


a = 2


b = a


The Reassignment


a = 3


In this scenario, we originally assign the variable “a” to the value of 2 and then we assign the variable “b” to the object of “a”. Then later, on we reassign the variable “a” to a new object. In this case, “b” would still reference the object that contains the value 2 and the variable “a” would reference a new object that contains the value 3. Check out another one of my awesome drawings to better understand how this works.


 Reassigning a Variable That Has An Assigned Variable


How To Access Python Objects Identifier?


We can actually gain access to the identifier with some coding. I will show how we can see the identifier and how we can also see that we are using the same object when referencing the same value with different variables. The returned integer is the actual location of objects in the memory of the underlying C language.


Access Python Objects Reference Example


#Acces Objects Reference
>>> a = 78
>>> id(78)
4297368960

#Access Another Reference
>>> b = "String"
>>> id(b)
4328969864

#How about two variables accessing the same object
>>> c = 2
>>> id(c)
4297366528
>>> d = 2
>>> id(d)
4297366528

#Easier way to compare the two
>>> id(c) == id(d)
True

#How about using above example and the reference is not the same what would happen?
>>> id(c) == id(a)
False

In this tutorial, we really went deep into the workings of the Python programming language. We now see how Python variables reference objects. We also looked into how to find the memory location of an object using the built-in function id(). If you have any questions about how to Python variables reference objects leave a comment below.



type() - Python Tutorial


The type() built-in Python function gives us the ability to check the type information of an object via the variable.


Type() Built-In Python Function Example


#Create a variable
>>> a = 9
#Call the variable
>>> a
9
#Check the type of the object
>>> type(a)
<class 'int'>
#create a variable containing a string
>>> b = "string"
#check the type of the object
>>> type(b)
<class 'str'>
#Create a variable
>>> c = "apple": 5, "banana": 3
#check the type
>>> type(c)
<class 'dict'>

 



Sunday, April 26, 2015

Why There is No Type Declaration in Python - Python Variables - Python Tutorial

Why There is No Type Declaration in PythonWhy There is No Type Declaration in Python


If you have learned other programming languages like Java or C you may a bit confused why we can declare a variable without giving that variable a type.  If Python is your first language, then you may not. Python is smart enough to find out our types when we declare a variable in our programs. If Python can find out what type of data we are using in our code then why would we waste our time declaring types of variables?


The Reason Why There is No Type Declaration in Python


The reason why we do not have to give a variable a type declaration is because the variable does not contain any information.  In the previous tutorial, we discussed that variables are just pointers or they just reference a section of memory called an object. The object contains the type information, the value that was given and a reference counter.


Why Does The Object Hold Type Information?


The object holds the type information to save memory and accelerate your programs. If we created several variables with the exact same value then these variables are just pointing to one memory space. If the variables held the type information and values then we would be using a lot more memory in our programs which would slow down the program. In this day and age everyone wants programs to run at high speed.  So when an object holds the information we can cut down on memory used.


How to Access Python Objects Type Information?


We can access the type information in our objects by using a built-in function called type(). Let’s take a look at some examples.


Access Python Objects Type Information Examples


#Type integer
>>> a = 9
>>> type(a)
<class 'int'>

#Type Float
>>> b = 6.7
>>> type(b)
<class 'float'>

#Type String
>>> c = "String"
>>> type(c)
<class 'str'>

#Type List
>>> d = [123, 456, 789]
>>> type(d)
<class 'list'>

#How about first object contained in the list
>>> type(d[0])
<class 'int'>

Now that we have seen Why There is No Type Declaration in Python the understanding of how variables work should become clear as we move on with our Python Variable tutorials.  If you have any questions about Why There is No Type Declaration in Python leave a comment below we will be sure to help you out.



Complete Guide to Variables - Python Variables - Python Tutorial

Python VariablesComplete Guide to Variables


In an earlier Python tutorial, we introduced how to assign a variable in Python but as you know we like to go very deep into Python here at Learn Python Tutorial and we are going to very deep into Python variables.What is really cool about Python is creating a variable is so simple it confuses some programmers coming from other languages.


Python Variables Are So Simple


Let’s start by making a simple variable that contains an integer – var = 2. Here is why Python variables are so damn easy! First we do not have to tell Python that var is a variable like other languages and we do not need to tell Python that var is going to contain an integer. If you have been working on other programming languages you may be thinking what, how and why? This is because Python determines variables and types at runtime so there is no need to declare this information before time.


Creating Variables


We already looked at creating variables in a previous tutorial but a review will be a good thing. To create a variable we give the variable a name like “var” and assign the variable by using the equal symbol(=) with a value like an integer of 6. When making a variable it must always have a value to exist if the variable is not assigned a value then that variable would not exist. If we assign the variable again to a different value, then that variable would take on the new value and drop the old value.   In Short… Initial assignment creates the variable in Python and changing a value in a variable is referred to as reassignment.


How About Type Information In Variables?


Variables never ever store any information! What?  Variables are just a label or a title that points to an object(value) at a certain point of time. The object includes the value, type information and reference counter. Check out the diagram below this will hopefully explain it better.


Python Variables


 


*Note – The Pointer in Python is referred to as a Reference


How Are Variables Used?


When the script is ran the variables are replaced by their associated objects stored values. This is the main reason why all variables must be assigned before the script is ran and if they are not assigned we will get errors.


Variables Life Cycle


What happens from the time a variable is created to the time object is no longer used? First we will create a variable and then we will talk about the life cycle.


var = 9


Above we create a variable and it references an object that contains the value of 9. We will take a closer look at what happens here.


The Life Cycle


  1. An object is created that holds the value of 9 (allotted part of memory that stores the value 9, integer type and the reference counter.

  2. A variable is created but it really does not exist till it is assigned to an object(look at step 3)

  3. The pointer(reference) is created which associates the variable of the object.

  4. When a variable no longer exists then, Python may remove the object to free up space. This does not always happen because some objects may be used more than once, but Python makes that decision when objects will be used more than once.

Important Information About Python Variables


  • Variables can not point to other variables but we can write something like this var1 = var but they both reference the same object not each other.

  • Objects can reference other objects like in list, dictionaries and tuples can reference other objects since the store different types of values within those data types.

  • Python runs its own cache which removes objects that are no longer needed to free up memory which will help programs run faster.

 


We have dug deep into Python variables and we gave you a lot of information and some may go over your head but it is import to know the following.


  • Variables do not store information but reference the location where the information is stored.

  • Objects are just a block of memory which contains the value, type information and reference counter.

  • Variables are replaced by the value when program is ran

  • Variables are not created till they are assigned a value and they must have a value to exist.

If you have any questions about our tutorial on Python Variables leave a comment below.



Saturday, April 25, 2015

Python Variables - Chapter 3 - Python Tutorial

Python VariablesPython Variables


In this chapter, we are going to explore everything you need to know about Python Variables. This chapter on Python Variables will begin our comprehensive Python education tutorials.  We will go at a snail’s pace but we will cover everything you need to know about the Python Programming Language. As we dig into variables, in Python we will see how Python is different from other programming languages you may know.This chapter will also give to light how flexible the Python language is. Check out the Python variables Tutorial list below.


Python Variables Tutorial List


  1. Complete Guide To Python Variables

  2. Why Is There No Type Declaration In Python

  3. How Python Variables Reference Objects

  4. Python Keywords

  5. Python Garbage Collection

sequence - Python Tutorial

sequenceSequence – In Python a sequence is a way to store data in an organized and efficient way.  There are several different data types in Python that use a sequence and they are strings, list and tuples are the most used sequences in Python and there are some rarer ones like ranges and binary sequences.



Friday, April 24, 2015

rmdir - Python Tutorial

rmdir – Allows us to remove directories via the terminal. This command makes for an easier and quicker way to remove folders. If your directory contains files or folders the rmdir will not work. The Work around to delete a directory that contains folders or files we need to use rm -rf folder name to delete that directory. Be Careful you do not delete an important directory or files there is no returning from this.


rmdir Example


#Find a file to delete
Thomass-MBP:desktop Tommy$ ls
Screen Shot 2015-03-02 at 9.17.06 AM.png
Screen Shot 2015-03-02 at 9.20.35 AM.png
Screen Shot 2015-03-02 at 9.28.59 AM.png
Screen Shot 2015-03-02 at 9.35.50 AM.png
django
learnPython
manage.py
movies
projects
python
test
videos
web developement notes
wizardtut.py

#We will remove Test
Thomass-MBP:desktop Tommy$ rmdir test
Thomass-MBP:desktop Tommy$ ls
Screen Shot 2015-03-02 at 9.17.06 AM.png
Screen Shot 2015-03-02 at 9.20.35 AM.png
Screen Shot 2015-03-02 at 9.28.59 AM.png
Screen Shot 2015-03-02 at 9.35.50 AM.png
django
learnPython
manage.py
movies
projects
python
videos
web developement notes
wizardtut.py

#Remove a directory that contains folders and files
Thomass-MBP:desktop Tommy$ mkdir test
Thomass-MBP:desktop Tommy$ cd test
Thomass-MBP:test Tommy$ touch test.py
Thomass-MBP:test Tommy$ ls
test.py
Thomass-MBP:test Tommy$ cd ..
Thomass-MBP:desktop Tommy$ ls
Screen Shot 2015-03-02 at 9.17.06 AM.png
Screen Shot 2015-03-02 at 9.20.35 AM.png
Screen Shot 2015-03-02 at 9.28.59 AM.png
Screen Shot 2015-03-02 at 9.35.50 AM.png
django
learnPython
manage.py
movies
projects
python
test
videos
web developement notes
wizardtut.py
Thomass-MBP:desktop Tommy$ rm -rf test
Thomass-MBP:desktop Tommy$ ls
Screen Shot 2015-03-02 at 9.17.06 AM.png
Screen Shot 2015-03-02 at 9.20.35 AM.png
Screen Shot 2015-03-02 at 9.28.59 AM.png
Screen Shot 2015-03-02 at 9.35.50 AM.png
django
learnPython
manage.py
movies
projects
python
videos
web developement notes
wizardtut.py
Thomass-MBP:desktop Tommy$

 



How to Install Sublime Text on Windows - Python Basics - Python Tutorial

How to Install Sublime Text on WindowsHow to Install Sublime Text on Windows?


In this Python tutorial, we will take a very quick look at how to install Sublime Text on Windows. Sublime Text is a text editor which will enable us to edit our Python code as we move on with our Python Tutorials. I am sorry this tutorial does not contain screenshots, but my wife’s laptop doesn’t take screenshots for some odd reason.

Step 1 – Visit Sublime Text website at http://sublimetext.com

Step 2 – Located and press the download menu item on the top bar of their website.

Step 3 – About half way down the page you will see two links one for Windows and another for Windows 64 bit pick which operating system you are using.


Step 4 – Your download of Sublime Text will begin.

Step 5 – Locate your download and run the sublimetext.exe to start the installation of the editor

Step 6 – Open File – Security Warning will pop up we simply just press run.

Step 7 – Welcome To Sublime Text – Press Next

Step 8 – Select Destination Folder – Press Next keep in your main folder

Step 9 – Select Additional Task – Press Next

Step 10 – Ready to Install – Press Install

Step 11 – Open Sublime Text to make sure the installation worked

Now that we have successfully learn how to install Sublime Text on Windows we can turn to writing some Python Code. If you have any questions leave a comment below.


 



Thursday, April 23, 2015

How to Install Sublime Text on Mac OS X - Python Basics - Python Tutorial

How to Install Sublime Text on Mac OS X How to Install Sublime Text on Mac OS X?


We are moving right along with our Python tutorials and we are about to go very deep into the Python language but before we dig any deeper we need a program which is called a text editor to edit our code.  I have my favorite and you may have your own favorite if you have a text editor that you are comfortable with then use that one.  If you do not have have a text editor yet I will show you how to install Sublime Text on your mac in todays tutorial. I have been using Sublime Text for years and I believe that it is one of the best text editors and its completely free. They do ask you to upgrade to a paid version and you may do that if you want but it is not necessary. Make sure you follow each step of how to install Sublime Text on Mac OS X closely so you install the editor correctly.


How to Install Sublime Text on Mac OS X?


Step 1 – We need to visit Sublime Text website to download the software. Visit sublimetext.com


How To Install Sublime Text on Mac OS x?


Step 2 – After you are on sublimetext.com we need to click on the Download menu item on the top menu bar of the their website.


How To Install Sublime Text on Mac OS x?


Step 3 – We are going to download Sublime Text 3 it is still beta but since it is going to be the future of Sublime Text we should get use to it.  Click on Sublime Text 3 about two paragraphs down.


How To Install Sublime Text on Mac OS x?


Step 4 – Click on the OS X link to begin the download.


Step 5 – Locate your download of Sublime Text 3 probably in your download folder.


Step 6 – Slide the Sublime Text logo into your applications folder


Install Sublime Text on Mac OS X


Step 7 – Open your application folder and locate Sublime Text and open your Sublime Text.


Step 8 – This step is optional we will be using Sublime Text often so we suggest you keep it in the dock.  Right click on the Sublime Text Logo in your dock and go to options keep in dock.


 


Now that you have complete the Python Tutorial How to Install Sublime Text on Mac OS X we can move on to more in depth Python tutorials.  If you have any questions leave a comment below.



rm - Python Tutorial

rmrm – Remove files via the command line in the terminal.  This command gives us the ability to quickly remove files from our computer.  You need to be careful when using the rm command there is no way to recover the files once they are rm.


rm Examples


#Remove a file using the rm command
Thomass-MBP:test Tommy$ ls
test.py test1.py test2.py test3.py
Thomass-MBP:test Tommy$ rm test.py
Thomass-MBP:test Tommy$ ls
test1.py test2.py test3.py

#Remove multiple files using rm
Thomass-MBP:test Tommy$ ls
test1.py test2.py test3.py
Thomass-MBP:test Tommy$ rm test1.py test2.py test3.py
Thomass-MBP:test Tommy$ ls
Thomass-MBP:test Tommy$

 




rm

Wednesday, April 22, 2015

touch - Python Tutorial

touchtouch – Is a terminal command that is used to change a file’s date but we can also use touch to create a file via the terminal.


touch Example


#Create a file using touch
Thomass-MBP:test Tommy$ touch test.py
Thomass-MBP:test Tommy$ ls
test.py

#Create multiple files using touch
Thomass-MBP:test Tommy$ touch test1.py test2.py test3.py
Thomass-MBP:test Tommy$ ls
test.py test1.py test2.py test3.py
Thomass-MBP:test Tommy$

 



mkdir - Python Tutorial

mkdir



 


mkdir – Make directory is the terminal command we use to create a new directory(folder).


mkdir Example


We will be using our terminal for this demonstration if you would like to try some of our mkdir examples then go a head and fire up your terminal.


#Make a new directory
Thomass-MBP:desktop Tommy$ mkdir test
Thomass-MBP:desktop Tommy$ ls
django projects web developement notes
learnPython python wizardtut.py
manage.py test
movies videos

#Make multiple directories
Thomass-MBP:desktop Tommy$ ls
django python videos
learnPython test web developement notes
manage.py test1 wizardtut.py
movies test2
projects test3

 



Tuesday, April 21, 2015

cd - Python Tutorial

cd



 


cd – Change Directory gives us the ability to change through our directories while we are in the terminal.


cd Examples


In these cd examples we are going to use our Mac terminal.  If you are following along open your Mac terminal and try some of these commands.


#cd to directory but first we need to know what directories are in the current directory

Thomass-MBP:~ Tommy$ ls
Applications Downloads Pictures
Creative Cloud Files Library Public
Desktop Movies
Documents Music
Thomass-MBP:~ Tommy$ cd desktop
Thomass-MBP:desktop Tommy$

#go back a directory
Thomass-MBP:desktop Tommy$ cd ..
Thomass-MBP:~ Tommy$

#Change to root directory
Thomass-MBP:~ Tommy$ cd /
Thomass-MBP:/ Tommy$

#change to home directory
Thomass-MBP:/ Tommy$ cd ~
Thomass-MBP:~ Tommy$

 




cd

ls - Python Tutorial

ls



 


ls – List information about folders and files of the current file you are in.  ls will list folders and files in alphabetical order.


ls Example


In this example we will be working in our terminal and our current directory is our desktop.


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

 




ls

Monday, April 20, 2015

pwd - Python Tutorial

pwd



 


pwd – Print Working Directory is a terminal command that will output the path to the current directory that you are working in.  This command is useful when you need to know the path to the current directory.


PWD Example


Thomass-MBP:~ Tommy$ cd desktop
Thomass-MBP:desktop Tommy$ pwd
/Users/Tommy/desktop



pwd

Basics of the Python Tuple - Python Basics - Python Tutorial

Basics of the Python TupleBasics of the Python Tuple


In this Python Tutorial, we will discuss the basics of the Python tuple which is another data type in the Python programming language. This will be a brief introduction to the tuple and we will cover tuples in full in a later chapter.


What is a Tuple?


A tuple is similar to a list but unlike a list tuples cannot be amended after they have been created which makes them immutable. Tuple is a sequence like a list so we would use the index to call our values.  Tuples are not used all that often in practice but them being immutable give us a little more security which protects them from being changed when the program runs. We normally use tuples for a fixed set of values in our programs. If our values have to be changed in place, then we would be better off using a list over tuple. Python has a few built-in methods that we can call on tuples. We will cover the methods in a future tutorial.


What is the Tuples Syntax?


We indicate to Python that we want to use a tuple by using parentheses( ) and just like list we separate data by commas.


Tuple Syntax 


var = (213, 5643, “Red”, “Blue”)


var1 = (“TV”, “light”, “Camera”, “Seat”)


Some Tuple Examples


Below we will create some tuples and assign them to a variable.  Follow along in your Python interpreter and when you are done with our tuple examples try some on your own.


 


#Create a tuple and assign to a variable.
>>> a = ("Red", "Blue", "White", "Green", "Black", "Orange")

#Call the tuple via the variable
>>> a
('Red', 'Blue', 'White', 'Green', 'Black', 'Orange')

#Create a tuple and assign to a variable.
>>> b = ("123", "456", "789")

#Call the tuple via the variable
>>> b
('123', '456', '789')

#Call a value via the index
>>> b[1]
'456'

#Proof that Tuples are immutable
>>> b[1] = "987"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Now that you have complete the Python tutorial called Basics of the Python Tuple you learned how to create and call tuples via a variable. We also discussed that tuples are immutable and the are ordered in a sequence.  If you have any questions leave a comment below the basics of the Python tuple.



Sunday, April 19, 2015

Basics of the Python Dictionaries - Python Basics - Python Tutorial

basics of the Python dictionariesBasics of the Python Dictionaries


In this tutorial, we are about cover the basics of the Python dictionaries.  This tutorial’s main focus is placed on what a dictionary is and how to create a dictionary in Python. We will cover dictionaries in full in a later chapter this is only a brief overview. We will walk you through creating a dictionary and assigning it to a variable and then call that variable to display the dictionary.


What is a Python Dictionary?


A dictionary in Python is not a list of words and their meanings but you could make an actual dictionary if you wanted to use the dictionary data type in Python. Dictionaries are different from strings and list because the do not follow a sequence order but it does follow something called mapping which is also a collection of data but not in a specific order so Python stores the data via a key. Dictionaries do not comply with a left to right ordering like list and strings. Dictionaries are also mutable which means we can change a dictionary after it is created so a dictionary can grow or shrink on demand.


What is Mapping?


Since dictionaries do not follow a sequence order like a string or list we need a way to get values stored in a dictionary and we do this by giving the value of a key. The key is tied to a value which gives the ability to identify a value. In sequences like list, we can use the index to get the value and in dictionaries we use a key to get the value.


Python Dictionary Syntax


When we create a dictionary in Python we use curly braces   and inside those curly braces we provide a key and value like “key: value” the key and value are separated by a colon( : ). It is important to keep in know which is the key and which is the value. The key will always be on left and the value will always be on the right.


Python Dictionary Syntax


‘pet': “dog”, ‘age': 7, ‘name': ‘maggie’


Take a look at our example above. The keys in this dictionary are ‘pet’, ‘age’ and ‘name’. The values in this dictionary are ‘dog’, 7 and ‘maggie’.


Python Dictionary Examples


In these examples, we are going to show you how to create some dictionaries and assign them too variables.  Then we will show you how to call the whole dictionary and call a key to get the value associated with that value.


#create a dictionary and assign it to a variable
>>> a = "pet": "cat", "age": 2, "name": "Missy"

#Call the dictionary after it is created
>>> a
'name': 'Missy', 'age': 2, 'pet': 'cat'

#Call a value from the dictionary by its key
>>> a["name"]
'Missy'

#Something new lets change the age of the cat
>>> a["age"] += 1

#call the new dictionary
>>> a
'name': 'Missy', 'age': 3, 'pet': 'cat'

#To prove there is no index with dictionaries
>>> a[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1

Basics of the Python Dictionaries Conclusion


We have covered the very basics of the Python dictionaries and we still have not scratched the surface. We will be revisiting dictionaries in a future chapter where we will dig deep into dictionaries and learn everything there is to know about Python dictionaries. If you have any questions please leave us a message below.



Python List Basics - Python Basics - Python Tutorial

Python listPython List


In this Python tutorial, we are about to take a brief tour of another data type in Python called a list. A Python list is a positionally ordered collection of data. A List has no maximum size limitations so your list can be as large as you want or need it can even be empty which is referred to an empty string. We are able to modify the list after they are created by assignment or specific list methods. A Python list is very useful when programming we could use them for so many reasons like a sports roster, list of business and many more.


Python List Syntax


A list is declared by a square bracket( [ ] ) and the content inside a list referred to as the list data and each piece of data is separated by a comma.


List Syntax


[987, “Python”, “Dog”, 1, -5, “Cat”]

In the example above we have created a list that contains numbers and strings. Each piece contained in the list is separated by a comma.


Python List Sequence


Like strings a list is in a sequence but unlike strings the index on counts pieces of data(between the commas) in the list. We will use the above example in list syntax to see how the sequence work in list.
















List987“Python”“Dog”1-5“Cat”
Index012345

 


Python List Examples


Fire up your Python interpreter and follow long.  We will create some Python list and assign them to a variable. This will be a good chance for you to practice creating and assigning list.


#Create a Python list
>>> a = ["Tom", "Mike", "Jaime", "Jake"]

#Call the Python List
>>> a
['Tom', 'Mike', 'Jaime', 'Jake']

#Create a Python List
>>> b = [987, 897, 34, 21, 67]

#Call the Python List
>>> b
[987, 897, 34, 21, 67]

#Try slicing the list we have not tried this before
>>> b[2]
34
>>> b[-1]
67
>>> b[-0]
987
>>> b[-2]
21

#Create a list
>>> list = ["eggs", "milk", "apples", "tea"]

#call the list
>>> list
['eggs', 'milk', 'apples', 'tea']

Now we have complete our brief overview of Python list we can now set up and call list. We will cover list in full in a couple chapters.  If you have any questions leave us a comment below or Q & A section.



Saturday, April 18, 2015

Python String Basics - Python Basics - Python Tutorial

Python StringPython String


In today’s Python tutorial, we are going to look at another important data type that we use often in Python. We are about to focus on a data type called a string. A string is a sequence of data like text or a collection of bytes. In this tutorial, we are going to only be focusing on an introduction to the Python string. We will dig deeper into strings in chapter 4 of this series.


What is a String?


A string contains a list of characters in a specific order(sequence). The characters in a string can be letters, numbers, special characters like symbols and spaces.  Strings have no limit on how long they may be and you may also take a string that contains no characters this is called a “empty string”.


What is the Python String Syntax?


The syntax of a string in Python is quite simple. We create a string by enclosing characters in quotes which can be single quotes, double quotes or triple quotes.  Which method you use is entirely up to you. Through out the tutorials I will be using single quotes for most of the tutorials except when certain situations arise where single quotes are not the best option.


Python String Syntax


‘This is a string in Python’


“This is a string in Python”


”’This is a string in Python”’


“””This is a string in Python”””


We can not start with single quote and end with a double quote.  We must start and end with same type of quote.


String Syntax Examples


We will be using a print statement in these examples we have not discussed the print statement.  The print statement tells Python to print the content contained in the print statement to the screen.  The syntax for the print statement is print().


#String Syntax Examples

>>> print('This is a string')
This is a string

>>> print("This is a string")
This is a string

>>> print('''This is a string''')
This is a string

>>> print("""This is a string""")
This is a string

 


Why Is A String a Sequence?


We mentioned earlier that a string is a sequence of data.  The data in a string takes on a positional ordering which means each character in a string has an exact position.  We can access the exact position of a character in a string by its index which is a numbered position of each character.  Each string index starts with the number 0 and counts up from that point(left to right).  Look at the example below.


 































This
is
a
String
0123456789101112131415

 


In the above example you can see that each letter and space have specific index position starting a 0. It is safe to say each character is its on data which is packaged together to create a string.  We will dive into this some more in chapter 4.


Strings are Immutable


Strings can not be changed once they have been created.  This is why they are considered immutable.  We can change a string by creating a new string but the current string can not be changed.


Python String Basics Conclusion


In this Python tutorial we took a very brief look at the Python string.  We didn’t even scratch the surface when it comes to strings but we can now create a string and we understand that each character in a string is in a specific order because strings are just a collection of letters, numbers, spaces and special characters.  We also learned that strings are immutable meaning once we create them we can not change them unless we create a new string.



Floating Point Number - Python Tutorial


A floating point number is a number that contains a decimal point. These numbers are floating point numbers 4.5, 767.43, -89.342343, -67.8988893



Friday, April 17, 2015

Integer - Python Tutorial


A integer is a number that doesn’t have a decimal point.  These numbers are integers 1, 4, 6, -4, -6, -99



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