Python Essentials Classroom Series – 03/Sept/2021

Floats

  • Floating point numbers (decimal numbers) are floats in python Preview
  • we can use all the operators which we have used for integer
  • To convert other types to float we have float() function Preview

input method in python

  • Input method in python is used to accept input from the command line or terminal
  • input function always returns a type string.
  • if we need to use it as int or float we need to use int() or float() methods Preview

Comments in python

  • When we write python anything after # is a comment and is used for readability purposes and will not be executed by interpreter

Conditional Statments if, elif and else

  • The syntax of the conditional statement if
if <conditional expression>:
    <if-code-block-1>
    <if-code-block-2>
    ...
    <if-code-block-n>

  • The conditional expression should return a boolean i.e True or False. The code block will be executed in the case of true
flag = False
if flag:
    print('This code is executing as flag is True')
    print('line 1')
    print('line 2')
    print('line 3')
print('This code is outside of if statement')
  • Exercise:
    • Create a learningif.py file.
    • ask the input from the user and store it is integer
    • if the value is greater than 5 print good
x = int(input('Enter the number: '))

if x > 5:
    print('good')
  • We have one more flavor if else
if <expression>:
   <code block>
else:
   <code block>
  • we have one more flavor if elif else
if <condition>:
    <block>
elif <condition>:
    <block>
...
else:
    <block>
  • Lets write a program which accepts the aggregate marks and prints the grade
average = int(input('Enter the average marks: '))

if 92 <= average <= 100 :
    print('A1')
elif 83 <= average <= 91:
    print("A2")
elif 75 <= average <= 82:
    print("B1")
elif 67 <= average <= 74:
    print("B2")
else:
    print("C")

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin