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.