## 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
• 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 • 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 • Escape sequences in python: • Trying escape sequences  • Exercise:
• Try to write a statement in python which prints
``````The world's largest rubber elephant was 74'2" by 45'7" by 110
`````` • The input function which we are using will return string which is represented as type `str`

• String Concatenation • String Interpolation:

• Comma Separators:
• Variables may be interpolated into string using commas to separate clauses • Format: With format Python types ints, floats and so on can be converted into string upon execution   • f-strings: ### 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 []: • Python also supports negative index • Exercise: Solve the following hacker rank problem Refer Here

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