Python - Variables, Expressions & StatementsVariablesThe major purpose of variable is to store a value from one part of program so that it can be used in other part of program....
Python - Variables, Expressions & Statements
Variables
The major purpose of variable is to store a value from one part of program so that it can be used in other part of program. It can be used to store the value of computation that is carried out during the flow of the program. A variable is a name that referes to a value.
Assignment statement
Assignment statement creates a new variable and store a value in it. For example,
message = "this is first string variable" # a string type variable named message is created and value is stored in it
print(message)num = 5 # an int type variable named num is created and 5 is stored as value in int
print("num contains ----> ", num)
print("type of num -----> ", type(num))float_num = 3.5 # a variable of type float is created and value 3.5 stored in it
print("float_num contains -----> ", float_num)
float_num = 4.5 # value of variable can be changed
print("float_num now contains -----> ", float_num)Variable Naming
Choose the variable name that is meaningful and improves the readability of the program. <br>
Rules to be kept in mind while naming the variables : <br>
- Letters, numbers and underscore are allowed
- Can not contain spaces
- Can not start with number
- It's case sensitive
- It should not be from the reserved keyword list
name = "valid variable name" #valid variable name with lettersnumber1 = 12 # valid varibale name of type int_name = "valid variable" #variable name with underscore# var name = "some value" #invalid variable name as it contains space in it#1_name = "invalid variable" # throws error as it starts with numberVariable names are case sensitive. Following three variable has same spelling but still contains different values in it.
name1 = "xyz"Name1 = "por"NaMe1 = "abc"print("name1--->", name1)print("Name1--->", Name1)print("NaMe1--->", NaMe1)Variable name can not be a keyword.
#class = "my class value" # throws erros as class is reserved wordStatements
It is unit of code that Python interpreter executes. It can be a single statement or group of statement like statements within loops, functions etc. When interpreter hits statement, it executes the statement and displays the result (if there is any).
#Function call is statement
print('this is statement')#Assignment is statement but does not produce any output
x = 5#Compound statment - for loop
for i in range(5):
print("Coming inside the loop ----> " , i + 1 , " times")
print("i is ---> ", i)Expressions
Expressions in Python can include operations among compatible types (e.g., integers and floats). For example, basic arithmetic operations like adding multiple numbers:
35 + 45 + 23Python follows well accepted mathematical conventions when evaluating mathematical expressions. In the following example, Python adds 3 to the result of the multiplication (i.e., 12).
4 * 3 + 3Operators and Operands
These are special symbols that represents computations like addition, multiplication etc. Operand are values on which operators are applied.
x = 5 + 6 # + is operator applied on operands 5 & 6y = x * 9.5 # * is operator applied on operands x & 9.5# z = y + 'abcs' #throws error as operands should be compatible with the operatorstring = 'BITS' + 'Pilani' #for string + is concatenation
stringstring = "BITS Pilani " * 3 #for string * is repetation
stringMathematical Operations / Expressions
5 + 3 # addition5 - 3 # subtraction5 * 3 #multiplication5 / 3 # regular division5 // 3 # modulo division, returns reminder after division5 % 3 #integer division5 ** 3 # exponentnum1 = 2
num2 = 3
print(num1 + num2)
print(num1 - num2)
print(num1 * num2)
print(num1 / num2)
print(num1 // num2)
print(num1 % num2)
print(num1 ** num2)num1 = 8
num1 += 5
print(num1)
num1 -= 1
print(num1)Comparison Operations
num1 = 4
num2 = 12
print(num1 > num2)
print(num1 < num2)
print(num1 >= num2)
print(num1 <= num2)
print(num1 == num2)
print(num1 != num2)Exercise
Q1. Compute the simple interest using the formula interest = p n r / 100 where p is principal, n is tenure of loan, and r is rate of interest. Accept p, n, r from users and then perform the interest calculation.
#Try it hereQ2. Write a program that asks user weight in kilogram and converts into pounds. There are 2.2 pound in one kilogram.
#Try it hereQ3. If a four digit number is entered through keyboard by user. Write a program to calculate the sum of its digits.
#Try it hereAnswers
#Q1 Solution
p = eval(input("Enter the principal amount \n"))
n = eval(input("Enter the tenure of loan in years \n"))
r = eval(input("Enter the rate of interest in percentage \n"))
interest = (p * n * r) / 100
print("The interest for loan amount of Rs ", p , " for tenure ", n, " years ", "with interest rate ", r, "%\n", interest)#Q2 Solution
weight = eval(input("Enter the weight in kilogram \n"))
weight_in_pounds = weight * 2.2
print(weight, "kg = ", weight_in_pounds , " pounds ")#Q3 Solution
number = int(input("Enter a four digit number"))
sum_of_digits = 0
digit1 = number % 10
sum_of_digits = sum_of_digits + digit1
number = int(number / 10 )
digit2 = number % 10
sum_of_digits = sum_of_digits + digit2
number = int(number / 10)
digit3 = number % 10
sum_of_digits = sum_of_digits + digit3
number = int(number / 10)
digit4 = number % 10
sum_of_digits = sum_of_digits + digit4
number = int(number / 10)
print("The sum of digits of number is ", sum_of_digits)