y = mx + c
y – Dependent variable(DV)
x – Independent variable(IV)
c – constant
dataset = [library alias name].read_csv(‘file.csv’)
x = dataset.iloc[:,:-1].values
y = dataset.iloc[:, -1].values
In the above lines, read_csv and iloc are functions.
[:,:-1] is slicing range of the values.
import [library] as 
a=10 b=5 add = a+b sub = a-b mul = a*b div = a/b mod = a%b print(add) print(sub) print(mul) print(div) print(mod)
Simple Python Program
print('python is big!')
A function is the simplest callable object in Python.
NOTE: For Indentation either use TAB space or 4 space characters, never both.
“””Adds 25% tax to a restaurant bill.”””
bill *= 1.25
print “With tax: %f” % bill
“””Adds 35% tip to a restaurant bill.”””
bill *= 1.35
print “With tip: %f” % bill
meal_cost = 100
meal_with_tax = tax(meal_cost)
meal_with_tip = tip(meal_with_tax)
def spam(): /*Header*/
“”” This is spam function””” /* Comment*/
print “Spaceship!” /*body*/
Call and Response
“””Returns the square of a number.”””
squared = n**2
print “%d squared is %d.” % (n, squared)
Parameters and Arguments
A function can require as many parameters as you’d like, but when called the function, it should generally pass in a matching number of arguments.
def power(base, exponent): # Add your parameters here!
result = base**exponent
print “%d to the power of %d is %d.” % (base, exponent, result)
power(20, 6) # Add your arguments here!
Functions Calling Functions
A function can call another function.
return n + 1
return n_plus_one(n) + 2
A module is a file that contains definitions, including variables and functions, that we can use once it is imported.
math includes a number of useful variables and functions, and sqrt() is one of those functions. In order to access math, all you need is the import keyword. When you simply import a module this way, it’s called a generic import.
It’s possible to import only specific variables or functions from a given module. Pulling in just a single function from a module is called a function import.
# Import *just* the sqrt function from math
from math import sqrt
if we want all the variables and functions in a module but don’t want to have to constantly type math then we can use
# Import *everything* from the math module
from math import *
The max() function takes any number of arguments and returns the largest one.
>> maximum = max(10,35,64)
>> print maximum
min() function returns the smallest of a given series of arguments.
>> minimum = (10,35,64)
>> print minimum
The abs() function returns the absolute value of the number it takes as an argument, it gives the number’s distance from 0 on an imagined number line.
>> absolute = abs(-42)
>> print absolute
The type() function returns the type of the data it receives as an argument.
>> print type(42)
>> print type(4.2)
>> print type(‘spam’)
Now let us see the code which returns biggest, smallest and absolute values.
biggest_number(-105, -53, 51, 100)
smallest_number(-106, -55, 54, 105)
These are some of the functions and built-in functions in python.
Using dot notation with strings.
Let us consider an example where we convert the string to upper case using dot notation.
>>> mysore = “The city is awesome !”
>>> print len(mysore)
>>> print mysore.upper()
>>> print(“This String”)
>>> Stone_cold = “Stunner”
Combining strings together is called concatenation . The + operator combine the strings.
>>> print “Indian “+ “Air ” +”Force ”
The space after the word is to make the string look like sentence.
The str() method converts non-strings into strings.
>>> print “Sourav Ganguly scored “+ str(100) + “on his Test Debut”
The % operator after a string combines a string with variables. The % operator will replace %s in the string with the string variable.
>>> string_1 = “Sourav”
>>> string_2 = “Dada”
>>> print “Indian captain %s is also called as %s.” % (string_1, string_2)
>>> batsman = raw_input(“Who is the batsman?”)
>>> captain = raw_input(“Who is the captain?”)
>>> series = raw_input(“Which series did he win for India?”)
>>> print “The Indian %s and %s Sourav Ganguly won %s series for India.” % (batsman, captain, series)
Date and Time
We can import datetime function from datetime module.
>>> from datetime import datetime
Current Date and Time
Here is the code to print current date and time.
>>> from datetime import datetime
>>> now = datetime.now()
>>> print datetime.now()
The ability to choose from different situations based on what’s happening in the program.
>>> x = int(input(“Please enter an integer: “))
Please enter an integer: 35
>>> if x < 0:
… x = 0
… print(‘Negative changed to zero’)
… elif x == 0:
… elif x == 1:
if statements, for statements, range fuction, break and continue, else clause loops, pass statements are few of the control flow statements.
Equal to (==)
Not equal to (!=)
Less than (<)
Less than or equal to (<=)
Greater than (>)
Greater than or equal to (>=)
The statements used for decision making are called conditional statements.
This post concludes basics of python.
Download the python interpreter from this link . Choose either 3.0 or 2.0 version depending upon your compatibility. In this exercise version 2.0 was used for examples. Based on the current and future trends I would recommend 3.0.
Now let us look into some basic concepts in python programming language.
As with most programming languages, assigning a value for a variable is as simple as:
a = 10
The two switch states True or False can be assigned to a variable.
a = True
a = False
Reassigning variable values:
A variable b = 10 can be given a value as b = 5 in the next line, and the value can be printed using
Indentation is one of the most important aspect of python, either use a Tab space or 4 white spaces but not both. Here’s an example of a simple python function.
var = 15
Use # at the beginning of a line to give a single line comment.
# This is a single line comment in python.
If the comment is more than one line use double inverted comma’s thrice at the start and end of the comment.
“”” This is a multi-line
Basic Math Operations:
All basic math operations such as addition(+), subtraction(-), multiplication(*) and division(%) can be performed similar to other programming languages.
>>print 36 + 64
>>print 200 – 100
>>print 10 * 10
>>print 1000 / 10
In python, the function of exponential can be performed using ** keys.
>> chairs = 10 ** 2
>> print chairs
Modulo operation can be performed using %.
Assigning strings values and printing them.
python = “Hello world!”
In python both single quotes and double quotes can be used for commenting. When using single quote, the interpreter doesn’t understands the code. In such cases we can use escape characters such as backslash(\).
‘Isn\’t it the python?’
Accessing by Index:
We can access any character of a string by indexing. Indexing starts from 0.
>>sixth_letter = “python”
We can calculate length of the string using len()
>>python = “Indian”
We can convert lower case string to upper case string
>>python = “indian”
We can convert upper case string to lower case string
>>python = “INDIAN”
We can convert a variable to a string
>>pi = 3.14
So, this concludes basics of python part-1.