Difference between revisions of "Workshop: Introduction to programming (with Python)"

From Design and Build Lab
Jump to navigation Jump to search
(Started a "variables" section)
Line 18: Line 18:
  
  
 
+
==Variables==
== Variables ==
 
 
Now that we understand the basic workflow of python (create code in text editor → execute in command prompt), we can start discussing the different functions and capabilities of Python. We can start this discussion with variables. You can think of variables as the objects that store information for our program. A variable has two basic parts; '''A name''' (which we will use to refer to the variable) and '''A value''' (the info we care about). Besides those two parts, variables can hold different types of information.
 
Now that we understand the basic workflow of python (create code in text editor → execute in command prompt), we can start discussing the different functions and capabilities of Python. We can start this discussion with variables. You can think of variables as the objects that store information for our program. A variable has two basic parts; '''A name''' (which we will use to refer to the variable) and '''A value''' (the info we care about). Besides those two parts, variables can hold different types of information.
  
Line 25: Line 24:
 
'''Types Of Variables:'''
 
'''Types Of Variables:'''
  
# Strings - A sequence of characters (ex: "Python is fun!")
+
#Strings - A sequence of characters (ex: "Python is fun!")
# Integers - Whole numbers (ex: 3, 10, 999)
+
#Integers - Whole numbers (ex: 3, 10, 999)
# Floats - Numbers w/ a decimal (ex: 1.0, 3.14, 7.77)
+
#Floats - Numbers w/ a decimal (ex: 1.0, 3.14, 7.77)
# Lists - An sequential list of other variables (floats, integers, strings, more lists, or a mix) (ex: ['cat', 'dog', 999, 3.14, variable1])
+
#Lists - An sequential list of other variables (floats, integers, strings, more lists, or a mix) (ex: ['cat', 'dog', 999, 3.14, variable1])
  
  
Line 39: Line 38:
  
  
Updating Variables:
+
'''Updating Variables:'''
 +
 
 +
To update a variable (change it's value later on in the code) we simply need to redefine it's value. You can do this by:
 +
 
 +
variableName = [new value]
 +
 
 +
'''Casting Variables:'''
 +
 
 +
When we get into more complex implementations our variable type will become very important. Ask yourself 'what is the difference between these two codes?'
 +
 
 +
# float sum = 1.5 + 3
 +
# int sum = 1.5 + 3
 +
 
 +
Answer: 1.5 + 3 = 4.5 but 4.5 is a float value. So for code #1 the answer is 4.5, but the "sum" variable in code #2 is an integer so the answer will be rounded down to 4.
  
 +
It because of cases like these that sometimes we implement a technique known as casting. Casting simply means changing the type of information a variable holds. With casting we can turn a int to a float, float to an int, str to an int, etc. We cast by writing the variable type we want ("float", "int", "str", etc.) followed by the variable/value we want to cast enclosed by parenthesis.
  
 +
Examples:
  
Casting Variables
+
* float(3) → 3.0
 +
* int(2.9999) → 2
 +
* int("1964") → 1964
  
  
== '''The first step''' is to download Python- ==
+
=='''The first step''' is to download Python-==
 
If you haven’t yet got python, the latest official installation packages can be found here:
 
If you haven’t yet got python, the latest official installation packages can be found here:
 
http://python.org/download/
 
http://python.org/download/

Revision as of 21:52, 9 September 2019

Welcome to Introduction to Programming!

Setting up your work station

  1. Download Python - You can download it here
  2. Check that your installation was successful - We will do this by checking the version of python we just downloaded. We can do this by opening up our command prompt and simply typing "python --version". If you did everything right you should see a message that looks something like this: "Python 3.7.3"
  3. Let's test our distribution - Let's get our first introduction into Python programming by making a hello world file.


Basic Hello World Program

A hello world program is the most basic program you can write to test that your distribution is working smoothly.

The print statement will simply print out "Hello world!" to the console when ran.
  1. Open a text editor - This is where we will be writing all of our code. There are many different editors you can use, so feel free to choose the one you are most comfortable with. The only requirement is that the text editor should be able to product plain text files. Windows users already have Notepad installed. Mac users have TextEdit (other text editors include TextWrangler, Atom, and much more).
  2. Write your code - To make this program as simple as possible we will simply write a print statement. After writing your print statement save the file to your Desktop (or whatever other directory you want) as "hello.py". All python files need to have the extension .py
  3. Execute the code - Now is the fun part, running the code! Open up the command prompt (Terminal for Mac Users, Powershell for Windows). Navigate to the directory where you saved the file (ex: cd Desktop). Now execute your python code by typing "python [filename]"
    Execute the code!


Variables

Now that we understand the basic workflow of python (create code in text editor → execute in command prompt), we can start discussing the different functions and capabilities of Python. We can start this discussion with variables. You can think of variables as the objects that store information for our program. A variable has two basic parts; A name (which we will use to refer to the variable) and A value (the info we care about). Besides those two parts, variables can hold different types of information.


Types Of Variables:

  1. Strings - A sequence of characters (ex: "Python is fun!")
  2. Integers - Whole numbers (ex: 3, 10, 999)
  3. Floats - Numbers w/ a decimal (ex: 1.0, 3.14, 7.77)
  4. Lists - An sequential list of other variables (floats, integers, strings, more lists, or a mix) (ex: ['cat', 'dog', 999, 3.14, variable1])


Defining Variables:

One of the great features of Python is that the built in compiler can intelligently figure out what type of variable your specified variables should be and takes care of this distinction automatically. This means defining variables is easier in Python than some other languages you may be familiar with. We define a variable by writing its name, followed by an equal sign, followed by the value of the variable (ex: pi=3.14, name="Dylan", eq="3+4"). We can also define variables without setting the values immediately. To do this we need to tell the compiler what type of variable we need (since it can't figure it out automatically) (ex: int pi, str myName)


Important Note: The standard naming convention of variables follows: The first word is always all lowercase then the proceeding words begin with an uppercase letter (ex: myVariable, reallyAwesomeInteger). You don't need to follow this convention, but it is worth knowing.


Updating Variables:

To update a variable (change it's value later on in the code) we simply need to redefine it's value. You can do this by:

variableName = [new value]

Casting Variables:

When we get into more complex implementations our variable type will become very important. Ask yourself 'what is the difference between these two codes?'

  1. float sum = 1.5 + 3
  2. int sum = 1.5 + 3

Answer: 1.5 + 3 = 4.5 but 4.5 is a float value. So for code #1 the answer is 4.5, but the "sum" variable in code #2 is an integer so the answer will be rounded down to 4.

It because of cases like these that sometimes we implement a technique known as casting. Casting simply means changing the type of information a variable holds. With casting we can turn a int to a float, float to an int, str to an int, etc. We cast by writing the variable type we want ("float", "int", "str", etc.) followed by the variable/value we want to cast enclosed by parenthesis.

Examples:

  • float(3) → 3.0
  • int(2.9999) → 2
  • int("1964") → 1964


The first step is to download Python-

If you haven’t yet got python, the latest official installation packages can be found here: http://python.org/download/ Python 3 is preferable, being the newest version out!

The second step is to understand the different things we can do with it!

Ok, so python is this thing called a programming language. It takes text that you’ve written (usually referred to as code), turns it into instructions for your computer, and runs those instructions. We’ll be learning how to write code to do cool and useful stuff. No longer will you be bound to use others’ programs to do things with your computer - you can make your own!

We can use Python as a calculator, very advanced calculator, build softwares, games, and analyze data!

We will start with the most simple concept- variable!

We name a variable and stick to it a number, a word, a list etc. This way we can call it in the future. example for a variable is a=6.

If we want to print the variable we can do - print(a) and we will get 6.

In addition we can use python as a calculator- just type 6+6 and run it and you will get 12.


Types of inputs are integers, floats, string, and list (those are the basic ones that we will use)-

integer is a whole number-> int(), 6, 7

float is not a whole number-> 6.25, 7.3333

string is a word or a phrase- the written part of something-> 'My name is', '6', (we use the tags to help Python recognize it).

list is a list of integers, floats or strings-> [1,2,3,4,5], ['1','2','3','4','5']


Now that we are a bit more familiar lets see how we can use Python to solve Problems!!!

We will go to project Euler- https://projecteuler.net or http://rosalind.info/problems/locations/

The first one is more of solving math problems with Python (or any other software) and the latter is solving real life Bioinformatics problems!!

lets do together the first problem of Project Euler!


Another great source you can use in order to learn the basics of Python is-https://thepythonguru.com. This website provides pictures and explanations on the different structures we have in Python. You can also find there some more advanced programing tutorials.