Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Python_S2AIDS_2nd_Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Python_S2AIDS_2nd_Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Working with Strings:

1. Concatenation
2. Appending
3. Multiplication

1. Concatenation:
Two strings can be concatenated by using + operator.

str1 = 'String '


str2 = 'Concatenation'
str1+str2 # Concatenation

'String Concatenation'

2. Appending:
A character can be appended to string using += operator or by reassignment.

str3 = 'Let'
ch = 's'
str3 += ch # str3 = str3 + ch
str3

'Lets'

3. String Multiplication:
String multiplication results a string with repitition. It can be done using * operator.

str4 = 'Namaste '


str4*4

'Namaste Namaste Namaste Namaste '

# if i want to print the python program 2 times


str=" hello world "
ss=str*2
print(ss)

hello world hello world

** Concatenate two or more strings **


To concatenate two or more string variables, you use the operator +:
greeting='Good'
time=' Afternoon'
gt=greeting+time
print(gt)

Good Afternoon

greeting='Good'
time='Afternoon'
gt=greeting+' '+time
print(gt)

Good Afternoon

Conditional Statements in Python


Decision making is the most important aspect of almost all the programming
languages.
Condition statements or Control flow statements or Desicion Control Statements are
statements that are used to control the flow or execution of a program.

The flow is controlled the values of few variables that decide the proceedings of a program.

The conditions statements have same basic structure in all programming languages. The list of
Control statements are:

1. if statement
2. if - else statements
3. if - elif - else statements
4. Nested if - else statements
5. Inline if - else statements
1. if statement:
Executes the statements inside the block only if the condition is satisfied.

Syntax:

if condition:
Statements

Note: Indentation in python indicates blocks.


Unlike many other programming languages that use braces {} to define blocks of code,

Python uses indentation (whitespace) to define the scope and grouping of code blocks

age = 18

if age >= 18:


print("You are eligible to vote.")

You are eligible to vote.

# Simple Python program to understand the if statement


num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamically
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The Given number is an even number")

enter the number: 224

The Given number is an even number

x=100
y=200
if(x<y):
print("The condition is True")

The condition is True

2. if - else statements:
Executes else when if condition fails
Syntax:
if condition1:
Statements
else:
Statements

# if..else statement example


x = 3
if x == 4:
print("Yes")
else:
print("No")

No

# python program to illustrate else if in Python statement

i = 44
if (i < 50):
print("i is smaller than 50")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
i is smaller than 50
i'm in if Block
i'm not in if and not in else Block

3. if - elif - else statements:


Checks for truth of elif statement when if statement fails. Chain of if-else

The if...else statement is used to execute a block of code among two alternatives.

Syntax:

if condition1:
Statements
elif condition2:
Statements
else:
Statements

# Define the temperature


temperature = 12

# Classify the temperature


if temperature >= 85:
print("It's hot outside.")
elif temperature >= 65:
print("The weather is warm.")
elif temperature >= 45:
print("It's a bit chilly.")
elif temperature >= 32:
print("It's cold outside.")
else:
print("It's freezing!")

It's freezing!

# Simple Python program to understand elif statement


number = int(input("Enter the number?"))
# Here, we are taking an integer number and taking input dynamically
if number==10:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The given number is equals to 10")
elif number==50:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The given number is equal to 50");
elif number==100:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The given number is equal to 100");
else:
print("The given number is not equal to 10, 50 or 100");

Enter the number? 50

The given number is equal to 50


4. Nested if - else statements:
if else within another if else statement
Syntax:

if condition1:
if condition2:
Statements
else:
Statements
else:
if condition3:
Statements
else:
Statements

if condition1:
# Code to execute if condition1 is True
if condition2:
# Code to execute if condition1 and condition2 are True
else:
# Code to execute if condition1 is True but condition2 is
False
else:
# Code to execute if condition1 is False
if condition3:
# Code to execute if condition1 is False and condition3 is
True
else:
# Code to execute if condition1 and condition3 are False
x = 10
y = 20

if x > 5:
print("x is greater than 5")
if y > 15:
print("y is greater than 15")
else:
print("y is not greater than 15")
else:
print("x is not greater than 5")
if y > 15:
print("y is greater than 15")
else:
print("y is not greater than 15")

x is greater than 5
y is greater than 15

Short Hand If ... Else OR Ternary Operators OR Conditional


Expressions.
If you have only one statement to execute, one for if, and one for else, you can put it all on the
same line:

It allows you to evaluate a condition in a single line, returning one


value if the condition is true and another if it is false.
The general syntax is:

<expression1> if <condition> else <expression2>

a=11
"even" if a%2==0 else "odd"

'odd'

a=-49
aa="positive" if a>0 else "negative"
print(aa)

negative

a = 2
b = 330
print("A") if a > b else print("B")

# Simple Python Program to print the largest of the three numbers.


a = int (input("Enter a: "));
b = int (input("Enter b: "));
c = int (input("Enter c: "));
if a>b and a>c:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print ("From the above three numbers given a is largest",a);
if b>a and b>c:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print ("From the above three numbers given b is largest",b);
if c>a and c>b:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print ("From the above three numbers given c is largest",c);

You might also like