python_math

Python Tutorial

About Python

Python is a very simple style syntax language. It uses a new line for a new statement. A line space for the scope of the functions and loops.

Why Should You Learn?

 

  • Python is very popular among data science and machine learning developers.
  • Google, DropBox, Facebook, and Amazan companies are using Python as a major language for software development.
  • It is used for numerical and mathematical computing.
  • It is functional, procedural, object-oriented and also works as the scripting language.

Python Tutorial

In this tutorial, I will  write  some programs  to illustrate the concept

  • Sum of Two Numbers

[code lang=”python”]

sum=0 #sum variable initialized to 0

x=3 #x variable initialized to 3

y=5 #y variable initialized to 5

sum = x + y # Add x and y and assigns to sum

print ( sum) # Prints sum of x and y

[/code]

 

The Output of the program would be 8

 

  • Sum of Natural Numbers

[code lang=”python”]

sum=0                                  #Initialize variable sum equal to zero
i=0                                          #Initialize variable i equal to zero
while i <= 10:                     # Check the condition
sum += i                              # Sum of natural numbers
i += 1                                     # Increment the variable i

print(sum)                         # Print sum of the natural numbers

[/code]

 

The output of the program would be    55

 

  • Factorial of Natural Numbers

[code lang=”python”]

fact=1                #Initialize variable fact equal to 1
i=1                      # Initialize variable i equal to 1
while i <= 5:    # Check the condition
fact *= i              # Calculate factorial
i += 1                  # Increment the variable i

print(fact)        # Print Factorial

[/code]

 

The output of the program would be    120

 

  • “import” statement in Python

To use modules library, Python uses import command.

For instance, if you want to use a random number generator.

You will write as 

import math

and you will be able to use the function fmod (x,y).

See the program

 

[code lang=”python”]

import math

print(math.fmod(35,4))

[/code]

 

The output of the program

python_math

 

 

fmod(x, y)  function is defined in math.py library.

“as” statement with import

“as”statement is used to create an alias of library name.

The same program you can write as

[code lang=”python”]
import math as m
print(m.fmod(35,4))
[/code]

 

Function-

A function is a block of statements that performs a specific task.

Python uses def keyword to define a function.

For example, add function for  adding two numbers

[code lang=”python”]

def add(x,y):
return x+y

print(add(5,3))

[/code]

Calculating factorial of a natural number

[code lang=”python”]

def factorial( n):
fact=1
i = 1
while i < n:
i+= 1
fact = fact * i
return fact

print(factorial(5))

[/code]

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.