101​

YOUR FIRST LITTLE STEPS WITH THE PYTHON

WHAT IS PYTHON

& its features 

Python is a general purpose, dynamic, high level and interpreted programming language.

Static 

Datatype decides at Compile time.

Dynamic

Datatype decides at Execution time.
int a = 5;

int a
a = 5;
a = 5;

Why python is Popular 

  • Open Source
  • Community support 
  • Web development (Django)
  • Data science (Machine learning, AI)
  • Automation 
  • GUI Programming
  • & its F**k easy 

WHERE DO, WE RUN PYTHON PROGRAMMES

  • Command prompt, Terminal
      Or IDE (PyCharm, PyDev etc) 

Let's GO

Open Cmd Or Terminal

HELLO, WORLD!

print "HELLO, WORLD!"

hello.py

Standard Data Types

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Python Lists

  • To some extent, lists are similar to arrays in C.
  • One difference between them is that all the items belonging to a list can be of different data types.
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']

#print
print list

Python Tuples

  • Read-only 

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
tinytuple = (123, 'OM')
    tuple = ( 'abcd', 786 , 2.23, 'OM', 70.2  )
list = [ 'abcd', 786 , 2.23, 'OM', 70.2  ]
tuple[2] = 1000    # Invalid syntax with tuple
list[2] = 1000     # Valid syntax with list

Python Dictionary

# empty dictionary
my_dict = {}

# dictionary with integer keys
my_dict = {1: 'apple', 2: 'ball'}

# dictionary with mixed keys
my_dict = {'name': 'John', 1: [2, 4, 3]}

# using dict()
my_dict = dict({1:'apple', 2:'ball'})

# from sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')])
  • Creating a dictionary is as simple as placing items inside curly braces {} separated by the comma.
  • An item has a key and the corresponding value expressed as a pair, key: value.

Console Input


#only for strings
username = raw_input('What is your name? ')


#all types except string
myage = input("What is your age? ")

  • Two types - input() and raw_input()

Functions 

  • Defining a function 
  • Passing arguments 
  • Returning from a function
  • Calling a function
  • Defining a function
def function_name():
    print("fuck our funtion is working")    
  • Passing Arguments
function_name(4,6)
  • Returning from a function
def fun(a, b):
    sum=a+b
    return sum

#OR

def fun(a, b):
    return a + b
  • Calling Statement 
def fun(a, b):
    return a + b

x=fun(3, 6)
#x holds value returned by function 

Classes and Objects

Creating a Class 

class class_name:
    something="lol"

Instance objects

instance = class_name()

instance.something

dir()

Try this

PYTHON 101

By omkar yadav

PYTHON 101

Python 101

  • 220