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.