Skip to main content

Python Programming Basics

Python is one of the powerful languages which has picked up the popularity after Machine Learning and Artificial Intelligence has boomed. I don't want to highlight on the why Python but how Python can be used.

Here are the list of contents

1. Installation
2. Libraries
3. Data Structures
4. Loops
5. Exercises



1. Installation


You get Python from Anaconda :)
Download Anaconda for your respective environments and install

Launch Anaconda Navigator and you will see few options like Jupyter notebook, spyder, orange, rstudio etc. launch Jupyter notebook for python.

Jupiter notebook will launch on your default browser and select the "New" -> "Python 3" option.
A new tab will be opened with option to play with python.

Click on the Untitled and rename appropriately as per your exercise/chapter.
That's it we are all set for to get our hands dirty.. Let's jump into programming.




2. Libraries


            For the basic programming Python provides default libraries which need not be imported, but you would always want to take the advantage of inbuilt libraries for complex scenarios and faster execution then you need to import libraries.

Some of the libraries you would import are


  1.    numpy
  2.    pandas
  3.    matplotlib
  4.    scipy
  5.    sklearn


2.1 Numpy:

     Numpy is package used for Scientific Computing like

     a. Multi dimensional array
     b. Methods for processing arrays
     c. Linear algebra


2.2 Pandas:
 
    Pandas are used for data analysis, we can analyze the data in Pandas with
 
    a. Series - one dimensional array
    b. DataFrames - two dimensional array consists of rows and columns.

    Pandas are used to read the data from files (csv, tsv, excel, sql, json, url, dict)
    Here is the cheatsheet for pandas for your reference.


2.3 Matplotlib:

    Matplotlib is used for data representation in a visual form. The visualization of the data can be represented in the below formats

    a. Bar Graphs
    b. Pie Charts
    c. Histogram
    d. Scatter Plots and 3-D plotting




3. Data Structures

      To hold the data we need some container or objects and also data could be of different type and formats, hence we have different data structures to hold these different types of data.

      a. String, Int, Float
      b. List
      c. Tuple
      d. Dictionary
   

String

    String is a basic datatype which is used for storing the data.

    Examples:
        name = "Mr Python"
        mobile = "3232001231"
        address = "First Street, Frankfurt"

   Operations: String operations can be found here.

   Let's look at few of the operations:
 
   1. Length: of the string:

   Syntax: len(str)  //where str is any string
   Example:
   name = "Mr Python"
   length = len(name)
   print(length)

   2. Capitalize:  Converts the first letter of the String to Upper case.

   Syntax: str.capitalize()  //where str is any string
   Example:
   name = "python"
   # name.capitalize()
   print( name.capitalize())  //returns 'Python'

   Similarly we have upper() lower() and swapcase() for changing the alphabets to upper,lower and         viceversa using these operations
   title() - returns title case version of the string.

   3. Count: returns the no. of occurrences of the substring.
 
   Syntax: str.count(substr)  //where str is any string and substr is any string
   Example:
   sentence = "python is a powerful language, is that true?"
   count =  sentence.count("is")
   print(count) //returns 2

  4.  Find: returns the index of the substr if present in the str

   Syntax: str.find(substr)  //where str is any string and substr is any string
   Example:
   sentence = "python is a powerful language, is that true?"
   count =  sentence.find("powerful ")
   print(count) //returns 12

  4.  Index: returns the index of the substr if present in the str

   Syntax: str.index(substr)  //where str is any string and substr is any string
   Example:
   sentence = "python is a powerful language, is that true?"
   count =  sentence.index("powerful ")
   print(count) //returns 12

  5. Format: allows simple placeholders for formatting the statement

   Syntax: format(str)  //where str is any string
   Example:
   sentence = "Welcome {}, your room no is {}."
   sentence =  sentence.format("Hillton", 3033)
   print(sentence ) //prints Welcome Hillton, your room no is 3033

   6. isalnum: returns true if all the charecters of the string are alphanumeric

   Syntax: str.isalnum()//where str is any string
   Example:
   sentence = "Welcome123"
   print(sentence.isalnum()) //prints true
   sentence = "Welcome_123"
   print(sentence.isalnum()) //prints false
   sentence = " Welcome 123 "
   print(sentence.isalnum()) //prints false

  Similarly we have the following functions which are self explanatory
  isalpha()
  isdecimal() 
  isdigit()
  isnumeric()

  7. join: concatenates two strings
 
  Syntax: str.join(str2)//where str, str2 are any strings
  Example:
  sentence1 = 'Welcome123'
  sentence2 = 'Welcome123'
  print(sentence1.join(sentence2)) //prints Welcome123Welcome123

 8. lstrip: left strip - removes the leading characters

 Syntax: str.lstrip(str2)//where str, str2 are any strings
 Example:
 sentence1 = 'Welcome123'
 sentence2 = 'Wel'
 print(sentence1.lstrip(sentence2)) //prints true come123

 9. rstrip: right strip - removes the trailing characters

 Syntax: str.rstrip(str2)//where str, str2 are any strings
 Example:
 sentence1 = 'Welcome123'
 sentence2 = '123'
 print(sentence1.rstrip(sentence2)) //prints Welcome

 10. strip: removes the leading & trailing characters

 Syntax: str.strip(str2)//where str, str2 are any strings
 Example:
 sentence1 = '123Welcome123'
 sentence2 = '123'
 print(sentence1.strip(sentence2)) //prints Welcome

11. reversed: returns the List of reversed string with each characters as elements

 Syntax: reversed(str)//where str, is any strings
 Example:
 sentence1 = 'Welcome123'
 print(list(reversed(sentence1))) //prints ['3', '2', '1', 'e', 'm', 'o', 'c', 'l', 'e', 'W']



Lists


Tuples


Dictionaries


4. Loops

5. Exercises

1. Write a program to reverse a String.

Solution using loop:
givenString = input("Enter a String")
len1 = len(givenString)
rev = ""
while(len1 > 0):
    rev += givenString[len1 -1]
    len1 = len1 -1

print(rev)

Solution using slicing:

str1 = input("Enter a String")
rev = str1[:: -1]
print(rev)

Solution using Join & reversed functions:

str2 = input("Enter a String")
rev = ''.join(reversed(str2))
print(rev)




2. Write a program to find out if the given String is palindrome or not.

Solution:

str3 = input("Enter a String")

rev = str3[::-1]
if(rev == str3):
    print("The given string is palindrome")
else:
    print("The given string is not a palindrome")



3. Write a program to convert the list to list of squares
     Given list1 = [1,2,3,4,5] Expected list2 = [1,4,9,16,25]
4. list prime numbers from 1 to 30
5. factorial of a given number.
6. Fibonacci series upto given number.


3. Write a program to convert each letter in the String to upper case if it is lower case and vice-versa:

Solution:

str3 = input("Enter a String")
caseStr = ""
for char in str3:
    if(char.isupper()):
        caseStr += char.lower()
    else:
        caseStr += char.upper()
print(caseStr)


4. Write a program to return the highest number which occurs in a series of number and how many times does it occur in the series.


ar = [23,46,23,56,]

\print(ar)
max_num = 0
for num in ar:
    if(num>max_num):
        max_num = num
    
       
print(max_num)


5. Write a program to print the below order for n =5

1
11
111
1111
11111

n = 5
for i in range(1, n+1):
    print((10**i//(9)))

1
22
333
4444
55555

n = 5
for i in range(1, n+1):
    print((10**i//(9) * i))

1
121
12321
1234321

n = 5
for i in range(1, n):
    print((10**i//9) ** 2)

6. Write a program to Print 'Gent' or 'Lady' with the combination of substring words possible from a given string. Condition: Print 'Gent'  and no.of words that starts with CONSONENTS and 'Lady' and no.of words that starts with VOWELS.


str = 'ELEPHANT'
strLen= len(str)
Gent = 0
Lady = 0
vowels = 'AEIOU'


for i in range(strLen):
    if(str[0] not in vowels):
        Gent += strLen-i
    else:
        Lady += strLen-i
if(Gent > Lady ):
    print("Gent {}".format(Gent ))
elif(Kevin > Stuart):
    print("Lady {}".format(Lady ))
else:
    print("Nutral")

7. How to read a CSV file:


import pandas as pd

data = pd.read_csv("xyz.csv")
data.head(20)

//with encoding
data = pd.read_csv('xyz.txt',delimiter="\t", encoding = 'palmos')
data .head(20)

8. import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

countries = pd.read_csv('D://$Kiran/personal/IIIT/companies.txt',sep = "\t",encoding = "ISO_8859_1")
countries
#countries.isnull().sum()
#countries.isnull().all(axis=1).sum()
//Displays the column's and the percentage of null values.
round(100*(countries.isnull().sum()/len(countries.index)), 2)

//Based on the above drop the columns which are having highest percentage of null values
countries = countries.drop('founded_at', axis=1)
countries = countries.drop('state_code', axis=1)
countries = countries.drop('region', axis=1)
countries = countries.drop('city', axis=1)
countries = countries.drop('homepage_url', axis=1)

round(100*(countries.isnull().sum()/len(countries.index)), 2)
countries[countries.isnull().sum(axis=1) > 1]



rounds2 = pd.read_csv('D://$Kiran/personal/IIIT/rounds2.csv', encoding = "ISO_8859_1")
round(100*(rounds2.isnull().sum()/len(rounds2.index)), 2)
rounds2 = rounds2.drop('funding_round_code', axis=1)
rounds2[np.isnan(rounds2['raised_amount_usd'])]

##apply the median value for the NaN values for the raised_amount_usd
rounds2.loc[np.isnan(rounds2['raised_amount_usd']), ['raised_amount_usd']] = rounds2['raised_amount_usd'].median()

# Filter the dataframe with 'private_equity'
gapminder_2007 = rounds2[rounds2['funding_round_type']=='private_equity']
gapminder_2007.shape

gapminder_2007.boxplot(by='company_permalink', column=['raised_amount_usd'], grid=False)
import matplotlib.pyplot as plt
plt.show()




df['gender'].value_counts()

 output:

 M=120
 F=80
With Normalize and Percentages
df['gender'].value_counts(Normalize=True)*100 #will convert output to percentages

  output:

  M=60
  F=40

7. Get the number from the list which appears odd no.of times.

def getOddEntry(lst):
 for i in lst:
  if lst.count(i) % 2 == 1:
   return i

8. Two List compare between same indexes.

return a_list[1] > b_list[0] and a_list[2]>b_list[1]

9. Input List with 











10. +ve no of integers/ Sum of -ve numbers 

def sum_neg(nums): if not nums: return [] return [len([n for n in nums if n > 0]), sum(n for n in nums if n < 0)]



11. List to Unique List

return sorted(set(give_list))

12. Return the max no.of 

-> "000" or "00000"
return max(s.split('1'))

13. rever + map + concatinate

-> input word
-> output magic("banana") ➞ "0n0n0baca"
sol 1:
return word[::-1].translate(str.maketrans('aeou', '0123')) + 'aca'
sol 2:
v= {'a':'0','e':'1','o':'2','u':'3'} return ''.join(v[i] if i in v else i for i in word[::-1]) +'aca'
sol 3:
word = word[::-1]
str1 = '' for i in word: if(i == 'a'): str1 = str1 + str(0) elif(i == 'e'): str1 = str1 + str(1) elif(i == 'o'): str1 = str1 + str(2) elif(i == 'u'): str1 = str1 + str(3) else: str1 = str1 + str(i) str1 = str1 + 'aca' return str1

14. return first digit in a string 

for i in word[:]: if i.isdigit(): return int(i)

15. Factorial


sol 1:
fact(n)
return n*fact(n-1) if n>1 else 1
sol 2:
def fact(n): count = 1 for i in range(1, n+1): count = count * i return count


15. Upper or Lower Case with ASCII code reversed

sol 1:
input Xchar return ord(Xchar.upper()) if Xchar.islower() else ord(Xchar.lower())
sol 2:
return ord(Xchar.swapcase())

16. Split string with multiple delimiters

import re text = 'The quick brown\nfox jumps*over the lazy dog.' print(re.split('; |, |\*|\n',text))
Output:
['The quick brown', 'fox jumps', 'over the lazy dog.']

17. Eval function - to check a given string condition

input:
1. 2<3<-1>=9<0
2. -5<-2<0=0<3<6<200
return eval(txt.replace('=', '==').replace('<==', '<=').replace('>==', '>='))

18. Eval function - to check a given string condition

Sample in out:
pie_chart({ "a": 1, "b": 2 }) ➞
{ "a": 120, "b": 240 } pie_chart({ "a": 30, "b": 15, "c": 55 }) ➞
{ "a": 108, "b": 54, "c": 198 } pie_chart({ "a": 8, "b": 21, "c": 12, "d": 5, "e": 4 }) ➞
{ "a": 57.6, "b": 151.2, "c": 86.4, "d": 36, "e": 28.8 }
Sol:
def draw_pie(data): result = dict() total = sum(data.values()) for i in data: result.update({i: round(data[i]/total *360, 1)}) return result

19. input string and add them with condition

sol:
def add_strings(num1, num2):
# not "" if not num1: num1 = '0' if not num2: num2 = '0' try: return str(int(num1) + int(num2)) except ValueError: return '-1'

20. return True or False if the first and second charecters are not in order of first<second index

sol 1:
def first_then_second(s1, xword, yword): return s1.rindex(xword) < s1.index(yword)
sol 2:
def first_then_second(s1, xword, yword): x = [] y = s1.find(yword) for i in range(0, len(s1)): if s1[i] == xword: x.append(i) flag = True for i in x: if(i > y): flag = False return flag return flag

21. check the duplicate char's in each string of the phrase

Sol 1:
Output: True/False
def duplicate_chars_word_phrase(phrase): return all(i.count(j)==1 for i in phrase.split() for j in i)
Sol 2:
def duplicate_chars_word_phrase(phrase): splt = phrase.split(" ") for word in splt: l = len(word) lst = [] for i in word: lst.append(i) if(len(set(lst)) != l): return False return True

22. check the dots in 5 sided 

outout:
side = 1, => 1
side = 2, => 6
side = 3, => 16
side = 8, => 141
def 5Side(side): ans = 1 for i in range(1,side): ans += (5*i) return ans

23. List of duplicates count which element has more no.of entries

input: ['J', 'K', 'L', 'L', 'J', 'L']
output: 'L'
Sol 1:
def max_entries(lst): for i in set(lst): if lst.count(i)>len(lst)//2: return i return None
Sol 2:
def max_entries(lst): if len(lst) == 0: return None keys = dict() for i in lst: if keys.get(i) != None: keys.update({i:keys.get(i) + 1}) else: keys.update({i: 1}) max_value = max(keys.values()); if max_value > len(lst)/2: return max(keys, key=keys.get) else: return None

24. Date conversion to a list format 

import dateutil.parser def convert_date(date): date = dateutil.parser.parse(date) return [date.month, date.day, date.year]

25. Check is the list of lists with numbers form a circle.

input: [[2, 1], [1, 2]]
output: True
input: [[2, 1], [1, 0]]
output: False
def is_circular(lst): return sorted(i[0] for i in lst)==sorted(i[-1] for i in lst)

http://onlinestatbook.com/2/regression/intro.html

linear regression algorithm:

Linear Regression comes under Supervise Machine Learning methods, where the past data with labels
is used for building the model. A simple linear regression model explains the relationship between a dependent (output variable) and
an independent (predictor variable) using a straight line which is also called as Best Fit Line.
The equation of best fit regression line is y = β₀ + β₁X + ϵ
interpret this as 'β₁' the average effort on 'y' for a one unit increase in 'X'.
β₀ is Y-intercept of the line
β₁ is Slope of the line
ϵ is the residual error
Example:
Imagine Y as Distance traveled, X is the speed
AIM: to find the best fit line using Least Square Method


X
ȳ
X-X̄
ȳ -ȳ
(X-X̄)2
(X-X̄) * (ȳ -ȳ)

1
3
-2
-0.6
4
1.2

2
4
-1
0.4
1
-0.4

3
3
0
-1.6
0
0

4
4
1
1
1
0.4

5
5
2
1.4
4
2.8
Mean
3
3.6


10
4

Comments

Popular posts from this blog

Naive Bayes

Naive Bayes Naïve Bayes is a probabilistic classifier that returns the probability of a test point belonging to a class P(Ci|x) = P(x|Ci)P(Ci)/ P(x) where Ci denotes the classes, and X denotes the features of the data point. Ex: C1, C2 or C = edible/poisonous The feature ‘cap-shape’ is represented by X and X can take the values CONVEX, FLAT, BELL, etc The probability of a CONVEX mushroom being edible, P(C = edible | X = CONVEX) is given by: P( X = CONVEX | C = edible) . P(C = edible) / P(X = CONVEX) Comprehension - Naive Bayes with Two Features S.No Type of mushroom   Cap shape 1.       Poisonous         Convex 2.       Edible Convex 3.       Poisonous Convex 4.       Edible Convex 5.       Edible Convex 6.       Poisonous Convex 7.       Edible Bell 8.       Edible Bell 9.    ...

Advanced Regression.

Advanced Regression. - Generalized Linear Regression - Regularized Regression - Ridge and Lasso Regression Generalized Linear Regression process consists of the following two steps: 1. Conduct exploratory data analysis by examining scatter plots of explanatory and dependent variables. 2. Choose an appropriate set of functions which seem to fit the plot well and build models using them. Functions having no global maxima or minima are usually polynomial functions. Also, they typically have multiple roots and local maxima and minima. Ex: ax^4+bx^3+cx^2+dx+f Monotonically increasing function Ex: e^x 1. Can x1.x2.x3 be a feature if the raw attributes are x1, x2, x3 and x4? A. Yes, Derived features can be created using any combination of the raw attributes (linear or non-linear). In this case, the combination x1. x2. x3 is non-linear. 2. How many maximum features can be created if we have d raw attributes for n data points? Note that (n r)  here refers to the number of...

Model Selection

Model Selection: Finally, you learned 4 unique points about using a simpler model where ever possible: A simpler model is usually more generic than a complex model. This becomes important because generic models are bound to perform better on unseen datasets. A simpler model requires less training data points. This becomes extremely important because in many cases one has to work with limited data points. A simple model is more robust and does not change significantly if the training data points undergo small changes. A simple model may make more errors in the training phase but it is bound to outperform complex models when it sees new data. This happens because of overfitting. Complexity: The disadvantages the first person is likely to face because of a complex model are (mark all that apply): 1. He’ll need more training data to ‘learn’ 2. Despite the training, he may as well not learn and perform poorly in the real world Overfitting: The possibility of overfitting exist...