PROBLEM SOLVING USING PYTHON Unit 1 MADRAS UNIVERSITY SYLLABUS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 117
At a glance
Powered by AI
The key takeaways are that computer science involves computational problem solving using algorithms and representations. Two things are needed to solve a problem computationally - a representation to capture relevant aspects and an algorithm to solve the problem using the representation.

The two things needed to solve a problem computationally are representation, which captures all relevant aspects of a problem, and algorithm, which solves the problem using the representation.

Abstraction is presenting only relevant aspects and omitting irrelevant aspects in representation. An example is the man, cabbage, goat and wolf problem.

Problem Solving Using Python

R.Dharmeshwari
Asst. Prof. Dept. of BCA
ANNAI THERASA ARTS AND SCIENCE COLLEGE,
THIRUKAZHUKUNDRAM, MANGALAM.

Problem Solving Using Python Unit 1


Introduction
• What Is Computer Science?
– It is about computational problem solving.
– Computation is given by the notion of algorithm.

Problem Solving Using Python Unit 1


The essence of computational problem
solving
• Two things are needed to solve a problem
computationally.
– Representation : captures all relevant aspects of a
problem.
– Algorithm : solves the problem using the
representation.
• Brute Force Attack : Trying all possible
solutions to the given problem.

Problem Solving Using Python Unit 1


• Abstraction : Presenting only relevant aspects
and the irrelevant aspects are omitted in
representaion.
• Example :
• Man, cabbage, goat and wolf problem.

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
Problem
A man lives on the east side of a river. He wishes to
bring a cabbage, a goat, and a wolf to a village on the
west side of the river to sell.
However, his boat is only big enough to hold himself,
and either the cabbage, goat, or wolf.
In addition, the man cannot leave the goat alone with
the cabbage because the goat will eat the cabbage,
and he cannot leave the wolf alone with the goat
because the wolf will eat the goat. How does the
man solve his problem?
Problem Solving Using Python Unit 1
Solution
• The wolf does not eat cabbage, so the crossing can
start with the goat. 
• The man leaves the goat and returns, puts the
cabbage in the boat and takes it across. On the other
bank, he leaves the cabbage but takes the goat. 
• He leaves the goat on the first bank and takes the
wolf across. He leaves the cabbage with the wolf
and rows back alone. 
• He takes the goat across.  

Problem Solving Using Python Unit 1


Limitations of computational problem solving

• Once an algorithm for solving a given problem


is developed or found, an important question
is, “Can a solution to the problem be found in
a reasonable amount of time?”
• If not, then the particular algorithm is of
limited practical use.
• Eg: Travelling salesman problem
– Game of chess

Problem Solving Using Python Unit 1


The Traveling Salesman problem

Problem Solving Using Python Unit 1


• Any algorithm that correctly solves a given
problem must solve the problem in a
reasonable
• amount of time, otherwise it is of limited
practical use.

Problem Solving Using Python Unit 1


Computer Algorithms
• Definition :
• An algorithm is a fi nite number of clearly
described, unambiguous “doable” steps that
can be systematically followed to produce a
desired result for given input in a finite
amount of time.
• Eg: Checking the number is prime or not.

Problem Solving Using Python Unit 1


History of Algorithm
• 9th century
• Arab mathematician, Al-Khwarizmi who
worked on “written processes to achieve some
goal.
• Computer algorithms are central to computer
science.
• They provide step-by-step methods of
computation that a machine can carry out.

Problem Solving Using Python Unit 1


Algo contd…
• Having high-speed machines (computers) that
can consistently follow and execute a given set
of instructions provides a reliable and effective
means of realizing computation.
• since the study of algorithms does not depend
on the existence of computers.
• computers can execute instructions very quickly
and reliably without error , algorithms and
computers are a perfect match
Problem Solving Using Python Unit 1
Algorithm for finding even number or odd
number
1. Enter the number n
2. If the given number n is divided by 2 it is even
number.
3. Otherwise it is a odd number.

Problem Solving Using Python Unit 1


COMPUTER HARDWARE
• Computer hardware comprises the physical
part of a computer system.
• Eg: CPU, Main Memory,
• Peripheral components such as keyboard,
mouse, monitor and printer.

Problem Solving Using Python Unit 1


Digital Computing
• It’s All about Switches.
• It is essential that computer hardware be
reliable and error free.
• Rare occurrence of a hardware error was
discovered in 1994.
• The key to developing reliable systems is to
keep the design as simple as possible.

Problem Solving Using Python Unit 1


• In digital computing, all information is
represented as a series of digits.
• Decimal numbers : 0-9 having base 10.
• Represents there are 10 numbers.

Problem Solving Using Python Unit 1


• In current electronic computing, each digit is
represented by a different voltage level.
• The more voltage levels (digits) that the
hardware must utilize and distinguish, the
more complex the hardware design becomes.
• All information within a computer system is
represented by the use of only two digits, 0
and 1 , called binary representation .

Problem Solving Using Python Unit 1


• In this representation, each digit can be one of
only two possible values, similar to a light switch
that can be either on or off.
• Transistors : switch at very high speed.
• Integrated circuits : the building blocks of
computer hardware, are comprised of millions
or even billions of transistors

Problem Solving Using Python Unit 1


The Binary Number System
• For representing numbers, any base (radix)
can be used.

Problem Solving Using Python Unit 1


Decimal to binary conversion
• 99 :divide the number by 2.
• 99
• 49-1
• 24- 1
• 12-0
• 1100011
• 6-0
• 3-0
• 1-1
Problem Solving Using Python Unit 1
Binary to Decimal conversion

Problem Solving Using Python Unit 1


• The term bit stands for binary digit.
• A byte is a group of bits operated on as a
single unit in a computer system, usually
consisting of eight bits.

Problem Solving Using Python Unit 1


Fundamental components of Computer
hardware

Problem Solving Using Python Unit 1


Operating System
• An operating system is software
that has the job of managing and
interacting with the hardware
resources of a computer.
• An operating system acts as the
“middle man” between the
hardware and executing
application programs

Problem Solving Using Python Unit 1


• An operating system is software that has the
job of managing the hardware resources of a
given computer and providing a particular
user interface.

Problem Solving Using Python Unit 1


Limits of Integrated Circuits Technology:
Moore’s Law
• Moore’s Law states that the number of
transistors that can be placed on a single
silicon chip doubles roughly every two years.

• In 1965, Gordon E. Moore (Figure 1-16), one


of the pioneers in the development of
integrated circuits and cofounder of Intel
Corporation predicted this law.

Problem Solving Using Python Unit 1


COMPUTER SOFTWARE
• The first computer
programs ever written
were for a mechanical
computer designed by
Charles Babbage in the
mid-1800s.
• First computer
programmer - 

ADA LOVELACE
Problem Solving Using Python Unit 1
Definition
• Computer software is a set of program
instructions, including related data and
documentation, that can be executed by
computer.
• Types
– System Software
– Application software

Problem Solving Using Python Unit 1


Syntax, Semantics, and Program Translation

• Programming languages are languages just as


“natural languages”
• Syntax and semantics are important concepts that
apply to all languages.
• The syntax of a language is a set of characters and
the acceptable sequences of those characters.
• The semantics of a language is the meaning
associated with each syntactically correct
sequence of characters.

Problem Solving Using Python Unit 1


Program Translation
• A central processing unit (CPU) is designed to
interpret and execute a specific set of
instructions represented in binary form (i.e., 1s
and 0s) called machine code .
• A compiler is a translator program that
translates programs directly into machine code
to be executed by the CPU.
• An interpreter executes program instructions
in place of (“running on top of”) the CPU.
Problem Solving Using Python Unit 1
Problem Solving Using Python Unit 1
Program Debugging:
Syntax Errors vs. Semantic Errors
• Program debugging is the process of fi nding
and correcting errors ( “bugs” ) in a computer
program.
• Syntax errors are caused by invalid syntax.
• semantic errors (generally called logic errors )
are errors in program logic
• Computers do not understand what a program is
meant to do, they only follow the instructions
given .
Problem Solving Using Python Unit 1
Procedural vs. Object-Oriented
Programming
• Programming languages fall into a number of
programming paradigms .
– Procedural
– Object Oriented

• Python supports both procedural and object-


oriented programming.
Problem Solving Using Python Unit 1
The Process of Computational Problem
Solving
• Involve the act of computer programming
• Before a program is written, a design for the
program must be developed.
• before a design can be developed, the
problem to be solved must be well
understood.
• Once written, the program must be
thoroughly tested

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
1.Problem Analysis
• Understanding the Problem
– the fundamental computational issues for solving
it can be determined.
– the representation is straightforward
– Eg.Calendar month problem
– determining the first day of a given month, and
displaying the calendar month in the proper
format

Problem Solving Using Python Unit 1


• The first day of the month can be obtained by
direct calculation by use of the algorithm

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
• Knowing What Constitutes a Solution
• program may be stated as finding
– A solution
– An approximate solution
– A best solution
– All solutions
• A best solution here is one with the shortest
number of steps.

Problem Solving Using Python Unit 1


2. Program Design
• Describing the Data Needed
• For the Calendar Month problem, the data include the
month and year (entered by the user), the number of days
in each month, and the names of the days of the week.
• A useful structuring of the data is given below
– [ month , year ]
– [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
– [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’,
‘Friday’, ‘Saturday’]
– 0 – Sunday, 1 – Monday, . . ., 6 – Saturday

Problem Solving Using Python Unit 1


• Describing the Needed Algorithms
• For the MCGW problem, there are standard
search algorithms that can be used.
• Algorithms that work well in general but are
not guaranteed to give the correct result for
each specific problem are called heuristic
algorithms .

Problem Solving Using Python Unit 1


3. Program Implementation
• Design decisions provide general details of the
data representation and the algorithmic
approaches for solving a problem.
• The implementation needs to be expressed in
a syntactically correct and appropriate way,
using the instructions and features available in
Python.

Problem Solving Using Python Unit 1


4. Program Testing
• As humans error is common.

Problem Solving Using Python Unit 1


The Python Programming Language

About Python
• Guido van Rossum is the creator of
the Python programming language
• first released in the early 1990s
• Its name comes from a 1970s
British comedy sketch television
show called Monty Python’s Flying
Circus .

Problem Solving Using Python Unit 1


• Python
– has a simple syntax.
– Programs are clear and easy to read.
– provides powerful programming features.
– is well supported and freely available at
www.python.org.
• Companies and organizations that use Python
include YouTube, Google, Yahoo, and NASA.

Problem Solving Using Python Unit 1


The IDLE Python
Development Environment
• The development environment provided with Python.
• IDLE is an integrated development environment ( IDE ).
• An IDE is a bundled set of software tools for program
development.
• It includes
– Editor – for creating and modifying programs
– Translator – for executing programs
• Program debugger - provides a means of taking control
of the execution of a program to aid in finding program
errors
Problem Solving Using Python Unit 1
• Python is most commonly translated by use of
an interpreter.
• The window that provides this interaction is
referred to as the Python shell .

Problem Solving Using Python Unit 1


The Python Standard Library
• The Python Standard Library is a collection of
built-in modules , each providing specific
functionality beyond what is included in the
“core” part of Python.
• To utilize the capabilities of a given module in
a specific program, an import statement is
used.

Problem Solving Using Python Unit 1


A Bit of Python
• Variables
– variable is “a name that is assigned to a value”
– n = 5 variable n is assigned the value 5
• Basic Arithmetic Operators
The common arithmetic operators in Python are
+ (addition)
 - (subtraction)
* (multiplication)
 / (division), and
 ** (exponentiation).

Problem Solving Using Python Unit 1


• Basic Input and Output
• In Python, input is used to request and get
information from the user.
Example
name = input('What is your name?: ')
• Characters within quotes are called strings .
• This particular use of a string, for requesting
input from the user, is called a prompt .
Problem Solving Using Python Unit 1
• The input function displays the string on the screen
to prompt the user for input,
What is your name?: Charles
• The underline is used here to indicate the user’s
input.
• Print is used to display information on the screen.
• >>> print('Welcome to My First Program!')
• Output
Welcome to My First Program!
Problem Solving Using Python Unit 1
Learning How to Use IDLE
• First, to create a Python program file, select
New File from the File menu in the Python
shell.

Problem Solving Using Python Unit 1


• A new, untitled program window will appear:

Problem Solving Using Python Unit 1


• Type the program

Problem Solving Using Python Unit 1


• Save the program. File  save

Problem Solving Using Python Unit 1


• Run the program. Run Run Module
• Or press F5.

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
• Execution

Problem Solving Using Python Unit 1


LITERALS
• A literal is a sequence of one or more
characters that stands for itself.
• CLASSIFICATION
– Numeric Literals
– String Literals
– Control Characters

Problem Solving Using Python Unit 1


Numeric Literals
• A numeric literal is a literal containing only
the digits 0–9, a sign character (+ or -) and a
• possible decimal point. Commas are never
used in numeric literals.
• If a numeric literal contains a decimal point,
then it denotes a floating-point value .
• otherwise, it denotes an integer value.

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
• Limits of Range in Floating-Point
Representation
– There is no limit to the size of an integer that can
be represented in Python.
– Floating-point values, however, have both a
limited range and a limited precision .
– Python uses a double-precision standard format
(IEEE 754) providing a range of 10^-308 to 10^308
with 16 to 17 digits of precision.

Problem Solving Using Python Unit 1


• Example

Problem Solving Using Python Unit 1


• Arithmetic Overflow
– Arithmetic overflow occurs when a calculated result is too
large in magnitude to be represented.
Example
the multiplication of two values may result in arithmetic
overflow , a condition that occurs when a calculated result is
too large in magnitude (size) to be represented,
>>>1.5e200 * 2.0e210
>>>inf

• This results in the special value inf (“infinity”) rather than


the arithmetically correct result 3.0e410, indicating that
arithmetic overflow has occurred.
Problem Solving Using Python Unit 1
• Arithmetic Underflow
• Arithmetic underflow occurs when a calculated result is too
small in magnitude to be represented.
• Example
– the division of two numbers may result in arithmetic underflow , a
condition that occurs when a calculated result is too small in
magnitude to be represented,
>>>1.0e2300 / 1.0e100
>>>0.0

• This results in 0.0 rather than the arithmetically correct


result 1.0e2400, indicating that arithmetic underflow has
occurred.
Problem Solving Using Python Unit 1
• Limits of Precision in Floating-Point Representation
• Since any floating-point representation contains only a
finite number of digits, what is stored for many floating-
point values is only an approximation of the true value.
• The loss of precision that can result in a calculated
result.
• Eg: 1/3 = 0.3333333…
• Eg : 3* (1/3) = 0.9999999999
• Python displays a rounded result to keep the number of
digits displayed manageable.
Problem Solving Using Python Unit 1
• Built-in Format Function
• The built-in format function can be used to produce a
numeric string of a given floating-point value rounded to
a specific number of decimal places.
• >>>12/5
• 2.4
• >>>format(12/5,’2f’)
• 2.40
• >>>format(13402.25, ' , .2f')
• 13,402.24
Problem Solving Using Python Unit 1
• String Literals
• A string literal, or string, is a sequence of
characters denoted by a pair of matching
single or double (and sometimes triple)
quotes in Python.
• Eg:
• 'Hello' 'Smith, John' "Baltimore, Maryland
21210"

Problem Solving Using Python Unit 1


• string literals may be delimited
• >>> print('Welcome to Python!')
• Welcome to Python!

Problem Solving Using Python Unit 1


• a string may contain zero or more characters,
including letters, digits, special characters, and
blanks.
• A string consisting of only a pair of matching
quotes (with nothing in between) is called the
empty string .

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
• Representation
• Unicode is capable of representing over 4
billion different characters, enough to
represent the characters of all languages, past
and present.
• Python’s (default) character encoding uses
UTF-8, an eight-bit encoding that is part of the
Unicode standard.
Problem Solving Using Python Unit 1
• Partial UTF-8 (ASCII) Code Table

Problem Solving Using Python Unit 1


• The ord function gives the UTF-8 (ASCII)
encoding of a given character.
• The chr function gives the character for a
given encoding value.

Problem Solving Using Python Unit 1


• Control Characters
– Control characters are nonprinting characters used
to control the display of output (among other
things).
– Control characters do not have a corresponding
keyboard character.
• Escape Sequence Character:
– An escape sequence is a string of one or more
characters used to denote control characters.
– \n  newline print(‘ Hello World \n Welcome to
this python programming’)
Problem Solving Using Python Unit 1
VARIABLES AND IDENTIFIERS
• A variable is a name (identifier) that is
associated with a value.

• A variable can be assigned different values


during a program’s execution—hence, the
name “variable.”

Problem Solving Using Python Unit 1


• Variables are assigned values by use of the
assignment operator , =
• Num = 10
• Num= num+1

Problem Solving Using Python Unit 1


• the right side of an assignment is evaluated
first, then the result is assigned to the variable
on the left .
• A variable assigned to another variable.

Problem Solving Using Python Unit 1


• Variables num and k are both associated with
the same literal value 10 in memory.
• >>>id(num)
• 505494040
• >>>id(k)
• 505494040
• The id function produces a unique number
identifying a specific value (object) in memory.
Problem Solving Using Python Unit 1
• Immutable Value :
• An immutable value is a value that cannot be
changed.

• If no other variable references the memory location of


the original value, the memory location is deallocated

Problem Solving Using Python Unit 1


• Finally, in Python the same variable can be
associated with values of different type during
program execution, as indicated below.
• Var = 12 integer
• Var = 12.45 float
• var = 'Hello' string

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
VARIABLE ASSIGNMENT & KEYBOARD INPUT

• Use of input function.


– To receive the value of the variable assigned by
the user.
• All input is returned by the input function as a
string type.
• Built-in functions int() and float() can be used
to convert a string to a numeric type.

Problem Solving Using Python Unit 1


Eg 1:
>>>name = input(‘What is your name?’)
>>>what is your name? Agalya
Eg 2 :
>>>s1=input(‘Enter the Mark1’)
>>>s2 = input(‘Enter the Mark2’)
>>> total = int(s1)+int(s2)
Eg 3:
>>>r=input(‘Enter the radius of the circle’)
>>>area = 3.14 * float(r) * float(r)
Problem Solving Using Python Unit 1
Identifiers
• An identifier is a sequence of one or more
characters used to name a given program
element.
• In Python, an identifier may contain letters
and digits, but cannot begin with a digit.
• The special underscore character can also be
used.

Problem Solving Using Python Unit 1


• Spaces are not allowed as part of an
identifier.
– This is a common error since some operating
systems allow spaces within file names.
– In programming languages, however, spaces are
used to delimit (separate) distinct syntactic
entities.
– Thus, any identifier containing a space character
would be considered two separate identifiers.

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
Keywords and Predefined Identifiers

• Keyword :
– A keyword is an identifier that has predefined
meaning in a programming language and
therefore cannot be used as a “regular” identifier.
This will result in a syntax error.
– Eg : >>> in=10
– SyntaxError: invalid syntax

Problem Solving Using Python Unit 1


• Keywords in Python

Problem Solving Using Python Unit 1


• There are other predefined identifiers that can
be used as regular identifiers, but should not
be. This includes float, int, print, exit, and quit.

Problem Solving Using Python Unit 1


OPERATORS
• Definition
• An operator is a symbol that represents an operation that
may be performed on one or more operands.
• A+B=C
• A, B, C  Operand
• +, =  Operator
• Types
– Operators that take one operand are called unary operators.Eg:
-7 negation operator
– Operators that take two operands are called binary operators.
Eg: 5+3, 6-2

Problem Solving Using Python Unit 1


• Arithmetic Operators
– Python provides various arithmetic operators
which are listed below.

Problem Solving Using Python Unit 1


• The + , - , * (multiplication) and / (division)
arithmetic operators perform the usual
operations.
• The symbol – is used both as unary and binary
operator.
Eg : 5-2=3 (- as binary operator)
Eg : 2-9 = -7 ( - as unary operator)

Problem Solving Using Python Unit 1


• In exponentiation, Integer and floating-point
values can be used in both the base and the
exponent.
– Eg : 3 **2 = 9 (3  Base 2 Exponent)
– Eg : 1.5 ** 2.5 = 2.7556759606312
• Python provides two forms of division. “true”
division is denoted by a single slash, /.
• Truncating division is denoted by a double
slash, //.
Problem Solving Using Python Unit 1
Problem Solving Using Python Unit 1
• The modulus operator (%) gives the
remainder of the division of its operands,
resulting in a cycle of values.
• The modulus and truncating (integer) division
operators are complements of each other.

Problem Solving Using Python Unit 1


• Eg :

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1
Expressions And Data Types
• An expression is a combination of symbols (or
single symbol) that evaluates to a value.
• A subexpression is any expression that is part of
a larger expression.
4 + (3 * 5)
4 and 3*5 are two subexpressions.
3*5 having two subexpressions.
• Expressions that evaluate to a numeric type are
called arithmetic expressions .
Problem Solving Using Python Unit 1
• From the Python Shell, enter the following
and observe the results.
• >>> (2 + 3) * 4 = 5*4 = 20
• >>> 2 + ((3 * 4)-8) = 2+(12-8)= 2+4=6
• >>> 2 + (3 * 4) = 2+12= 14
• >>> 2 + 3 * (4 – 1) = 2+3*3 = 2+9 = 11
– BODMAS rule (Bracket of Division Multiplication Addition &
Subtraction).

Problem Solving Using Python Unit 1


• Operator Precedence
– Operators appear between their operands, is
referred to as infix notation .(a+b)
• There are other ways of representing
expressions called prefix(+ab) and postfix(ab+)
notation, in which operators are placed before
and after their operands, respectively.

Problem Solving Using Python Unit 1


• Operator precedence of Arithmetic operators

• In the table, higher-priority operators are placed above


lower-priority ones
Problem Solving Using Python Unit 1
• Example : 4+ 3 * 5
• Multiplication is performed before addition
4+15 = 19

Problem Solving Using Python Unit 1


• Operator Associativity
• Operator associativity is the order that operators are
applied when having the same level of precedence,
specific to each operator.
• Eg: (8-4)-2  4-22
• For operators following the associative law
• A + (B +C) = (A+B) + C
• , the order of evaluation doesn’t matter,
• (2 + 3) + 4 ➝ 9
• 2 + (3 + 4) ➝ 9
Problem Solving Using Python Unit 1
• Division and subtraction, however, do not
follow the associative law,
• (8-4)-2 = 4-2=2 & 8-(4-2) = 8-2 = 6
• 2**(3**2) = 2**9 = 512 & (2**3)**2 =
8**2=64
• All operators except for exponentiation, have
left-to-right associativity—exponentiation has
right-to-left associativity.

Problem Solving Using Python Unit 1


• Eg: 2**3**2 = 2**9 = 512
• The exponentiation associativity is from right
to left. Hence solve 3**2 first.
• 2**9 . Then..The answer is 512.
• Eg : 6-3 + 2 = 6-5= 1

Problem Solving Using Python Unit 1


Data Type
• Definition
• A data type is a set of values, and a set of
operators that may be applied to those
values.
• A set of predefined data types in Python called
the built-in types .
• Eg: Integers, floats, and strings.

Problem Solving Using Python Unit 1


• Mixed-Type Expressions
– A mixed-type expression is an expression with
operands of different type.
• The CPU can only perform operations on
values with the same internal representation
scheme, and thus only on operands of the
same type. Operands of mixed-type
expressions therefore must be converted to a
common type.
Problem Solving Using Python Unit 1
• Coercion vs. Type Conversion
• Coercion is the implicit (automatic)
conversion of operands to a common type.
• Eg: 2+ 4.5
• 2.0 + 4.5 =6.5
• safe (automatic conversion of int to float)
• Type conversion is the explicit conversion of
operands to a specific type.
Problem Solving Using Python Unit 1
• Python provides built-in type conversion
functions int() and float(),
• Eg: float(2) + 4.5 = 6.5
• 2 + int(4.5) = 6

Problem Solving Using Python Unit 1


• numeric strings can also be converted to a
numeric type.
• Eg :
num_credits = int(input('How many credits do
you have? '))

Problem Solving Using Python Unit 1


Temperature Conversion Program
• Formula :
• Fahrenheit to celcius
C = F – 32 * (5/9)
Celcius to Fahrenheit
F= C * (9/5) + 32

Problem Solving Using Python Unit 1


Program
#program to convert temperature from fahrenheit to celcius and viseversa
print("Enter 1 to convert temperature from fahrenheit to celcius")
print("Enter 0 to convert temperature from celcius to fahrenheit")
x=int(input("Enter your choice"))
result=0.0

if x==1:
f=float(input("Enter the temperature in fahernheit"))
result=round((f-32)*(5/9),3)
print(str(result)+"C")
elif x==0:
c=float(input("Enter the temperature in celcius"))
result=round(c*(9/5)+32,3)
print(str(result)+"F")
else:
print("Invalid Input")

Problem Solving Using Python Unit 1


input("Press any key to exit")
Output
Enter 1 to convert temperature from fahrenheit to celcius
Enter 0 to convert temperature from celcius to fahrenheit
Enter your choice 1
Enter the temperature in fahernheit 98
36.667C
Press any key to exit
>>>
Enter 1 to convert temperature from fahrenheit to celcius
Enter 0 to convert temperature from celcius to fahrenheit
Enter your choice 0
Enter the temperature in celcius 36.6
97.88F
Press any key to exit Problem Solving Using Python Unit 1
Enter 1 to convert temperature from fahrenheit
to celcius
Enter 0 to convert temperature from celcius to
fahrenheit
Enter your choice 8
Invalid Input
Press any key to exit

Problem Solving Using Python Unit 1


Problem Solving Using Python Unit 1

You might also like