0% found this document useful (0 votes)
4 views12 pages

Python Assigment

Python Programming K scheme

Uploaded by

samsworld83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Python Assigment

Python Programming K scheme

Uploaded by

samsworld83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXP 2:

Aim: Prepare a flowchart and algorithm for simple problem.

Tools Used (Apparatus)


1.​ Flowchart Design Tool:
○​ Software: [Link], Lucidchart, or pencil/paper.
○​ Symbols: Terminal (start/end), Parallelogram (input/output), Diamond (decision),
Rectangle (process).
2.​ Algorithm Writing Tools:
○​ Text editor (e.g., Notepad, Google Docs).
3.​ Testing:
○​ Sample inputs (e.g., numbers like 5, -3, 0).

Related Theory
1.​ Algorithm:
○​ A step-by-step procedure to solve a problem. It is written in plain language or
pseudocode.
2.​ Flow Chart:
○​ A visual representation of an algorithm using standardized symbols to depict
processes, decisions, and flow of control.
3.​ Conditional Statements:
○​ Used to make decisions in algorithms (e.g., if, else if, else).
4.​ Key Concepts:
○​ Input/Output: Values provided by the user and results displayed.
○​ Decision Making: Checking conditions (e.g., number > 0).

Problem: Check if a number is even or odd.

Algorithm
1.​ Start
2.​ Input a number n.
3.​ If n % 2 == 0, print "Even".
4.​ Else, print "Odd".
5.​ End
(PREPARE FLOW CHART ON YOUR OWN)

Flow Chart Description


1.​ Start (Terminal Symbol)
2.​ Input n (Parallelogram Symbol)
3.​ Decision: Is n % 2 == 0? (Diamond Symbol)
○​ Yes: Print "Even" (Rectangle Symbol).
○​ No: Print "Odd" (Rectangle Symbol).
4.​ End (Terminal Symbol).

Problem 1: Find the Largest of Three Numbers


Algorithm:
1.​ Start.
2.​ Input three numbers: a, b, c.
3.​ If a > b and a > c, print a.
4.​ Else if b > a and b > c, print b.
5.​ Else, print c.
6.​ End.
Answer Example:
●​ Input: a = 5, b = 9, c = 3 → Output: 9.
(PREPARE FLOW CHART ON YOUR OWN)

Problem 2: Calculate Factorial of a Number


Algorithm:
1.​ Start.
2.​ Input a number n.
3.​ Initialize factorial = 1.
4.​ For i = 1 to n:
○​ Multiply factorial by i (factorial *= i).
5.​ Print factorial.
6.​ End.
Answer Example:
●​ Input: n = 5 → Output: 120 (5! = 5×4×3×2×1).
(PREPARE FLOW CHART ON YOUR OWN)

Problem 3: Check if a Number is Prime


Algorithm:
1.​ Start.
2.​ Input n.
3.​ If n <= 1, print "Not Prime".
4.​ Else, for i = 2 to √n:
○​ If n % i == 0, print "Not Prime" and exit.
5.​ If no divisors found, print "Prime".
6.​ End.
Answer Example:
●​ Input: n = 7 → Output: Prime.
●​ Input: n = 9 → Output: Not Prime.
(PREPARE FLOW CHART ON YOUR OWN)

Problem 4: Sum of First N Natural Numbers


Algorithm:
1.​ Start.
2.​ Input n.
3.​ Initialize sum = 0.
4.​ For i = 1 to n:
○​ Add i to sum (sum += i).
5.​ Print sum.
6.​ End.
Answer Example:
●​ Input: n = 4 → Output: 10 (1+2+3+4).
(PREPARE FLOW CHART ON YOUR OWN)

Conclusion: Flowcharts and algorithms are essential tools for structured problem-solving,
enabling clear visualization of logic and systematic decision-making. They bridge theoretical
concepts with practical implementation, fostering critical thinking and programming readiness.
Mastering these skills ensures efficient, error-free solutions across real-world applications.
EXP3:

Aim
To write a simple Python program that displays a message (e.g., "Welcome to Python
programming") using the print() function and understand its syntax and usage.

Tools Used (Apparatus)


1.​ Python Interpreter: Python 3.x installed on the system.
2.​ Code Editor: IDLE, VS Code, PyCharm, or any text editor.
3.​ Input/Output Tools: Keyboard (for inputting code) and console/screen (to view the
output).

Related Theory
1.​ print() Function:
○​ A built-in Python function used to display output on the screen.
○​ Syntax: print(*objects, sep=' ', end='\n', file=[Link], flush=False)
■​ *objects: Values to be printed (e.g., strings, numbers).
■​ sep: Separator between objects (default: space).
■​ end: Character appended after the last object (default: newline \n).
○​ String Handling:
■​ Enclose text in quotes (" " or ' ').
■​ Escape characters (e.g., \n for newline, \t for tab) can be used within
strings.

Algorithm
1.​ Start.
2.​ Use the print() function to display the message "Welcome to Python programming".
3.​ End.

Program Code
print("Welcome to Python programming")

Observation
1.​ Execution Process:
○​ When the program runs, the print() function sends the string argument to the
console.
○​ The message is displayed exactly as written, including spaces and punctuation.
2.​ Testing Variations:
○​ Test 1: print('Hello, World!') → Output: Hello, World!
○​ Test 2: print("C:\new_folder") → Output (without escape handling):
○​ Test 3: Using escape characters: print("C:\\new_folder") → Output:
C:\new_folder.
3.​ Key Observations:
○​ The print() function automatically adds a newline (\n) at the end unless modified
by the end parameter.
○​ Quotes (single or double) define strings but are not displayed in the output.

Conclusion
●​ The program successfully displayed the message "Welcome to Python programming"
on the console.
●​ Variations with escape characters and different string formats were tested, confirming the
flexibility of the print() function.
EXP 4:

Aim
To write a Python program that accepts user input to calculate:
1.​ Area of a rectangle (using length and width).
2.​ Area of a circle (using radius).

Tools Used (Apparatus)


1.​ Code Editor: IDLE, VS Code, Jupyter Notebook, etc.

RELATED THEORY

a. Identifiers
●​ Names given to variables, functions, or classes.
●​ Rules:
○​ Start with a letter or _.
○​ Cannot use keywords (e.g., if, for).
○​ Case-sensitive (e.g., Area ≠ area).​
Example: length = 10 # Valid identifier
○​ _width = 5.5 # Valid identifier
○​ 2radius = 3 # Invalid (starts with a number)
b. Indentation
●​ Defines code blocks (e.g., loops, conditionals).
●​ No braces {} in Python; indentation is mandatory.
●​ Example:

if length > 0:

print("Valid length") # Indented block

else:
print("Invalid")
c. Comments
●​ Explain code logic. Ignored by the interpreter.
●​ Single-line: # Comment here.
●​ Multi-line: ''' Comment here ''' or """ Comment here """.​
Example::

# Calculate area of rectangle


area = length * width # This is a comment
d. Variables
●​ Store data values. No explicit declaration needed.
●​ Dynamic typing: Type inferred at runtime.
●​ Example:

length = 5 # Integer

width = 3.2 # Float


shape = "Rectangle" # String
e. Operators
●​ Arithmetic: +, -, *, /, ** (exponent), // (floor division), % (modulus).
●​ Assignment: =, +=, -=, *=.
●​ Example:

x=5

y=3

sum = x + y #8

power = x ** y # 125 (5^3)


x += 2 # x becomes 7

f. Expressions
●​ Combinations of variables, operators, and values.​
Example:
area = (length * width) + 10 # Expression with arithmetic

Input Functions (2.3)


●​ input(): Reads user input as a string.
●​ Convert input to desired type using int() or float().​
Example:
radius = float(input("Enter radius: ")) # Converts input to float

Algorithm
1.​ Start.
2.​ Calculate Area of Rectangle:
○​ Input length and width from the user.
○​ Compute area_rectangle = length * width.
○​ Print the result.
3.​ Calculate Area of Circle:
○​ Input radius from the user.
○​ Compute area_circle = 3.14159 * radius ** 2.
○​ Print the result.
4.​ End.

CODE:
# Area of Rectangle
length = float(input("Enter length of rectangle: "))
width = float(input("Enter width of rectangle: "))
area_rectangle = length * width
print(f"Area of rectangle: {area_rectangle}")

# Area of Circle
radius = float(input("Enter radius of circle: "))
area_circle = 3.14159 * (radius ** 2)
print(f"Area of circle: {area_circle}")

Result
●​ The program successfully calculates:
○​ Area of rectangle (e.g., length=4, width=5 → 20.0).
○​ Area of circle (e.g., radius=3 → 28.27431).

Conclusion
This experiment demonstrated Python’s input handling and arithmetic operations to solve
real-world problems. It aligned with Chapter 2’s learning outcomes, emphasizing Python’s
building blocks, data types, and I/O functions.

Q1: What is the difference between = and == in Python?​


Answer:
●​ = is an assignment operator (e.g., x = 5).
●​ == checks equality (e.g., if x == 5).

Q2: Write a Python program to accept a number and print its square.
num = float(input("Enter a number: "))
print("Square:", num ** 2)

Q3: What is the purpose of indentation in Python?


Answer: Indentation defines code blocks (e.g., loops, functions). Incorrect indentation
causes syntax errors.

Q4: What happens if you multiply a string by an integer?


Answer:
The string is repeated (e.g., "Hi" * 3 → "HiHiHi").
EXP 5

AIM: Write a program to accept the value of Celsius and convert it to


Fahrenheit.

Apparatus: A computer with a suitable operating system (Windows, macOS, Linux).


●​ Python interpreter installed (version 3.6 or higher recommended).
●​ A text editor or Integrated Development Environment (IDE) for writing Python
code (e.g., VS Code, PyCharm, IDLE).

Related Theory:
●​ Input: The input() function is used to obtain data from the user. The input is read as
a string, so you might need to convert it to a numeric type (float or integer).
●​ Data Types: We'll use the float data type to handle potentially decimal temperature
values.
●​ Operators: Arithmetic operators (*, /, +) are used to perform the calculation.
●​ Output: The print() function is used to display the calculated Fahrenheit value to the
user.
●​ Type Conversion/Casting: The function float() will be used to convert the string
taken as input to a floating point number
●​ Variables: Variables are used to store the Celsius temperature, the calculated
Fahrenheit temperature
Conversion Formula
The formula to convert Celsius to Fahrenheit is:
°F = (°C * 9/5) + 32
Where:
●​ °F represents the temperature in Fahrenheit.
●​ °C represents the temperature in Celsius
OUTPUT:

Conclusion: This experiment successfully demonstrated how to write and execute a Python
program to convert temperatures from Celsius to Fahrenheit. The program takes user
input, performs a calculation based on the conversion formula, and displays the result.

Practice Questions:
Question: Why is it important to convert the input from the input() function to a
float before performing the calculation?

●​ Answer: The input() function returns a string. To perform arithmetic


operations, we need to convert the string to a numeric data type (float in this
case) to handle possible decimal values in temperature.

Question: What would happen if you didn't include the float() conversion in the
code?

●​ Answer: Python would treat the input as a string. If you tried to perform
arithmetic operations on it directly, you would likely get a TypeError because
you can't directly multiply or add strings and numbers in the way the
formula requires.

You might also like