0% found this document useful (0 votes)
65 views36 pages

Day 1

This document provides an introduction to Python including why it should be learned, its basic usage and applications. It demonstrates various Python concepts like variables, data types, strings, lists, dictionaries, tuples and sets through examples of code snippets and their output. Key concepts covered include arithmetic operations, string indexing and slicing, list operations, accessing elements in dictionaries and tuples.

Uploaded by

shan halder
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)
65 views36 pages

Day 1

This document provides an introduction to Python including why it should be learned, its basic usage and applications. It demonstrates various Python concepts like variables, data types, strings, lists, dictionaries, tuples and sets through examples of code snippets and their output. Key concepts covered include arithmetic operations, string indexing and slicing, list operations, accessing elements in dictionaries and tuples.

Uploaded by

shan halder
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/ 36

Let's Start python......

​ ¶ 
 
 
 
why python???​¶ 

 
 
 

Reqirement to learn python.................​¶ 


1. freedom of thinking
2. computer typing.....

use of python ....​¶ 


 
 
 

BYEEEEEEEEEEEEEEEEE​ ¶ 
 
 
 
 
Very easy to use :)​¶ 

 
 
 

one big question ..... is there any job????​¶ 


 
 
 
 
 
 
 
 
 
 
 
Python has two number types integer and floating point number​¶ 
 
 
In [1]:
# Addition
a​=3
b​=4
a​+​b

Out[1]:

In [2]:

# Subtraction
1​ ​-​ ​3

Out[2]:

-2

In [3]:

# Multiplication
4​ ​*​ ​6

Out[3]:

24
In [4]:

# Division always returns floating point number


2​ ​/​ ​2

Out[4]:

1.0

In [5]:

# Floating point arithmetic


2.0​ ​+​ ​4.0

Out[5]:

6.0

In [6]:

# Exponential operation
2​ ​**​ ​6

Out[6]:
64

In [2]:

a​=5
print​(a)

¶ 
 
 
 
Variable assignment in Python​¶ 
 
 
In [7]:
var ​=​ ​1

In [8]:

var

Out[8]:

In [9]:

x=
​ ​4 ​
y=​ ​9 ​
x+ ​ ​y

Out[9]:

13

In [10]:

x ​=​ x ​**​ x
x
Out[10]:

256

Variable cannot begin with numbers, special character etc​¶ 


 

In [3]:

12​var ​=​ ​5

File ​"<ipython-input-3-84843064e9c2>"​, line ​1


12var = 5
^
SyntaxError:​ invalid syntax

Strings​¶ 
 
 
In [2]:

'Hello'
Out[2]:

'Hello'

In [3]:

"Hello I'm John"


# If you have single quotes in your string

Out[3]:

"Hello I'm John"

In [4]:

str​ ​=​ ​"hello"


str

Out[4]:

'hello'

In [5]:

print​(​'str'​)
str

In [2]:

xyz​=​(​"jdhvdscvgsdchjbshvhsbchsasbch"​)
print​(xyz​.​find(​"d"​))

In [1]:

p​=​(​"i like to play guitar and i am huge fan of sanju and he play very good guitar"​)
q​=​(p​.​find(​"p"​))
print​(p[q:])

play guitar and i am huge fan of sanju and he play very good guitar

In [6]:
p​=​(​"i like to play guitar and i am huge fan of sanju and he play very good guitar"​)
q​=​(p​.​find(​"p"​))
#q
print​(p[:q])

i like to

In [6]:

p​=​(​"i like to play guitar and i am huge fan of sanju and he play very good guitar"​)
q​=​(p​.​find(​"guitar"​))
print​(q)

15

In [7]:

p​=​(​"i like to play guitar and i am huge fan of sanju and he play very good guitar"​)
q​=​(p​.​find(​"play"​))
r​=​(p​.​find(​"play"​,q​+1​))
print​(r)

56
requesting input​¶ 

 
 
In [2]:

x​=​input​(​"enter the string"​)


x

enter the stringkggfhjfhv

Out[2]:

'kggfhjfhv'

In [9]:

x​=​int​(​input​(​" enter the number"​))


y​=​x​*​x
print​(y)

enter the number5


25
procedure​¶ 

 
 
In [4]:

def​ ​sum​(a,b):
​return​ a​+​b
print​ (​sum​(​5​,​10​))

15

String formatting​¶ 
 

In [5]:

#name = 'John'
#num = 12
print​(​'My Name is ​{1}​ and my age is ​{0}​'​.​format(​'qwe'​,​'ewr'​))
My Name is ewr and my age is qwe

In [2]:

print​(​'My Name is ​{one}​ and my age is ​{two}​'​.​format(two​=​'12'​,one​=​'John'​))

My Name is John and my age is 12

In [3]:

print​(​'My Name is ​{one}​ and my age is ​{two}​'​.​format(two​=​num,one​=​name))

My Name is John and my age is 12

String Indexing​¶ 
 

In [9]:

Name ​=​ ​'Robert'


In [10]:

Name[​0​]

Out[10]:

'R'

In [11]:

Name[​3​]

Out[11]:

'e'

Slice method​¶ 
 

In [12]:

Name[:]
Out[12]:

'Robert'

In [13]:

Name[​1​:]

Out[13]:

'obert'

In [14]:

Name[​3​:​5​]
# Grab everything from index 3 till 4 (Don't include 5th one)

Out[14]:

'er'

List​¶ 
 
 
In [15]:

[​1​,​2​,​3​]

Out[15]:

[1, 2, 3]

In [8]:

lis1 ​=​ [​1​,​2​,​3​]

In [9]:

lis2 ​=​ [​4​,​5​,​6​]

In [18]:

lis1 ​+​ lis2

Out[18]:

[1, 2, 3, 4, 5, 6]
In [19]:

lis1[​2​] ​=​ ​'Element replaced'


lis1

Out[19]:

[1, 2, 'Element replaced']

In [20]:

lis1​.​append(lis2)
lis1
# Nested lists

Out[20]:

[1, 2, 'Element replaced', [4, 5, 6]]

In [21]:

lis1[​3​][​2​]
# Accessing the inner list at position 4 and element 6 which is at position 2 of the inner list
Out[21]:

¶ 
 
 
 
Dictionary​¶ 
 
 
In [22]:

d ​=​ {​1​:​'One'​,​2​:​'Two'​}

In [23]:

d[​1​]
Out[23]:

'One'

In [24]:

d​.​update({​3​:​'Three'​})

In [25]:

Out[25]:

{1: 'One', 2: 'Two', 3: 'Three'}

In [26]:

d​.​update({​4​:[​1​,​2​,​3​,​4​,​5​]})

In [27]:

d
Out[27]:

{1: 'One', 2: 'Two', 3: 'Three', 4: [1, 2, 3, 4, 5]}

In [28]:

d[​4​][​3​]

Out[28]:

¶ 
 
 
 
Tuples and Sets​¶ 
 
 
In [1]:

tup ​=​ (​1​,​2​,​3​)

In [2]:

tup[​0​]

Out[2]:

In [31]:

tup[​0​] ​=​ ​3
# We cannot the change the items in the tuple since its immutable

---------------------------------------------------------------------------
TypeError​ Traceback (most recent call last)
<ipython-input-31-126f4e82e227>​ in ​<module>​()
----> 1​ tup​[​0​]​ ​=​ ​3
2​ ​# We cannot the change the items in the tuple since its immutable

TypeError​: 'tuple' object does not support item assignment

In [35]:
s ​=​ {​1​,​2​,​3​,​4​}
s
# Sets

Out[35]:

{1, 2, 3, 4}

In [34]:

{​1​,​2​,​3​,​4​,​4​,​5​,​5​,​6​,​9​,​3​,​3​,​3​}
# Grabs only unique elements

Out[34]:

{1, 2, 3, 4, 5, 6, 9}

In [33]:

set​([​1​,​2​,​2​,​2​,​3​,​3​,​3​,​5​,​4​,​4​])

Out[33]:

{1, 2, 3, 4, 5}
In [36]:

set​(d[​3​])
# From the value at key 3 in the above defined dictionary

Out[36]:

{'T', 'e', 'h', 'r'}

In [38]:

s​.​add(​6​)
s

Out[38]:

{1, 2, 3, 4, 6}

¶ 
 
 
 
If elif and for loop​¶ 
 
 
In [4]:

if​ ​2​ ​>​ ​3​:


​print​(​'Hello'​)
elif​ ​3​ ​==​ ​3​:
​for​ i ​in​ s:
​print​(i)
else​:
​print​(​'Error'​)

---------------------------------------------------------------------------
NameError​ Traceback (most recent call last)
<ipython-input-4-f8770effdb35>​ in ​<module>​()
2​ print​('Hello')
3​ ​elif​ ​3​ ​==​ ​3​:
----> 4​ ​for​ i ​in​ s​:
5​ print​(​i​)
6​ ​else​:

NameError​: name 's' is not defined

In [40]:

i ​=​ ​1

while​ i ​<​ ​5​:


​print​(​'i is ​{}​'​.​format(i))
i ​+=​ ​1
i is 1
i is 2
i is 3
i is 4

In [10]:

for​ x ​in​ ​range​(​10​):


​print​(x)

0
1
2
3
4
5
6
7
8
9

In [ ]:

print​(​"Enter 'x' for exit."​)


ran ​=​ ​input​(​"Upto how many line ? "​)
if​ ran ​==​ ​'x'​:
exit();
else​:
rang ​=​ ​int​(ran)
k ​=​ ​1
​for​ i ​in​ ​range​(​1​, rang​+1​):
​for​ j ​in​ ​range​(​1​, i​+1​):
​print​(k, end​=​" "​)
k ​=​ k ​+​ ​1
​print​()

¶ 
 
 
 
List Comprehension​¶ 
 
 
In [44]:

x ​=​ ​list​(​range​(​0​,​10​))
out ​=​ []

for​ num ​in​ x:


out​.​append(num)

out

Out[44]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [47]:

# The above statements can be written in the form of list comprehension

out ​=​ [num​**2​ ​for​ num ​in​ x]


out

Out[47]:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

¶ 
 
 
 
Functions​¶ 
 
 
In [49]:
def​ ​func​(p1):
​print​(p1)

func(​1​)

In [1]:

def​ ​func1​(p1,p2):
​print​(​'Area of square is ​{}​'​.​format(p1​*​p2))

func1(​2​,​4​)

Area of square is 8

In [55]:

def​ ​circleArea​(r,pi​=3.14​):
​"""
This is Docstring
Returns area of circle
input the radius and preferrd pi value
"""
​print​(​'Area is ​{}​'​.​format(pi​*​r​**2​))
In [56]:

circleArea(​7​)

Area is 153.86

¶ 
 
 
 
Map & Filter Function and Lambda expression​¶ 
 
 
In [58]:

seq ​=​ ​list​(​range​(​1​,​6​))


seq
Out[58]:

[1, 2, 3, 4, 5]

In [59]:

def​ ​square​(s):
​return​ s​**2

In [61]:

list​(​map​(square,seq))
# The map function takes as an input the function, and the parameters which can be in the form of list and applies
each parameters to the function

Out[61]:

[1, 4, 9, 16, 25]

In [64]:

# writing the square function into lambda expression

Square ​=​ ​lambda​ num : num​**2


In [65]:

list​(​map​(Square,seq))

Out[65]:

[1, 4, 9, 16, 25]

In [66]:

Square(​2​)

Out[66]:

In [69]:

#filter returns or filters according to the conditions given

list​(​filter​(​lambda​ var: var​%​2​ == 0, seq))

Out[69]:

[2, 4]
In [71]:

str​ ​=​ ​'HeLLo'


str​.​lower()

Out[71]:

'hello'

In [72]:

str​.​upper()

Out[72]:

'HELLO'

In [74]:

tweet ​=​ ​'Today I went to beach #Sunkissed #Fun'


tweet​.​split()

Out[74]:
['Today', 'I', 'went', 'to', 'beach', '#Sunkissed', '#Fun']

In [81]:

tweet​.​split(​'#'​)[​2​]

Out[81]:

'Fun'

In [89]:

d​.​keys()

Out[89]:

dict_keys([1, 2, 3, 4])

In [90]:

'x'​ ​in​ [​'x'​,​'y'​,​'z'​]

Out[90]:
True

In [91]:

ls ​=​ [(​1​,​2​),(​3​,​5​),(​7​,​8​)]

In [92]:

ls[​0​]

Out[92]:

(1, 2)

In [94]:

# tuple unpacking
for​ item ​in​ ls:
​print​(item)

(1, 2)
(3, 5)
(7, 8)
In [95]:

for​ (a,b) ​in​ ls:


​print​(a)

1
3
7

In [100]:

for​ (a,b) ​in​ ls:


​print​ (a,b)

12
35
78

You might also like