0% found this document useful (0 votes)
14 views

MP 1 Arsd Lecture4

Uploaded by

sciyasingh
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)
14 views

MP 1 Arsd Lecture4

Uploaded by

sciyasingh
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
You are on page 1/ 21

Lecture 4

10-Sept-2024
on
“Python Basics”

Mathematical Physics -1
by
Prof. Vinita Tuli
Prof. Anjali Kaushik
Shivnandi
Sneh
INPUT from USER

➢ Ask for the user's name and print it:

TRY IT!
# Example 1: Taking name input
name = input("Enter your name: ") # The user will input their name
print(f"Hello, {name}! Welcome!") # Output will greet the user with their name

Enter your name: Shiv


Hello, Shiv! Welcome!
Change temperature

Ques 1 Write a python program to convert


temperature from celcius to fahrenheit
Change temperature

# Input temperature in Celsius


celsius = float(input("Enter temperature in Celsius: "))

# Convert Celsius to Fahrenheit


fahrenheit = (celsius * 9/5) + 32

# Display the result


print(f"The temperature in Fahrenheit is: {fahrenheit}")
If-Else Statements

➢ A branching statement, If-Else Statement, or If-Statement for short, is a code


construct that executes blocks of code only if certain conditions are met.

if logical expression P:
code block 1
if logical expression: elif logical expression Q:
code block code block 2
elif logical expression R:
code block 3
else:
code block 4
Change temperature

Ques 2 Write a python program


• Which asks user whether it is degree Celsius or kelvin and
Fahrenheit
• Then ask user in which unit you want the temperature
• Then, it takes input temperature as input from the user
and convert it
• Hint:: Use if-else statements carefully
Solve Quadratic equation
import math

# Input coefficients
a=
b=
c=

# Check if the equation is quadratic


if a == 0:
print("This is not a quadratic equation.")
else:
# Calculate the discriminant
discriminant =
if discriminant > 0:
# Two real and different roots
root1 = # use math.sqrt function
root2 = # use math.sqrt function
print(f"The roots are real and different: {root1} and {root2}")
elif discriminant == 0:
# One real root (both roots are the same)
root =
print(f"The roots are real and the same: {root}")
else:
# No real roots, roots are complex
real_part =
imaginary_part = # use math.sqrt function
print(f"The roots are complex: {real_part} + {imaginary_part}i and {real_part} - {imaginary_part}i")
Solve Quadratic equation
import math

# Input coefficients
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))

# Check if the equation is quadratic


if a == 0:
print("This is not a quadratic equation.")
else:
# Calculate the discriminant
discriminant = b**2 - 4*a*c

if discriminant > 0:
# Two real and different roots
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
print(f"The roots are real and different: {root1} and {root2}")
elif discriminant == 0:
# One real root (both roots are the same)
root = -b / (2 * a)
print(f"The roots are real and the same: {root}")
else:
# No real roots, roots are complex
real_part = -b / (2 * a)
imaginary_part = math.sqrt(-discriminant) / (2 * a)
print(f"The roots are complex: {real_part} + {imaginary_part}i and {real_part} - {imaginary_part}i")
For-loop

➢ A for-loop is a set of instructions that is repeated, or iterated, for every value in a


sequence. Sometimes for-loops are referred to as definite loops because they
have a predefined begin and end as bounded by the sequence.

TRY IT! Print numbers 1 to 5


for i in range(1, 6):
print(i)
For-loop

➢ A for-loop is a set of instructions that is repeated, or iterated, for every value in a


sequence. Sometimes for-loops are referred to as definite loops because they
have a predefined begin and end as bounded by the sequence.

TRY IT! Calculating 5!


n=5
factorial = 1
for i in range(1, n + 1):
factorial *= i
print(f"The factorial of {n} is: {factorial}")
While loop

➢ A while loop or indefinite loop is a set of instructions that is repeated as


long as the associated logical expression is true. The following is the
abstract syntax of a while loop block.

while <logical expression>:


# Code block to be repeated until logical
statement is false
code block
While loop

➢ A while loop or indefinite loop is a set of instructions that is repeated as


long as the associated logical expression is true. The following is the
abstract syntax of a while loop block.

TRY IT! 5! Using while loop


n=5
factorial = 1
counter = 1

while counter <= n:


factorial *= counter
counter += 1

print(f"The factorial of {n} is: {factorial}")


Infinite While loop

➢ A while loop or indefinite loop is a set of instructions that is repeated as


long as the associated logical expression is true. The following is the
abstract syntax of a while loop block.

DONOT TRY IT!


n=0
while n > -1:
n += 1
Break Statement

➢ The break statement is used to exit a loop prematurely when a certain


condition is met. Once break is encountered, the loop stops immediately,
and control moves to the next statement after the loop.

TRY IT!
# Print numbers from 1 to 10 but stop when it
reaches 6
for i in range(1, 11):
if i == 6:
break # Exit the loop when i is 6
print(i)
print("Loop stopped because of break.")
Continue Statement

➢ The continue statement skips the current iteration of the loop and moves
on to the next iteration. When continue is encountered, the remaining
code within the loop is skipped for that iteration only.

TRY IT!
# Print numbers from 1 to 10 but skip the number 6
for i in range(1, 11):
if i == 6:
continue # Skip the rest of the loop when i is 6
print(i)
print("Loop continued after skipping 6.")
Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task. A function is a block of code that can run when it is called. A
function can have input arguments, which are made available to it by the
user, the entity calling the function. Functions also have output
parameters, which are the results of the function that the user expects to
receive once the function has completed its task. For example, the
function math.sin has one input argument, an angle in radians, and one
output argument, an approximation to the sin function computed at the
input angle (rounded to 16 digits).

TRY IT! Verify that len is a built-in function using the type function.
type(len)
builtin_function_or_method
Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! Verify that np.linspace is a function using the type function.
And find how to use the function using the question mark.
import numpy as np

type(np.linspace)

function
Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! Verify that np.linspace is a function using the type function.
And find how to use the function using the question mark.
np.linspace?
Define your own function

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! We can define our own functions. A function can be specified in several ways. Here we will
introduce the most common way to define a function which can be specified using the keyword def, as
showing in the following:
def function_name(argument_1, argument_2, ...):
'''
Descriptive String
'''
# comments about the statements
function_statements

return output_parameters (optional)


Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! Define a function named my_adder to take in 3 numbers


and sum them.
def my_adder(a, b, c):
"""
function to sum the 3 numbers
Input: 3 numbers a, b, c
Output: the sum of a, b, and c
author:
date:
"""

# this is the summation


out = a + b + c

return out
Functions Basics
TRY IT! Define a function named my_adder to take in 3 numbers
and sum them.
def my_adder(a, b, c):
"""
function to sum the 3 numbers
Input: 3 numbers a, b, c
Output: the sum of a, b, and c
author:
date:
"""
# this is the summation
out = a + b + c

return out

d = my_adder(1, 2, 3)
print(d)

You might also like