How to get started with Python programming



By McDonald, T | Last updated 02/12/17

In this world of technology something is needed to make it all run.  This something is programming languages such as Java, C# and Python just to name a few.  Subsequently, the programming industry is a great place to be a part of with new opportunities opening up all the time.  If you are new to programming, Python is a great place to get started since it is relatively easy to learn and has extensive libraries.

Getting started 

The following will be required in order to get started.

  • Python 3 (Free)
  • Software to right the code (Free)

Python 3 is different to Python 2.  The difference is in the syntax, so for now i will just keep to Python 3 since it is the newest.  Installing Python 3 is easy just follow the instructions in the video and then install Pycharm.  Pycharm is software for writing code.  You will need to install the latest version of Python for your operating system and the Community Edition of Pycharm.



Alternatively, you could use pyScripter to write python with.  To learn how to install it and the correct version of python, please watch the video.  Remember  to install python 3.4.4 for this IDE.

Links:
python 
pycharm
pyscipter

In these tutorials i will break things down into small chunks so it is easy to go back over certain code and you will not have to watch the entire video. 

Variables 

Variables are containers that hold a value.  The value in a variable can be of different types such as letters, numbers, Boolean or even objects.  To keep it simple i will just keep to the letters, numbers and Boolean.  Letter values are called strings.  For example, “Hello world! “ is a string and strings can be manipulated in different ways, but must always be used in quotes.  The quotes can be single or double quotes.  Numbers can be integers, float, long, double although you do not have to worry about this to much since Python will recognise them.  Boolean has two values: true or false.

String Examples 

myName = “Tony”

myName is the variable that contains the value “Tony“.

myCat = “Tony’s cat is called Tiger”.

myCat is the variable and contains the value “Tony’s cat is called Tiger”.

Numerical Examples

myInt = 3

myInt is the variable, while 3 is the value it contains.

myDouble = 3.56

myDouble is the variable and 3.56 is the value it contains. 

Boolean Examples

Boolean are either True or False.

myBoolean = True

myBoolean is the variable and it contains the value True.

Variables are covered in this video.


Comparison and Equality operators

Comparison and equality operators compare values. 

Examples:
  • 3 < 8.  This is true because 3 is less than 8.
  • 4 > 2.  This is also true because 4 is greater than 2.
  • 3 < 1.  This is false because 3 is greater than 1.
  • 3 = 3.  This is true. 
In some cases it is necessary to use more than one operator. 

Examples:
  • >= which means greater than or equal to.
  • <= which means less than or equal to. 
  • == which is equal to.  This is used to distinguish the difference between = which is used to assign a value to a variable.




Conditional Statements 

These statements are used to make decisions in the program.   The condition to the statement is contained within the parentheses.  The statement ends with a colon and the following code must be indented with a tab or 4 spaces.  Notice how the else does not need a condition.

if statement

assume myInt is 3

if (myInt == 3):
    print (“True.”)

elif statement

elif (myInt >= 3):
    print (“False!  myInt is greater than 3.”)

else statement

else: 
    print (“False!  myInt is less than 3”)
   

Watch the video to find out more.



Lists 

Lists can be used to store things.  Lists are given a variable name and [ ] used to contain the items in the list with each item separated by a comma.  Furthermore, lists can hold different types. 

Example:

myList = [“Fred”, 45, “Lucy”, 23  “Candy”]

In the example above “Fred is an element held in index 0” while 23 is an element held at index 3.  Indexes are useful because they can be used to identify an element. 

Example:

print (myList [2])

will print “Lucy”

The length of a list can be found by using len(). 

Example:

print(len(myList))

will print 5


Methods and user input

This allows the user to enter data into a variable such as their name.
User input Example:

userName = input(“What is your name?”)

If Jill is entered, the value Jill will be contained within the variable.  However, if the user is asked to enter a number, an error will occur.  This is because input() passes everything as a string, so to avoid this error Python needs a way to convert the entered value to an int.  This is done by using int()

Example:

userNumber = int(input(“Enter an integer”))

def is used to define a method.  The method consists of a method header and a method suit.  The method head consists of the name someMethod() and possibly an argument in the parenthesises, while the suit has the code to be run or implemented.  Moreover, the suit must be indented with a tab or four spaces.  The method can have a return statement, but not every method will have a return statement. 

Example:

def someMethod(anArgument):
    your code to be implemented

Watch the video to find out more.



View the python playlist to see more lessons 

Hope you enjoyed this introduction.  Please give a like, leave a comment and watch out for more on Python.   


Comments

Popular posts from this blog

Random Images from API with python

Build A Test Website In 3 Easy Steps