Jul 2, 2018

Python Operators

#!/usr/local/bin/python2.7

'''
    #Learning variables in Python
    File name: python_operators.py
    Author: Prabhath Kota
    Date: June 29, 2018
    Python Version: 2.7
'''

'''
Python Arithmetic Operators: +, -, *, /, % (modulus), ** (exponent), // (Floor Division)
Python Comparison Operators: ==, !=, <> (not equals), >, <, >=, <=
Python Assignment Operators: =, +=, -=, *=, /=, %=, //=
Logical: and, or, not
Python Identity Operators: Identity operators compare the memory locations of two objects  
Python Operators Precedence: 
Exponentiation (raise to the power)
Multiply, divide, modulo and floor division
Addition and subtraction
Comparison operators
Equality operators
'''

a = 100
b = 200
c = 10
print '-----------------Python Arithmetic Operators-----------------'
print a+b #300
print a-b #-100
print a*b #20000
print b/a #2
print b%a #0
print b**a #....
print '-----------------Python Comparision Operators-----------------'
print a == b #False
print a>b #False
print a != b #True
print a<>b #True #Not equals to
print a>b #False
print a < b #True
print '-----------------Python Assignment Operators-----------------'
b += 100
print b
b *= 10
print b
b  = 200
b %= 10
print b
print '-----------------Python Membership Operators-----------------'
print 10 in [100, 200, 10, 300]
print 111 not in [100, 200, 10, 300]

print '-----------------Python Operators Precedence-----------------'
a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d       #( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ",  e

e = ((a + b) * c) / d     # (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ",  e

e = (a + b) * (c / d);    # (30) * (15/5)
print "Value of (a + b) * (c / d) is ",  e

e = a + (b * c) / d;      #  20 + (150/5)
print "Value of a + (b * c) / d is ",  e


Learning control flow statements in Python

#!/usr/local/bin/python2.7

'''
    #Learning control flow statements in Python
    File name: python_control_flow_statements.py
    Author: Prabhath Kota
    Date: June 29, 2018
    Python Version: 2.7
'''

'''
Sequential
Selection (Python Conditional Statements) 
if
if...else
if..elif..else statements
nested if statements
not operator in if statement
and operator in if statement
in operator in if statement
Repetetion
for loop
while loop
'''

print('-------------------Python if statements----------------')
x=20
y=10
if x > y :
  print("X is bigger")
#X is bigger

print('-------------------Python if...else statements----------------')
x=10
y=20
if x > y :
  print("X is bigger")
else :
  print("Y is bigger")
#Y is bigger

print('-------------------Python if...else...else statements----------------') 
x=500
if x > 500 :
  print("X is greater than 500")
elif x < 500 :
  print("X is less than 500")
elif x == 500 :
  print("X is 500")
else :
  print("X is not a number")
#X is 500

print('-------------------Python Nested if statements----------------') 
mark = 72
if mark > 50:
  if mark >= 80:
    print ("You got A Grade !!")
  elif mark >= 60 and mark < 80 :
    print ("You got B Grade !!")
  else:
    print ("You got C Grade !!")
else:
  print("You failed!!")
#You got B Grade !!

print('-------------------not operator in if statement----------------') 
mark = 100
if mark != 100: #mark != 100
  print("mark is not 100")
else:
  print("mark is 100")
#mark is 100

print('-------------------and operator in if statement----------------') 
mark = 72
if mark > 80:
  print ("You got A Grade !!")
elif mark >= 60 and mark < 80 :
  print ("You got B Grade !!")
elif mark >= 50 and mark < 60 :
  print ("You got C Grade !!")
else:
  print("You failed!!")
#You got B Grade !!

print('-------------------in operator in if statement----------------') 
color = ['Red','Blue','Green']
selColor = "Red"
if selColor in color:
  print("Red is in the list")
else:
  print("Not in the list")
#Red is in the list4

print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each

#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
#Lala Lajipati Rai
#Dadabhai Naoroji
#Rajendra Prasad
#Sarojini Naidu
#Dadabhai Naoroji
#Lal Bahadur Shastri

print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares
#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Jul 1, 2018

Learning variables in Python

#!/usr/local/bin/python2.7

'''
    #Learning variables in Python
    File name: python_variables.py
    Author: Prabhath Kota
    Date: June 29, 2018
    Python Version: 2.7
'''

'''
Unlike other programming languages, Python there is no need to declare a variable.
A variable is created when you first assign a value to it.
A variable can have a short or uder understandbale name

Rules for Python variables:
A variable name cannot start with a number
Variable names are case-sensitive (height, Height and HEIGHT are three different)
A variable name must start with a letter or the underscore character
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
'''

var_a = 100
print var_a
#100

var_a = 'Hi'
print var_a
#Hi

#1_var = 'Hi'
#wrong declaration

#Case-Sensitive
height = 5.0
print height

Height = 5.2
print Height

HEIGHT = 5.6
print HEIGHT