Partial preview of the text
Download mathematical physics sem 1 and more Summaries Mathematical Physics in PDF only on Docsity!
KIRORI MAL COLLEGE UNIVERSITY OF DELHI NAME: ROLL NUMBER: EXAMINATION ROLL NUMBER: SEMESTER: SUBJECT: SUBMITTED TO: PRAGYA TIWARI 2330228 23036567073 457 MATHEMATICAL PHYSICS MR. KIRTEE KUMAR UNIT-1 BASIC ELEMENTS OF PYTHON PYTHON: Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: - web development (server-side), - software development, - mathematics, - system scripting. PYTHON INTERPRETER: The Python interpreter is like a language translator for your computer. It takes the instructions you write in Python and converts them into a language the computer understands, allowing your programs to run and do what you've told them to do.The Python Interpreter itself consists of two parts: a compiler and the Python Virtual Machine (PVM) EXAMPLE: ers/Poorvi Tiwai File Edit Run Options Window Help |#pragya ari 2330228 | #PYTHON INTERPRETER Ja = "hello world' |print (a) OUTPUT: = = RESTART: C:/Users/Foorv hello world >>> PRINT STATEMENT: The print() function prints the specified message to the screen, or other standard output device. OUTPUT: 70 27 5670 6.1285714128571429 22> VARIABLES IN PYTHON: Python Variable is containers that store values. Python is not âstatically typedâ. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it. A Python variable is a name given to a memory location. It is the basic unit of storage in a program. ÂŤ NUMERIC VARIABLE: There are three numeric types in Python: - Integer - Float - Complex Variables of numeric types are created when you assign a value to them. EXAMPLE: File Edit Format Run Options Window Help |#pragya tiwari 2330228 #NUMERIC VARIABLE a= 18 |b = 45.18 je 74 |print (a) |print (type (a)) |print (b) \print (type (b) ) | print (c) |print (type (c)) OUTPUT: 18 45.18 74 >>> e CHARACTER VARIABLES: STRING: - Strings in python are surrounded by either single quotation marks, or double quotation marks. EXAMPLE: File Edit Format Run Options Window Help #pragya tiwari 2330228 #STRING a= 'this is awesomeâ print (a) |print (type (a)) OUTPUT: this is awesome >>>|| PYTHON SEQUENCE: A sequence is a positionally ordered collection of items. ¢ List: Lists are used to store multiple items in a single variable. ¢ Tuple: Tuples are used to store multiple items in a single variable. ¢ Dictionary: Dictionaries are used to store data values in key: value pairs. A dictionary is a collection which is ordered, changeable and do not allow duplicates. EXAMPLE: File Edit Format Run Options Window Help #pragya tiwari 2330228 #PYTHON SEQUENCE #list cricketers = ['virat','rohit', 'dhoni"] print (cricketers) print (type (cricketers) ) #tuple country = ("india', âaustrailia', print (country) print (type (country) ) #dictionary elements = {'gold':'metal', âcarbo: print (elements) print (type (elements) ) uth africaâ) "non metal", "boron": 'metalloid PYTHON INPUT STATEMENT: In Python, the input() function is used to take user input. It reads a line from the user, converts the input to a string, and returns that string. Here is a simple example: EXAMPLE: B, *pragya t python py - C/Users/Poorvi Ti File Edit Run Options Window Help #pragya tiwari 2330228 #INPUT STATEMENT input ("enter value of x:") b = input (âenter value of y:') | print (a+3*b) jownloads/pragy4 rm OUTPUT: >>>| enter value of x enter value of y:5 4555 >>>] LIST METHODS: Python has a set of built-in methods that you can use on lists. METHOD DESCRIPTION append() Used for appending and adding elements to the end of the List. copy() It returns a shallow copy of a list clear() This method is used for removing all items from the list. count() These methods count the elements extend() Adds each element of the iterable to the end of the List index() Returns the lowest index where the element appears. insert() Inserts a given element at a given index in a list. Pop() Removes and returns the last value from the List or the given Index value. remove() Removes a given object from the List. reverse() Reverses objects of the List in place. Sort() Sort a List in ascending, descending ,or user-defined order min() Calculates the minimum of all the elements of the List max() Calculates the maximum of all the elements of the List. EXAMPLE: B pragya t python. py - C:/Users/Poorvi Tiwari/Downloads/pragya t python.py (3.12.0) File Edit Format Run Options Window Help #pragya tiwari 2330228 #LIST METHODS cricketers = ['virat','rohit','dhoni','bumrah', 'siraj"] print (cricketers) cricketers.append('shami') print (cricketers) a = cricketers.copy() print (a) print (a.count ("virat')) a.extend (cricketers) print (a) print (cricketers .index ('shami')) cricketers.insert (4,'kl rahulâ) print (cricketers) cricketers.pop (3) print (cricketers) cricketers.reverse () print (cricketers) cricketers.clear () print (cricketers) OUTPUT: RESTART: C:/Users/Poorvi Tiwari /Downlo. pragya t python.py [ivirat', âohit', 'd Npumrah', "aira: [ivirak', teotil', tdhonit, âhumratiâ, 'siruyt, thomâ ('viratâ, ârohit'; âdhoni', âbumrah', âsiraj', âshami"] Tivirat', ârohitâ, âdhoni', âbumrah', 'oiraj', âchami', âvizat', ârohitâ, âĂŠhoni", "humrshâ, âcizaj', 'ohami* ['viratâ, ârohitâ, âdhoni', âbumrah', âkl rahul", âshani'] [ivirat', ârohit', âdhoni', 'Kl rahal', âeirajâ, Mohami', âciraj'; âkl rahil', âdhoni', ârobit", ul LIST MUTABILITY: Lists in Python are mutable data types as the elements of the list can be modified, individual elements can be replaced, and the order of elements can be changed even after the list has been created. EXAMPLE: File Edit Format Run Options Window Help #pragya tiwari 2330228 #LIST MUTABILITY countries = |'india','austrailia','new zealand','south atrica'| print (countries) countries[1] = 'sri lanka' countries[-2] â âenglandâ print (countries) EXAMPLE: Opt gpragya tiwari 2540228 #CONDITLONAL OPERATIONS thea bt) than aâ) 4 ftimisnes oft in style") print print ("Sharma has done it") io goen dows the graued ,Kebli goes oul of the greund') OUTPUT: >>> RESTART: C:/Users/Poo b is greater than a Kohli goes down the ground ,Kohli goes out of the ground PP> SS WHILE AND FOR LOOPS: With the while loop we can execute a set of statements if a condition is true. A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). EXAMPLE: Fle Eclt Format Run Ontions Window Help Hpeagya Liwacl 2330228 TWHILE AND 703 print () FINISHES CEF IN STYLE", "INDIA ket â ("TE THE WORLD C7P"] for enamentries in print (connentziea) OUTPUT: RESTA number multiple number multiple number multiple number multiple number multiple number multiple number multiple number multiple OUAHAWNE II THIS IS A SENSATIONAL HIT FROM VIRAT KOHLI SHARMA HAS DONE IT DHONI FINISHES OFF IN STYLE INDIA LIFT THE WORLD CUP >>> INDENTATION, BREAK AND CONTINUE: Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. The break keyword is used to break out a for loop, or a while loop. EXAMPLE (BREAK): iwari/Downloads/pragya t python.py File Edit Format #pragya tiwari 2 #DREAK OUTPUT: ce 3 S = >>>'l EXAMPLE (CONTINUE): i/Downloads/pragya t python.py (3.12.0)* File Edit Format Run Options Window Help #pragya tiwari 2330228 #CONTINUE >> PROGRAM TO SOLVE QUADRATIC EQUATION: A quadratic equation is a polynomial equation of degree 2, which means it contains a term with a variable raised to the power of 2. It takes the form: gelsius 37.5 fahrenheit 99.5 >>> PYTHON INBUILT FUNCTIONS: Here is a comprehensive list of Python built-in functions: FUNCTIONS abs() all() any() ascii() bin() bool() bytearray() bytes() callable() chr() classmethod() compile() complex() delattr() dict() dir() divmod() enumerate() eval() exec() filter() float() format() frozenset() getattr() globals() hasattr() hash() help() DESCRIPTION Returns the absolute value of a number Returns True if all items in an iterable object are true Returns True if any item in an iterable object is true Returns a readable version of an object. Replaces none-ascii characters with escape character Returns the binary version of a number Returns the boolean value of the specified object Returns an array of bytes Returns a bytes object Returns True if the specified object is callable, otherwise False Returns a character from the specified Unicode code. Converts a method into a class method Returns the specified source as an object, ready to be execute Returns a complex number Deletes the specified attribute (property or method) from the specified object Returns a dictionary (Array) Returns a list of the specified object's properties and methods Returns the quotient and the remainder when argument is divided by argument2 Takes a collection (e.g. a tuple) and returns it as an enumerate object Evaluates and executes an expression Executes the specified code (or object) Use a filter function to exclude items in an iterable object Returns a floating point number Formats a specified value Returns a frozenset object Returns the value of the specified attribute (property or methods) Returns the current global symbol table as a dictionary Returns True if the specified object has the specified attribute (property/method) Returns the hash value of a specified object Executes the built-in help system hex() id() input() int() isinstance() issubclass() iter() len() list() locals() map() max() memoryview() min() next() object() oct() open() ord() pow() print() property() range() repr() reversed() round() set() setattr() slice() sorted() staticmethod() str() sum() super() tuple() type() vars() Zip() Converts a number into a hexadecimal value Returns the id of an object Allowing user input Returns an integer number Returns True if a specified object is an instance of a specified object Returns True if a specified class is a subclass of a specified object Returns an iterator object Returns the length of an object Returns a list Returns an updated dictionary of the current local symbol table Returns the specified iterator with the specified function applied to each item Returns the largest item in an iterable Returns a memory view object Returns the smallest item in an iterable Returns the next item in an iterable Returns a new object Converts a number into an octal Opens a file and returns a file object Convert an integer representing the Unicode of the specified character Returns the value of x to the power of y Prints to the standard output device Gets, sets, deletes a property Returns a sequence of numbers, starting from 0 and increments by 1(by default) Returns a readable version of an object Returns a reversed iterator Rounds a numbers Returns a new set object Sets an attribute (property/method) of an object Returns a slice object Returns a sorted list Converts a method into a static method Returns a string object Sums the items of an iterator Returns an object that represents the parent class Returns a tuple Returns the type of an object Returns the _dict_ property of an object Returns an iterator, from two or more iterators PYTHON MODULES: Modules on Python are files that contain Python code, such as functions, classes, variables, or constants, that can be imported and used by other programs or modules. EXAMPLE: FB *pragya t pytho File Edit Format Run Options Window Help #pragya tiwari 2330228 rt math print (math.sqrt (25) ) isers/Poorvi jwari/Downloads/pragya t pyt OUTPUT: FINDING LARGEST NUMBER: Python can be used to find largest number among given number. EXAMPLE: \sers/Poorwi Tiwari/Downloads/pragya t pythor File Edit format Run tpragya tiwari 2330 ions Window _ Ilelp | pum = tloat (input ("enter tirst number:')) |num2 tloat (input ("enter second number:')) jnum3 = tloat(input('enter third number:')) if (numl>=num2) and (num1>=num3): largest = numl elif (num2>=numl) and (num2>ânum3) : largest = num2 largest = num3 print('the largest number is:',largest)| OUTPUT: >>> enter first number:4 enter second number:7 enter third number:9 the largest number is: 9.0 >>> LISTING PRIME NUMBERS: EXAMPLE: # PRAGYA TIWARI 2330 int (input ('5: 2")) auensnaninmameens ==== RESTART: C:\Users\DELL\ |Bnter the st. g range:5 |Enter the end range:50 |prime in the range 5 to 50 \5 |7 Fe 13 W7 19 \23 \29 \31 )37 ) 4a 43 \a7 PROGRAM 1: Make a function that takes a number N as input and returns the value of factorial of N. Use this function to print the number of ways a set of m red and n blue balls can be arranged. ragya t pytho inloads/ File Edit Forrnat Run Options Window Help #pragya tiwari 2330228 import random # Generate random integer in the range [a, b] random integer = random.randint (1, 10) # Generate random float in the range [a, b] random float = random.uniform(1.0, 10.0) print (f"Random Integer: {random integer}") print (f"Random Float: {random float}") # Calculate area of a square side length = random float area square = side _length**2 # Calculate volume of a cube side_length_cube = random_float volume cube = side length cube**3 print (f"Area of Square with side length print (f"Velume of Cube with side length length}: {area_square}") ength cube}: {volume_cube}") OUTPUT: = = RESTART: C:/Users/Poorvi Ti Random Integer: 6 Random Float: 7.7528106503480725 Area of Square with side length 7.7528106503480725: 60,1060729801505 Volume of Cube with side length 7.7528106503480725: 465.9910027511093 D>? >Âť UNIT -2 NUMPY FUNDAMENTALS NUMPY: NumPy is a powerful numerical library for Python that provides support for large, multi- dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. It is widely used in scientific computing, data analysis, and machine learning due to its efficiency and versatility. IMPORTING NUMPY: Once NumPy is installed, import it in your applications by adding the import keyword. EXAMPLE: B; pragya t python.py - C/Users/Poorvi i/Downloads/pragya t python.py (3.12.0) File Edit Format Run Options Window Help |#pragya tiwari 2330228 | #IMPORTING NUMPY |) im: ort numpy as np |a = np.array((1,2,3,41) |print (a) OUTPUT: S>> DIFFERENCE BETWEEN LIST AND ARRAY: 1. Mathematical operations: One of the main differences between a list and an array is that while you can store an integer or float in a list, you canât really do mathematical operations in it. On the other hand, NumPy arrays can handle mathematical operations with ease 2. Memory consumption: A list can store different types of data while an array doesnât. This is one of the reasons why a list consumes more memory than an array 1. 3. Indexing and slicing: Both lists and arrays can be indexed and sliced, but arrays are faster for vectorial functions than mapping functions to lists 2. 4. Mutability: Both lists and arrays are mutable, meaning that you can replace or change one of the data in a list or array. ADDING, REMOVING AND SORTING ELEMENTS USING NUMPY: numpy.add() function is used when we want to compute the addition of two array. It add arguments element-wise. If shape of two arrays are not same, that is arr1.shape !=