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