Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Samenvatting

Summary - CS6PET01 Python and Latex

Beoordeling
-
Verkocht
-
Pagina's
40
Geüpload op
05-06-2024
Geschreven in
2023/2024

Provides with easy to understand notes to get your started with your python learning, also helpful for B.Sc CS 3rd year students

Instelling
Vak

Voorbeeld van de inhoud

B Sc CS CS6PET01: Python and LateX Page |1

B Sc Computer Science – VI Semester

Elective Papers - CS6PET01: Python and LateX
Module II - Control Flow and Data Structures
Logical operators, if, If-Else, While loop, For loop, List value, length, operation and
deletion, Dictionary operation & methods, Tuples



Flow of Control
The order of execution of the statements in a program is known as flow of control. The flow of
control can be implemented using control structures. Python supports two types of control
structures—selection and repetition.

Program to print the difference of two numbers.
#Program to print the difference of two input numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
diff = num1 - num2
print("The difference of",num1,"and",num2,"is",diff)
Output:
Enter first number 5
Enter second number 7
The difference of 5 and 7 is -2

1. SELECTION

A selection is a decision involves selecting from one of the two or more possible options. In
programming, this concept of decision making or selection is implemented with the help of if
..else statement.

1. Simple if statement

The syntax of if statement is:

if condition:
statement(s)



In the following example, if the age entered by the user is greater than 18, then print that the
user is eligible to vote. If the condition is true, then the indented statement(s) are executed.


VIJAYA GOPINATH M

,B Sc CS CS6PET01: Python and LateX Page |2

The indentation implies that its execution is dependent on the condition. There is no limit on
the number of statements that can appear as a block under the if statement.

Example 6.1
age = int(input("Enter your age "))
if age >= 18:
print("Eligible to vote")



2. if .. else Statement

A variant of if statement called if..else statement allows us to write two alternative paths and
the control condition determines which path gets executed. The syntax for if..else statement is
as follows.

if condition:
statement(s)
else:
statement(s)


Let us now modify the example on voting with the condition that if the age entered by the user
is greater than 18, then to display that the user is eligible to vote. Otherwise display that the
user is not eligible to vote.

age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")


3. if .. elif .. else Statement

Many a times there are situations that require multiple conditions to be checked and it may
lead to many alternatives. In such cases we can chain the conditions using if..elif (elif means
else..if).

The syntax for a selection structure using elif is as shown below.
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)



VIJAYA GOPINATH M

,B Sc CS CS6PET01: Python and LateX Page |3


Number of elif is dependent on the number of conditions to be checked. If the first condition is
false, then the next condition is checked, and so on. If one of the conditions is true, then the
corresponding indented block executes, and the if statement terminates.

Example Check whether a number is positive, negative, or zero.

number = int(input("Enter a number: ")
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")

Example Display the appropriate message as per the colour of signal at the road crossing.

signal = input("Enter the colour: ")
if signal == "red" or signal == "RED":
print("STOP")
elif signal == "orange" or signal == "ORANGE":
print("Be Slow")
elif signal == "green" or signal == "GREEN":
print("Go!")
Program 6-3 Write a program to create a simple calculator performing only four basic
operations.
#Program to create a four function calculator
result = 0
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed.
Program terminated")
else:
result = val1/val2
else:
print("Wrong input,program terminated")



VIJAYA GOPINATH M

, B Sc CS CS6PET01: Python and LateX Page |4

print("The result is ",result)




Output:
Enter value 1: 84
Enter value 2: 4
Enter any one of the operator (+,-,*,/): /
The result is 21.0




4. Nested if Statements
Nested if .. statements is one if… statement inside another if… statement.

In the program, for the operators "-" and "/", there exists an if..else condition within the elif
block. This is called nested if. We can have many levels of nesting inside if..else statements.

2. INDENTATION

In most programming languages, the statements within a block are put inside curly brackets.
However, Python uses indentation for block as well as for nested block structures. Leading
whitespace (spaces and tabs) at the beginning of a statement is called indentation. In Python,
the same level of indentation associate statements into a single block of code. The interpreter
checks indentation levels very strictly and throws up syntax errors if indentation is not correct.
It is a common practice to use a single tab for each level of indentation.

Program : Program to find the larger of the two pre-specified numbers.



VIJAYA GOPINATH M

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
5 juni 2024
Aantal pagina's
40
Geschreven in
2023/2024
Type
SAMENVATTING

Onderwerpen

€5,31
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
peeku

Maak kennis met de verkoper

Seller avatar
peeku MGU
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
1 jaar
Aantal volgers
0
Documenten
3
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen