0% found this document useful (0 votes)
3 views

Introduction+to+Python+Programming+(programming-based)_new+size

Uploaded by

otheruses022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction+to+Python+Programming+(programming-based)_new+size

Uploaded by

otheruses022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to Python Programming

You are expected to handle and analyse large volumes of data to draw inferences, and Python serves as an easy and effective tool to run different
operations. It is widely used in the scientific and research communities because of the simple syntax and a rich collection of libraries dedicated to various
aspects of data science.
As part of Python programming, you covered:
Data and its types: integer, float, string, Boolean
Data structures such as list, tuples etc. used to store data in Python
Various operations that can be performed over lists, tuples, dictionaries and sets
Control structures to help in decision making such as if-else statements, for and while loop
Define functions to execute repetitive tasks in Python

Programming in Python
Common Interview Questions:
1. What is the difference between a module and a package in Python?
2. How to convert a number into a string?
3. What is lambda function in Python?
Basics of Python Data Structures in Python
4. Explain the difference between a list and a tuple?
1. Data Types 1. List
5. How do you change the data type of a list? 2. Arithmetic operations 2. Tuples
3. String 3. Dictionary
6. What is the difference between generators and iterators?
4. String Operations 4. Sets
7. What is the range() function and wha t are its parameters?
8. Is Python case-sensitive?
9. What are functions?
10. How do you do data abstraction in Python?
11. What are the escape sequences in Python? Control Structures in Functions
12. What is an Expression? Python 1. Comprehension
1. If-else statement 2. Lambda functions and
2. For loops it's use
3. While loops 3. Range
4. Comprehensions 4. Map, filter and reduce
functions
5. Functions
Introduction to Python Programming
Data Type Syntax Data Structures List Tuples Sets

Integer int Definitions Lists are like arrays but it can Tuples are containers for Contains all types of data with no duplicate
contains different types of data holding different types entry
Float float together whereas arrays can of values
hold same type of data
String str
Boolean bool Mutable/
Mutable Immutable Mutable
Immutable
Example list_1 = [1,2,3,4,5] tuple_1 = (1,2,3,4,5) set_1 = {1,2,3,4,5}
Arithmetic
list_1[1] #2nd value - 2 list_1[1] Is unordered: hence index of element
#Float division Indexing
#2nd value - 2 is not defined on sets
133//10 #13
Length len(list_1) #5 len(list_1) #5 len(list_1) #5
#Modulus division
133%10 #3 Sum of elements sum(list_1) #15 sum(tuple_1) #15 sum(set_1) #15
#PEMDAS rule min(list_1) #1 min(tuple_1) #1 min(set_1) #1
3*3-(4+3)//2 #6 Find min/max
max(list_1) #5 max(tuple_1) #5 max(set_1) #5

Add more list_1.append(6) Tuple objects are set_1.add(6)


element #[1,2,3,4,5,6] immutable #{1,2,3,4,5,6}
String
Additional del list_1[2] #[1,2,4,5,6] set_2 = {3, 4}
#Syntax: 'string' or "string" operations #Remove last item in a list #union #{1,2,3,4,5,6}
#\ is escape character: set_1 | set_2
list.1.pop() #[1,2,4,5]
'I don\'t know' set_1.union(set_2)
#\n-newline character #Sorting with replacement
print('Line1 \n Line2') list.1.sort() #[1,2,4,5] #Intersection #{3,4}
str1 = 'first' set_1 & set_2
str2 = 'last' set_1.intersection(set_2)
#Sorting without replacement
#concatenation #Difference #{1,2,5,6}
Sorted(list_1) #[1,2,4,5]
str3 = str1+str2 #firstlast set_1 - set_2
#Access any character set_1.difference(set_2)
str1[1] #f #Symmetric Difference #{1,2,5,6}
str1.upper() #FIRST set_1 ^ set_2
str1.lower() #first
str1.replace('f','th') #thirst Slower in performing iterations Consume less memory but has Significantly faster in checking if an element
str1.count('f') #1 than a tuple very less in-built methods is present then lists or tuples
Has many built-in methods Implications are comparatively
Takes more memory as compared Slower than list or tuples when performing
Importance faster than the list
iterations
to tuple
Better than the list for
Better for operations, such as
accessing elements
insertion and deletion
Introduction to Python Programming
Dictionaries: key:value relationship
Basis of
dictionary = {'john':'Professor','Martin':'Engineer'} For loop While loop
#Change value of an existing key Comparison
dictionary['John']='Doctor' for object in sequence: while condition:
Declaration
#{'John':'Doctor','Martin':'Engineer'} <indentation>body <indentation>body
del dictionary['John'] # {'Martin':'Engineer'} When you already know the number of When you don’t know the number of
dictionary['James'] = 'Scientist' Use Case
iterations iteration.
dictionary.update({key:value}) #Add a key:value pair
dictionary.keys() #Retrieve all keys Every element is fetched through the Every element is explicitly incremented
Iteration
dictionary.values() #Retrieve all values iterator/generator. Example, range() or decremented by the user

Generator For loop can be iterated on generators in While loop cannot be iterated on
Conditional statement: Support Python generators directly

If-else statement: To execute the true or the false part of a given Generator For loop can be iterated on generators in While loop cannot be iterated on
condition
Support Python generators directly
If-elif-else statement: Used for decision-making
Speed of
For loop is faster than while loop While loop is slower than for loop
a = float(input()) execution of code
if (a <= 20):
num = int(input('Integer value:')) num = int(input('Integer value:'))
print ('a is less or equal to 20')
if num>0: if num>0:
elif (a>20 and a <= 30): fac = 1 fac = 1
print ('a is in between 20 and 30') for i in range(1, num + 1): i = 1
elif (a>30 and a <= 40): Example: fac = fac * i while i <= num:
print('Answer', num) fac = fac * i
print ('a is in between 30 and 40') Finding factorial i = i + 1
elif num<0:
else: print('Invalid input') print('Answer:', fac)
print ('a is greater than 40') else: elif num<0:
print('Zero factorial is 1') print('Invalid input')
else:
print('Zero factorial is 1')

#The following methods are executed along with the for loop to iterate over each element in a list, set or dictionary

#List comprehension #Dictionary comprehension #Set comprehension


#Check if number is divisible by 2 #Length of words #Multipying each number by 3
[x for x in range (5) if x%2 == 0] words = ['data', 'science'] set1 = {1,2,3,4}
#[0, 4, 16] {i: len(i) for i in words} set1 = {i*3 for i in set1}
#{'data':4, 'science': 7}
Introduction to Python Programming

Function

#Square of any given number: def sum (a,b) : colon map()


k = float(input()) items = [1,2,3,4,5]
square = lambda x: x**2
def square(k):
Parenthesis list (map(square, items))
return k**2 #[1, 4, 9, 16, 25]
Parameters
square(k)
Function name
def Keyword filter()
list(filter((lambda x: x < 0),range(-5,5)))
Lambda Function #[-5, -4, -3, -2, -1]
Used to define a single expression with any number of arguments
Helpful when the function is used for 1 or 2 instances
reduce()
square = lambda x: x**2 #square(10) #100 #Find factorial:
from functools import reduce
n = int(input())
Range: Print all integers in a certain interval if n>0:
Syntax: range(start, stop, step) print('Factorial of',n,'is',reduce((lambda x, y:x * y),range(1,n+1)))
elif n==0:
#all integers from 0(included) upto 10(excluded)
print (1)
range(0,10)
else:
#print all elements of the range in list format
print('Factorial is not defined on negative integer')
list(range(0,10) #[0,1,2,3,4,5,6,7,8,9]
list(range(10,0,-2)) #[10,8,6,4,2]

You might also like