Professional Documents
Culture Documents
-----------------------------------------------------------------------------------
--
ways by which we can run python programs
-----------------------------------------------------------------------------------
-
1. install python in your machine
use notepad to write code
run on command prompt
-----------------------------------------------------------------------------------
---------
rules of programming in python
------------------------------------------------------------
1. we dont have to declare datatypes in python
2. 4 ways of giving comments in python
----------------------------------------------
__author__='Abhaya Agrawal'
# line comment
# this is my first program
'''
this is
a block
comment
'''
"""
this is again
a block comment
in multiple
lines
"""
a=10
b=20
c=a+b
print("sum of a and b is : ", c)
-------------------------------------------------------------
3. indentation rules
whenever we start any block - if, else, for, while, def, class, try, except,
else
you need to give one tab to signify the starting of the block
all of your code must start from 0th column.
if you want to start a block give if : or while: or else: and give a tab in next
lineto
start the block
a=10
b=20
c=30
if a<b:
print("a is less than b")
print("success")
if b<c:
print("b is less than c")
print("again success")
else:
print("b is greater or equal to c")
print("failure again")
else:
print("a is greater or equal to b")
print("failure")
print("bye") #this line is not a part of any block
-----------------------------------------------------------------------------------
---
Question
Question
---------------------------------------
if cp=5 and sp=10 find if its a profit or a loss or no profit no loss
if condition
elif condition
elif condition
else
-------------------------------------------------------------------
cp=5
sp=12
if cp<sp:
print("Profit", sp-cp)
elif cp>sp:
print("Loss",cp-sp )
else:
print("No Profit, No Loss")
-----------------------------------------------------------------------
and - for and all the conditions has to be true
or - if any single condition is true, ans is true
10 20 6
find the greatest number among them without using max function
a=10
b=20
c=6
if a>b and a>c:
print("a is greatest")
elif b>a and b>c:
print("b is greatest")
elif c>a and c>b:
print("c is greatest")
else:
print("Some Number is same")
a=10
b=20
c=30
if a<b:
print("a is less than b")
print("success")
if b<c:
print("b is less than c")
print("again success")
else:
print("b is greater or equal to c")
print("failure again")
else:
print("a is greater or equal to b")
print("failure")
print("bye") #this line is not a part of any block
------------------------------------------------------------------
in ver 2.x of python there two ways of taking input from the user
if age>18:
print(name, " you are an adult as your age is ", age)
else:
print(name, " you are not yet adult as your age is ", age)
------------------------------------------------------------------------
Question
take any number from the user and print its table
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
..
..
...
3 X 10 = 30
count=1
while count<=10:
print(count)
count+=1
print("bye")