Pythons Basic Programs

1. Python Comments
            
#Pythone Single Line comments
""""
Python Multiline Comments
"""
'''
Python Multiline Comments
'''
#Python Input
name=input("Enter name")#this is input funciton
#Python Output Statement
print(name)
#by default all inputs in string format
#type casting
age=int(input("Enter Age"))
print(age)
#it returns the given variable data type
print(type(age))
            
        
2 Python Input/Output Statements
            
#Take a person data and print that info(code,name,age,course,city)
code=input("Enter Code")
name=input("Enter Name")
age=int(input("Enter Age"))
course=input("Enter Course")
city=input("Enter City")
print("Code:"+code)
print("Name:"+name)
print("Age:"+str(age))
print("Age:",age)
print("Course:"+course)
print("City:"+city)

           

            
        

Data Types

Data Types
            
#if you are assign any data to variables it takes appropriate data type
code=100
name="SrinivasaRao"
age=35
course="Python"
fees=3000.50
is_active=True
person_id=9999j
#type function return the data type
print(type(code))
print(type(name))
print(type(age))
print(type(course))
print(type(fees))
print(type(is_active))
print(type(person_id))
print(type([1,2,3,4]))
print(type((1,2,3,4)))
print(type({1,2,3,4}))
print(type({"1":1,"2":2,"3":3,"4":4}))
            
        
Dynamic Inputs
            
#dynamically inputs with type cast
code=int(input("Enter Code"))
name=input("Enter Name")
age=int(input("Enter Age"))
course=input("Enter Course")
fees=float(input("Enter Fees"))
is_active=bool(input("Enter Status (True/False"))
# person_id=9999j
person_id=complex(input("Enter Complex Value"))
#type function return the data type
print(type(code))
print(type(name))
print(type(age))
print(type(course))
print(type(fees))
print(type(is_active))
print(type(person_id))
            
        

Variables

Variables Example
            
#w.a.p to assing values and print string
print("Welcome to Python First Program") #this is python output statment
code = 100
name = 'SrinivasaRao.K'
place = "Ongole"
salary = 45500.50
# str function can change any format to string format
strtxt = "My Name is " + name + " and Place is " + place + " MY code is " +\
       str(code) + " My Salary is " + str(salary)
print(strtxt)
print("My Name is ", name, " and Place is ", place, " MY code is ", code ,
      " My Salary is ", salary)
print(type(code))
print(type(name))
print(type(place))
print(type(salary))


            
        
Variables with assign values
            
# w.a.p to assign values and print those
code = 100
name = "SrinivasaRao.K"
place = "Ongole"
salary = 45500.50
#Single Line output
print("My Name is " + name, end=" " )
print("Place is " + place, end=" ")
print("MY code is " + str(code), end=" ")
print("My Salary is " + str(salary)+"\n")
#individual Lines Output
print("My Name is " + name)
print("Place is " + place)
print("MY code is " + str(code))
print("My Salary is " + str(salary))
              
        
Dynamically Type Casting
            
#dynamical type casting
froot_name=input("Enter Froot Name")
qty=int(input("Enter Qty"))
rate=float(input("Enter Unit Price"))
total_amount=qty*rate
print("Froot Name " + froot_name + " Qty " + str(qty) +
      " Unit Price" + str(rate) + " Total Amount is "
      + str(total_amount))

#while calculation only type casting

froot_name=input("Enter Froot Name")
qty=input("Enter Qty")
rate=input("Enter Unit Price")
total_amount=int(qty)*float(rate)
print("Froot Name " + froot_name + " Qty " + qty
      + " Unit Price" + rate + " Total Amount is "
      + str(total_amount))
            
        

Operators

Arithmatic Operators
            
#Arithmatic Operators
# a=45
# b=2
a = int(input("Enter a value"))
b = int(input("Enter b value"))
print("A + B=", a+b) # Addition
print("{}+{}={}".format(a, b, a+b))
print("A - B=", a-b) # Subtraction
print("A X B=", a*b) # Multiplication
print("A/B=", a/b) # Division
print("A//B=", a//b) # Rounded to floor
print("A%B=", a%b) # Moduls
print("2 Power 8=", 2**8) # Exponentiation**

            
        
Assignment Operators
            
#Assignment Operators
#exp1=exp1+exp2 => exp1+=exp2
a=5
a+=5 #a=a+5
print(a)
a-=5 # a=a-5
print(a)
a*=5 # a=a*5
print(a)
a/=2 #a=a/2
print(a)
a//=2 #a=a//2
print(a)
a**=2 #a=a**2
print(a)
a%=2 #a=a%2
print(a)
         
        
Relational operators
            
#Relational operators
a=45
b=35
print(a==b) # false
print(a>b)  # True
print(a=b) #True
print(a<=b) #false
print(a!=b) #true

#Logical Operators
c=55
print(a>b and a>c) #false
print(a<b and a<c)  #false
print(a>b or ab != a>c) #false
            
        
Identity Operator & Membership Operators
            
#Identity Operators
a=45
b=35
print(a is b) #False
print (a is not b) #True

#Membership Operators
x=[1,2,3,4,5]
print (3 in x) #True
print(3 not in x) #False
            
        
ord() functions
            
print("grape">"apple")
print("apple">"APPLE")

#key asci value
print(ord("a"))
print(ord("b"))
print(ord("A"))
print(ord("B"))

            
        
ternary Operator
            

#ternary Operator
age=17
if age >= 18:
    message="Eligible"
else:
    message="Not Eligible"

print(message)
#or
message="Eligible" if age>=18 else "Not Eligible"

print(message)
print("Eligible" if age>=16 else "Not Eligible")
            
        
chaining operators
            
#chaining operators
age=34
if age>= 18  and age<65:
    print("Eligible")

#or

if 18<= age <65:
    print("Eligible")
            
        
bitwise operators
            

# Python program to show
# bitwise operators
a = 10
b = 4

# Print bitwise AND operation
print("a & b =", a & b)
# Print bitwise OR operation
print("a | b =", a | b)
# Print bitwise NOT operation
print("~a =", ~a)
# print bitwise XOR operation
print("a ^ b =", a ^ b)