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

Python Basics

Uploaded by

oncome80
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Basics

Uploaded by

oncome80
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Variables

V=V ( Variable = Value )

Rules:

1) It cannot start with numbers


Example: Variable 1, Variable54, Variable_47,
1variable,
2) Uppercase, Lowercase, Digits and Underscore ( _ ) can be used but not any special characters
are allowed.
3) It can be of any length.

Datatypes

1) Integer
2) Float
3) String
4) Boolean
5) None

Operators

Arithmetic operators ( + , - , * , / , // (Whole division - Q) , %(Modulus - R) , ** )

Relational / Comparison ( == , != , > , < , >= , <= )

Logical Operators ( And, Or and Not )

Assignment operators ( = , += , -= , *= , /= , %= , **= )

A = 10 A = 10
A = A + 10 A += 10
Print(A) Print (A)
= 20 = 20

Type Conversion vs Type Casting


Dictionary
 Key : Value pairs
 It is Ordered, Mutable and no Duplicate keys
d = {
#"Key" : "Value",
"7" : "Dhoni" , #Integer as a Key
"20.36" : "Prashanth" , #Float as a Key
"India" : "Tamil Nadu" , #String as a Key
"(1,2,3)" : "Numbers" , #Tuple as a Key
"[4,5,6]" : "Numbers" , #Lists as a Key
"True" : "Success" #Boolean as a Key
}
print(d)

Methods

CURD

 d = {}
Create
 d=dict()
 d[“Key”] = “Value”
Update / Modify
 update()
 d[“Key”] / get()
Read / Access
 keys() , values() , items()
 pop() , popitem()
Delete / Remove  del – keyword ( a specific key or entire
dictionary )
 in – keyword
Check and Copy
 copy() , dict()

Sets
Methods

CURD

Create  s=sets()
 add() – Single value
 update() – Multiple values
Update / Modify
 union()
 intersection()
Read / Access
 pop()
Delete / Remove
 clear()
Control Flow

if ( condition ):
if ( condition ):
statements
statements
if ( condition ): elif ( condition ):
else:
statements statements
statements
else:
statement

Short Hand if

if ( condition ) : statement 1

Short Hand if else – Ternary Operators / Conditional Expressions

Statement if ( condition ) else statement

Functions – METHODS
1)Function Arguments
1. Default
2. Positional
3. Keyword
4. Variable-length
i. Arbitrary Positional
ii. Arbitrary Keyword

Default Argument

Provide a default value for a parameter in case if no value is passed for that parameter in the
Function call. When no value is specified for the parameter in the function call.

def area (length, width=1):


return length*width
print ( area (5) ) #5
print ( area (5,10) ) # 50

Default arguments must always follow Positional arguments.

Positional Argument

They are passed to a function by matching their position in the function call with the position
of the parameters in the function definition.
Order – Is important

Keyword Arguments

Keyword arguments allow you to pass arguments to a function by explicitly specifying the
name of the parameter and its value. This makes it easier to understand what each argument
does, especially when you have a lot of arguments with complex names or multiple default
values.
Order – Doesn’t matter

Variable-Length Argument

Sometimes we may not know the number of arguments that will be passed to a function. In
such cases, we can use variable-length arguments.
 Arbitrary Positional *args
 Arbitrary Keyword **kwargs

2) Lambda Function
Lambda functions are small, one-time use function that can be used as arguments for other
functions.
Syntax :

lambda Arguments : Expression

It can take any number of arguments, but


can only have one expression, which is evaluated and returned.
OOPS - Object Oriented Programming

Inheritance

Class
Encapsulation Polymorphism
Object

Abstraction

 OOPS – Focuses on creating objects and making the code reusable.


 Class – It is a Blueprint to create object. It has Attributes and Methods. Both coexist with
each other.
Attributes – Properties , Data
Methods – Behaviour , Function

Employee
Attributes Methods
Name , Age , E-mail , Blood group . To code , Give presentation , Attend
meetings , Arrange special events.

 In python, every class inherits from a built-in basic class called “Object”. The constructor
(__init__) is invoked when we create an object variable or an instance of the class.
 Default Constructor and Parametrized Constructor
 Self-Parameter : It will point / refer to the current instance of the class. It can access the
attributes that belongs to the class. ( Object ka Reference hae )

Public vs Private vs Protected Members


Public vs Private vs Protected Attributes
Public vs Private vs Protected Methods

Private Protected
 Attributes can be accessed only  Can be accessed even outside the
within the Class and should not be Class , yet Python will try to warn
accessed from outside the Class. you that it’s not a good idea to
 If we need to access it, if there is access them outside of the Class
absolute necessary to access
private attribute then a method,
that would return the value of
private attribute, is created within
the class.

Decorators:

Methods:

Instance method – Class method – Static method

 Takes class as the first parameter  Takes no specific parameter


 Can access or modify the class state  Knows nothing about the class state
Utility – Type : It takes some parameters
and work upon those parameters

Encapsulation
Access Modifiers

 Public , Private and Protected [ Methods and Attributes / Member ]


 Python doesn’t have any mechanism restricting access to the variables or methods.

Private Protected
 Attributes can be accessed only within Can be accessed even outside the Class , yet
the Class and should not be accessed Python will try to warn you that it’s not a good
from outside the Class. idea to access them outside of the Class

 If we need to access it, if there is


absolute necessary to access private
attribute then a method, that would
return the value of private attribute, is
created within the class.

You might also like