Python Uses in Such O/S:-
--------------------------
- UNIX
- Mac O/S
- Wiindows
First Program:-
----------------
- print("Abhsihek Pathak is a IT Trainer.")
Abhsihek Pathak is a IT Trainer.
- print("Abhsihek Pathak is a IT Trainer.")
- print("Success path of data transmission between two or more nodes that is known as comunication")
Abhsihek Pathak is a IT Trainer.
Success path of data transmission between two or more nodes that is known as comunication
- print("Abhsihek Pathak is a IT Trainer.,Success path of data transmission between two or more nodes that is known as comunication")
Abhsihek Pathak is a IT Trainer.Success path of data transmission between two or more nodes that is known as comunication
- print(100)
- print(200)
100
200
- print(100)
- print(200+100)
100
300
Variables: A variable is a name given to a memory location in a program. When we type a variable in python thereafter python print the value infront of my screen.
name = "Abhishek Pathak"
age = 32
price = 100.50
weight = 32
- print(name)
- print(age)
- print(price)
- print(id(name),id(age),id(weight))
Abhishek Pathak
32
100.5
1699679173040 140725619517320 140725619517320
Note:- "name is a variable & Abhishek Pathak is a value." Always use Simple,Short & Meaningful variable by the good programer.
Rules for Identifiers:-
------------------------
- Identifiers can be combination of uppercase and lowercase letters,digits or an underscore(_). So myVariable, variable_1, variable_for_print all are valid python identifiers.
- An identifiers can not start with digit. So while variable1 is valid, 1variable is not valid.
- We can not use special symbols like !,#,@,%,$ etc in our identifier.
- Identifier can be of any length.
Data Type in Python:-
----------------------
There are two types of data type in python.
- Mutable Data Type
- Immutable Data Type
"Mutable object can change its state or contents and immutable objects cannot."
Mutable Data Type:-
-------------------
- List
- Dictionary
- byte array
----------------------
- Int (+ve,-ve,0)
- Float (100.50)
- Complex
- String ("Abhishek Pathak")
- Tuple
- set
- None (a = )
- Boolean (True or False)
Note:- "Three types of number used in python data type."
- Integer
- Float
- Complex
a = 10
print(a,"is of type",type(a))
#5 is of type <class 'int'>
b = 100.50
print(b,"is of type",type(b))
#100.50 is of type <class 'float'>
c = 1+2j
print(c,"is of type",type(c))
#5 is of type <class 'complex'>
name = "Abhishek Pathak"
age = 32
price = 100.50
weight = 32
- print(type(name))
- print(type(age))
- print(type(price))
<class 'str'>
<class 'int'>
<class 'float'>
A string is a collection of one or more characters put in aa single quote,double-quote or triple quote. Multi-line strings can be denoted using triple quotes, '''or'''
- name="Abhishek Pathak"
- print(name,type(name))
Abhishek Pathak <class 'str'>
- name = '''
- Today is Sunday
- Tomorrow is Monday
- '''
- print(name,type(name))
Today is Sunday
Tomorrow is Monday
<class 'str'>
List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. Always use in []. Mostly used data type in python. We can add multiple elements in list with comma.
a = [1,2.2,'ws']
- l=[10,'Abhsihek',100.50]
- print(l,type(l))
Note:- "Index Number"
- l=[10,'Abhsihek',100.50]
- l[2]=200
- print(l,type(l))
[10, 'Abhsihek', 200] <class 'list'>
Tuple is an ordered sequence of items same as a list. It is defined within parentheses () where items are separated by commas. It's fast comare to list. Multiple value use in tuple.
t = (5,'program',1+3j)
- l=(10,'Abhsihek',100.50)
- print(l,type(l))
(10, 'Abhsihek', 100.5) <class 'tuple'>
- l=(10,'Abhsihek',100.50)
- l[2]=200
- print(l,type(l))
l(2)=200
^^^^
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Set:- A set is an unordered collection of items. Every set elements is unique(no duplicates) and must be immutable(can not be changed). Always use curly breaket{}.
set = {10,100,200,10}
print(set,type(set))
{200, 10, 100} <class 'set'>
Keywords are the reserved words whose meaning already defined in the python interpreter.
1) We can not use a keyword as a variable name, method name or any other identifier.
2) Python keywords are the case sensitive.
Total 35 keywords
import keyword
keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']
Print Sum:-
-----------
a=100
b=200
sum=a+b
print(sum)
300
Print Diff:-
------------
a=100
b=200
diff=a-b
print(diff)
-100
Python Comments:-
Comments are nothing but ignorable part of python program that are ignored by the interpreter. It enhance the readability of codes.
There are 2 types of comments:
- Single line comments #
- Multi line comments """______""""
name = "Abhishek Pathak" #This is a string data type.
print(name)
Abhishek Pathak
Operator is a nothing but symbols that perform certain task.
- Arithmetic Operators(+,-,*,/,%,**)
- Relational/Comparison Operators(==,!=,>=,<=,<,>)
- Assignment Operators(=,+=,-=,*=,/=,%=,**=)
- Logical Operators(not,and,or)
Arithmetic Operators:-
----------------------
a=100
b=10
print(a+b) #110
print(a-b) #90
print(a*b) #1000
print(a**b) #100000000000000000000
print(a/b) #10.0
print(a//b) #10
110
90
1000
100000000000000000000
10.0
10
Relational Operators:-
----------------------
a=14
b=4
print(a<b) #False
print(a>b) #True
print(a==b) #False
print(a!=b) #True
print(a<=b) #False
print(a>=b) #True
False
True
False
True
False
True
Logical Operator:-
------------------
a=14
b=4
print(a<b and a>b) #False (If both statement is true then display
true)
print(a<b or a>b) #True (If one statement is true then display
true)
print(not(a<b and a>b)) #True (Change the statement)
False
True
True
Membership operators are used to test if a sequence is presented in an object:
in operators:- Returns True if the value or variable is found in the sequence, otherwise false.
5 in X
name = "Abhishek","Avinash","Abhinash"
print("Abhishek" in name)
# True
not in:- Returns True if the value of variable is not found in the sequence false.
6 not in Y
name = "Abhishek","Avinash","Abhinash"
print("Abhishek" not in name)
# False
Python Identity Operators:- Identity operators are used to compare the objects not if they are equal, but if they are actually the same object, with the same memory location:
is operator:- The result will be true if both variables are of the same object.
x is y
name = ["Abhishek","Avinash","Abhinash"]
name1 = ["Abhishek","Avinash","Abhinash"]
print(name is name1) # Chcek the Variable must be same.
# False
name = ["Abhishek","Avinash","Abhinash"]
name1 = ["Abhishek","Avinash","Abhinash"]
print(name == name1) # Check the Value must be same
# True
is not:- The result will be true if both the variables are not of the same object.
X is ot y
name = ["Abhishek","Avinash","Abhinash"]
name1 = ["Abhishek","Avinash","Abhinash"]
print(name is not name1) # Check the Variable must not be same
# True
Bitwise operators are used to compare_(binary) numbers:
1) If both value 1 then show the 1 otherwise always display zero.
6 = 0 1 1 0
3 = 0 0 1 1
----------------------
2 = 0 0 1 0
----------------------
Formula:- 8 4 2 1 = 15
2) If both value 0 then show the 0 otherwise always display one.
10 = 1 0 1 0
10 = 1 0 1 0
----------------------
10 = 1 0 1 0
----------------------
4 = 0 1 0 0
2 = 0 0 1 0
----------------------
6 = 0 1 1 0
----------------------
Formula:- 8 4 2 1 = 15
3) If both value different then show the 1 otherwise always display zero.
4 = 0 1 0 0
2 = 0 0 1 0
----------------------
6 = 0 1 1 0
----------------------
10 = 1 0 1 0
10 = 1 0 1 0
----------------------
0 = 0 0 0 0
----------------------
Formula:- 8 4 2 1 = 15
4) Use negative sign & 1+1=10,0+0=0,1+0=1
print(6&3)
print(10|10)
print(2^2)
print(~9)
print(10<<2)
print(10>>2)
2
10
0
-10
40
2
Input/Output:- In python for input we can use input(). For output we can use use print().
In python program we can use the input() function to take any kind of data input from the user through the keyboard.
The input() function waits for the user's input and as soon as the user enters the data and press the enter key. The input function reads that data and assigns it to the variable.
Syntax:- Var-name=input()
Input function always return string value.
name=input("Enter Value")
print(name)
#Enter Value 100
100
name=input("Enter Value")
print("This is the format of ",type(name))
#Enter Value 100
This is the format of <class 'str'>
name=int(input("Enter Value"))
print("This is the format of ",type(name))
#Enter Value 100
This is the format of <class 'int'>
print(function):-
Conditional Statement:-
-----------------------
#In Python, there are four forms of the if.....else statement.
#if statement
#if.....else statement
#if.....elif.....else statement
#nested if
if always use true
else always use false
if statement:It consists of a Boolean expression which result is either TRUE or FALSE followed by one or more statements.
-------------------------------------------------------
age=int(input("enter your age"))
if age>18:
print("You can apply for licence")
print("Speed thrills but kills")
space known as indentation.
enter your age22
You can apply for licence
Speed thrills but kills
if.....else statement:-It also contains a Boolean expression. The if ststement is followed by an optional else statement & if the expression result in FALSE, then else statement gets executed. It is also called alternative execution in which there are two possibilities of the condition determined in which any one of them will get executed.
-----------------------------------------------------------------
age=int(input("enter your age"))
if age>18:
print("You can apply for licence")
else:
print("Please you can not apply before 18.")
enter your age25
You can apply for licence
#if.....elif.....else statement:-if-elif statement is a conditional statement that checks multiple conditions to determine which code to execute. The word "elif" is short for "else if".
- The if statement checks the first condition.
- If the if statement is false, the elif statement is checked.
- If the elif statement is false, the else statement is checked.
- Once a condition is true, the interpreter executes the code for that condition.
----------------------------------
handsome='true'
good_salary='true'
if handsome=='true' and good_salary=='true':
print("You will marry with a super model")
elif handsome!='true' and good_salary=='true':
print("You will marry with a beautiful girl")
elif handsome=='true' and good_salary!='true':
print("You will marry with a girl")
else:
print("Ishwar Malik Hai Aapka Ab To")
You will marry with a super model
handsome='true'
good_salary='false'
if handsome=='true' and good_salary=='true':
print("You will marry with a super model")
elif handsome!='true' and good_salary=='true':
print("You will marry with a beautiful girl")
elif handsome=='true' and good_salary!='true':
print("You will marry with a girl")
else:
print("Ishwar Malik Hai Aapka Ab To")
You will marry with a girl
handsome='false'
good_salary='true'
if handsome=='true' and good_salary=='true':
print("You will marry with a super model")
elif handsome!='true' and good_salary=='true':
print("You will marry with a beautiful girl")
elif handsome=='true' and good_salary!='true':
print("You will marry with a girl")
else:
print("Ishwar Malik Hai Aapka Ab To")
You will marry with a beautiful girl
handsome='false'
good_salary='false'
if handsome=='true' and good_salary=='true':
print("You will marry with a super model")
elif handsome!='true' and good_salary=='true':
print("You will marry with a beautiful girl")
elif handsome=='true' and good_salary!='true':
print("You will marry with a girl")
else:
print("Ishwar Malik Hai Aapka Ab To")
Ishwar Malik Hai Aapka Ab To
Nested if statement:-We can implement if statement and or if-else statement inside another if or if-else statement. Here more than one if conditions are applied & there can be more than one if within elif.
------------------------------------------------------------
age=17
own_car='false'
# outer if statement
if (age >= 18):
# inner if statement
if (own_car == "true"):
print('Drive your car')
# inner else statement
else:
print('Work Hard & Purchase a New Car')
# outer else statement
else:
print('You must cross the of 18')
You must cross the of 18
age=21
own_car='false'
# outer if statement
if (age >= 18):
# inner if statement
if (own_car == "true"):
print('Drive your car')
# inner else statement
else:
print('Work Hard & Purchase a New Car')
# outer else statement
else:
print('You must cross the of 18')
Work Hard & Purchase a New Car
age=21
own_car='true'
# outer if statement
if (age >= 18):
# inner if statement
if (own_car == "true"):
print('Drive your car')
# inner else statement
else:
print('Work Hard & Purchase a New Car')
# outer else statement
else:
print('You must cross the of 18')
Drive your car
Object-Oriented Programming:-
-----------------------------
Python support different programing approach.
One of the popular approaches to solve a programing problem is by creating objects.
This is known as Object-Oriented Programming_(OOP)
An object has two characteristics:-
- Attributes
- Behavior
Parrot is an object
@ name,age,color are attributes
@ singing,dancing are behavior
OOP follow some basic principles
@ Inheritance
@ Encapsulation
@ Polymorphism
A class is a blueprint for the oject and logical grouping of data and functions.
Gives the freedom to create data structures.
Example:- "Any bank employ who want to fetch the customer details online would go to customer class, where all its attributes like transaction details,withdrawl and deposite details, outstanding debit etc. would be listed out"
An object is an intance of class. When class is defined, only the description for the object is defined. Therefore no memory or storage is allocated.
# Creating Class
class Student:
name = "Abhishek Pathak"
# Creating object(Instance)
S1 = Student()
print(S1)
<__main__.Student object at 0x0000019B1B816A50>
# Creating Class
class Student:
name = "Abhishek Pathak"
# Creating object(Instance)
S1 = Student()
print(S1.name)
Abhishek Pathak
class car:
color = "Red"
Brand = "Maruti"
car1 = car()
print(car1.color)
Red
class car:
color = "Red"
Brand = "Maruti"
car1 = car()
print(car1.color,car1.Brand)
Red Maruti
No comments:
Post a Comment