MP 1 Arsd Lecture4
MP 1 Arsd Lecture4
10-Sept-2024
on
“Python Basics”
Mathematical Physics -1
by
Prof. Vinita Tuli
Prof. Anjali Kaushik
Shivnandi
Sneh
INPUT from USER
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
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
# Input coefficients
a=
b=
c=
# Input coefficients
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient 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
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
TRY IT! Verify that len is a built-in function using the type function.
type(len)
builtin_function_or_method
Functions Basics
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
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
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 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)