Python Classroom Series – 23/May/2021

Python Continued

Conditional Statements

  • The if and else lines are Python statements that check whether the condition is True and then executes the block of statements
if condition:
   true condition block
else:
   false condition block
  • Lets look at simple even odd program
number = int(input('Enter the number: '))
if number%2 == 0 :
    print(f"{number} is even")
else:
    print(f"{number} is odd")
  • When we have more conditions then if elif and else can be useful
if <condition>:
    <block>
elif <condition>:
    <block>
..
..
else:
     <block>
  • Consider a scenario where you have written a python program
    • which accepts marks in subjects maths, physics, chemistry and English
    • Program will calculate the average and displays the grade of the student
      • A Grade: 90-95 %
      • B Grade: 80-90 %
      • C Grade: 60-80 %
      • D Grade: 40-60%
      • Failed < 40 %
  • Refer Here for the solution to above problem and also hacker rank problem

String

  • This is a data type which can hold text
  • In Python anything that goes b/w single or double quotes is considered as string Preview
  • Strings are example of Python sequence.
  • In python we can also use three single quotes (”’) or three double (""") to represent a string. Triple quotes are the common way to create multi line strings Preview
  • Escape sequences in python: Preview
  • Trying escape sequences Preview Preview
  • Exercise:
  • Try to write a statement in python which prints
The world's largest rubber elephant was 74'2" by 45'7" by 110

Preview

  • The input function which we are using will return string which is represented as type str

  • String Concatenation Preview

  • String Interpolation:

    • Comma Separators:
      • Variables may be interpolated into string using commas to separate clauses Preview
    • Format: With format Python types ints, floats and so on can be converted into string upon execution Preview Preview Preview
    • f-strings: Preview

String Methods

  • Create a new variable called as name and assign it to any name which you want
name = 'Guido'
  • Refer Here for the sample methods used

  • Getting Character with []: Preview

  • Python also supports negative index Preview

  • Exercise: Solve the following hacker rank problem Refer Here

Leave a Comment

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

About learningthoughtsadmin