Python 101

By Shashank

blog.shanky.xyz | realslimshanky

Author - Guido van Rossum

Who Am I

def user ( ):

    name = Shashank Kumar

    about = shanky.xyz

    find me anywhere​ = @realslimshanky          

    this slide = slides.com/realslimshanky/python101

What Is ?

A dynamic, interpreted, high-level language 

Does not require declaring the types of variables or parameters or methods

Extremely readable

Used for solving a really wide range of problems

Who's using it ?

Why ?

A thriving community of developers and contributors and third party application developers

PyPI (Python Package Index) lists 99381 third-party software projects

Implementation in every field of technology

First Program

The customary "Hello World"

The Main Elements

Identifiers

Keywords

Assignments

Indentation & Comment

Input/Output

The Main Elements

identifier ::= (letter|"_") (letter | digit | "_")*

letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"

The Main Elements

Keywords

False None True and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield

The Main Elements

Indentation & Comment

# Single Line Comment

'''

Multiple

Lines

Comment

'''

The Main Elements

Assignments

 >>> a = 12
 >>> b = 23
 >>> a + b
 >>> 35

The Main Elements

Input/Output

input()
print()

Handling Data

Variables & Datatypes

Operator & Expression

Condition & Looping

Function

Variables & Datatypes

int

float

string

boolean

int(string)

float(string)

str(integer)

str(float)

type(variable/value)

Operator & Expression

Relational Operators
<
<=
>
>=
==
!=

Logical Operators

and, &

or, |

not

Shorthand Operators

+=

-=

*=

/=

Condition & Looping

if condition:

     statement1

if condition:            statement2

elif:

     statement3

else:

     statement4

for x in range(): statement1 statement2

while condition: statement

(continue, break)

Function

def function(parameter):

     statement

     return

Local and Global Scope
Default Argument
Keyword Argument

Strings

Single Line Declaration

s = 'I am a string'

s ='I am a \

string'

Multiple Line Declaration

s = 'I am a \n string'

s = ''' I

am

a string '''

String Validation

islower() isupper() isalnum() isdigit()

 isalpha() istitle()

String Function

String Manipulation

title() lower()

upper() swapcase()

String Strip

strip() lstrip()

 rstrip()

String Strip

find() startswith()

 endswith()

String Length

len()

Data Structure

Lists

Tuples

Sets

Dictionary

How above are copied?

List

Declaration

L = [1, 2.3, True, 'a', 'bcd']

Getting an element

L[index]

List elements are ordered

List Methods and Some Functions

a.append(2)

a.insert(0,23)

a.count(2)

a.append(list)

a.extend(list)

 

 

del list[position]

del list

list[-position]

len[pos1:pos2]

len(list)

enumerate(list)

a.sort()
a.reverse()

a.remove(2)

a.index(3)

zip(list1,list2)

 

Tuples

Declaration

a = ('hello',1,4,3)

Conversion

list(tuple)

tuple(list)

Tuple elements are odered

Sets

Declaration

a = {'a','b','c'}

b = set('ababcbccbabc')

Set theory

a-b a+b a|b a^b a&b

Set elements are unordered

Dictionary

Declaration

a = {key : value}

Adding New Key,Value

a[key]=value

a.setdefault(key,default)

Getting A Value

a[key]

a.get(key,fallback) 

Some helpful Functions

a.keys() a.value() a.items() enumerate()

Dictionary elements are unordered

How above are copied ?

Mutable -> Reference

Immutable -> Value

import copy

copy.copy()

copy.deepcopy()

What are Mutable and Immutable objects ?

File Handling

Opening A File f=open('filename')

Option Arguments

mode

encoding

Closing A File f.close()

Alternative with open('filename') as fobj:

File Handling

Reading From A File f=open('filename')

f.tell()

f.read()

f.seek()

Writing To A File f=open('filename', 'a')

f.write('Hello')

Error Handling

try:

        print(2/0)

except:

       print('Error Encountered')

finally:

      print('No matter what happens, I will run')

Classes

Defining A Class

class cname():

        a=10

        def amethod(self):

                print(self.a)

Classes

Creating An Instance Of Class

myinstance = cname()

Creating A Class Constructor

def __init__(self, b):

self.a=b

Classes

Getting and Setting the properties

myinstance = cname()

print(myinstance.a)

myinstance.a = 12

Classes

Inheritance

class a():

    age=20

class b(a):

    def show(self):

        print(self.age)

Method

myinstance = b()

myinstance.show()

Thank You

For any query

abc@shanky.xyz | @realslimshanky

Python 101

By Shashank Kumar

Python 101

  • 1,806