Once Anaconda / Mini Conda has been installed, open up command prompt and type the command:
(1) Please type
conda update conda
(2) Then create environment using;
Conda create - - name (any name) anaconda
Conda info - -envs
activate envsname
(3) If conda asks; ‘proceed([y]/n)’ , type ‘y’ which means "yes" to proceed
(4) To launch jupyter notebook, type;
Jupyter notebook
Make sure you press ENTER after ever command.
However, if miniconda is downloaded instead of anaconda, jupyter notebook must be downloaded separately. This version of the jupyter notebook doesn't require the internet.
print ('Hello World')
Then, when you press ENTER, you can see this output:Hello World
Python is one of the easiest language to learn and create.
PROGRAM TO ADD TWO NUMBERS
# Add two numbers
num1 = 3
num2 = 5
sum = num1+num2
print(sum)
Next, we proceed with the program that add two numbers.
Line 1: # Add two numbers. Any line starting with # in Python programming is a comment. Comments are used in programming to describe the purpose of the code. This helps you as well as other programmers to understand the intent of the code. Comments are completely ignored by compilers and interpreters.
Line 2: num1 = 3
Here, num1 is a variable. You can store a value in a variable. Here, 3 is stored in this variable.
Line 3: num2 = 5
Similarly, 5 is stored in num2 variable.
Line 4: sum = num1+num2
The variables num1 and num2 are added using + operator. The result of addition is then stored in another variable sum.
Line 5: print(sum)
The print() function prints the output to the screen. In our case, it prints 8 on the screen.
The output display on screen is
8
PYTHON VARIABLES
A variable is a location in memory used to store some data (value).They are given unique names to differentiate between different memory locations. In Python, we simply assign a value to a variable and it will exist. We don't even have to declare the type of the variable. This is handled internally according to the type of value we assign to the variable. We use the assignment operator (=) to assign values to a variable. Any type of value can be assigned to any valid variable.
a = 5
b = 3.2
c = "Hello"
In Python, multiple assignments can be made in a single statement as follows:
a, b, c = 5, 3.2, "Hello"
- If we want to assign the same value to multiple variables at once, we can do this as
x = y = z = 1
DATA TYPES
There are various data types in Python. Some of the important types are listed below.
1 .Python Numbers
Integers, floating point numbers and complex numbers falls under Python numbers category. They are defined as int, float and complex class in Python.
Integers can be of any length, it is only limited by the memory available.
A floating point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. 1 is integer, 1.0 is floating point number.
Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part. Here are some examples.
a = 1234567890123456789
b = 0.1234567890123456789
c = 1+2j
2. String
String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ''' or " " ".
s = "This is a string"
s = '''a multiline
3. List
List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.
Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].
a = [1, 2.2, 'python']
We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.
a = [5,10,15,20,25,30,35,40]
# a[2] = 15
print("a[2] = ", a[2])
Output :
a[2] = 15
# a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])
Output :
a[0:3] = [5, 10, 15]
# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])
Output :
a[5:] = [30, 35, 40]
colors = ['red', 'blue', 'green']
print colors[0] ## red
print colors[2] ## green
print len(colors) ## 3
Output:
red
green
3
4. Dictionary
Dictionary is an unordered collection of key-value pairs.
It is generally used when we have a huge amount of data. Dictionaries are optimised for retrieving data. We must know the key to retrieve the value.
In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.
d = {1:'value','key':2}
print(type(d))
Output :
<class 'dict'>
print("d[1] = ", d[1]);
Output :
d[1] = value
print("d['key'] = ", d['key']);
Output :
d['key'] = 2
# Generates error
print("d[2] = ", d[2]);
5. Set
Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.
a = {5,2,3,1,4}
# printing set variable
print("a = ", a)
# data type of variable a
print(type(a))
CONVERSION BETWEEN DATA TYPES
We can convert between different data types by using different type conversion functions like int(), float(), str() etc.
float(5)
Output : 5.0
int(10.6)
Output: 10
str(25)
Output : '25'
set([1,2,3])
Output : {1, 2, 3}
tuple({5,6,7})
Output : (5, 6, 7)
list('hello')
Output : ['h', 'e', 'l', 'l', 'o']
For more details, we can learn this program in the tutorial in Software Carpentry
Last but not least, download python-novice-inflammation-data.zip and extract the files.
REPEATING ACTION WITH LOOPS
The loop variable was given the name char as a mnemonic; it is
short for ‘character’. ‘ Char is not a keyword in Python that pulls the
characters from words or strings.
list
= ['oxygen','nitrogen','argon']
for
char in list:
print(char)
oxygen
nitrogen
argon
We can choose any name we want for variables.We might
just as easily have chosen the name banana for the loop variable.It is a
good idea to choose variable names that are meaningful so that it is easier to
understand what the loop is doing.
word
= 'oxygen'
for
banana in word:
print(banana)
o
x
y
g
e
n
Here is another loop
that repeatedly updates a variable:
length
= 0
for
vowel in 'aeiou':
length = length + 1
print('There
are', length, 'vowels')
There
are 5 vowels
The loop variable is
still exists even after the loop is over and we can re-use variables previously
defined as loop variables as well:
letter
= 'z'
for
letter in 'abc':
print(letter)
print('after
the loop, letter is', letter)
a
b
c
after
the loop, letter is c