Python Tutorial


python_math

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?

 

  • 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

</p>
<p>sum=0                  #sum variable initialized to 0</p>
<p>x=3                    #x variable initialized to 3</p>
<p>y=5                    #y variable initialized to 5</p>
<p>sum = x + y           # Add x and y and assigns to sum</p>
<p>print ( sum)          # Prints sum of x and y</p>
<p>

 

Related Posts  How Do API Key Work?

The Output of the program would be 8

 

  • Sum of Natural Numbers

</p>
<p>sum=0                                  #Initialize variable sum equal to zero<br />
i=0                                          #Initialize variable i equal to zero<br />
while i &lt;= 10:                     # Check the condition<br />
sum += i                              # Sum of natural numbers<br />
i += 1                                     # Increment the variable i</p>
<p>print(sum)                         # Print sum of the natural numbers</p>
<p>

 

Related Posts  Factorial in CPP Using Class

The output of the program would be    55

 

  • Factorial of Natural Numbers

</p>
<p>fact=1                #Initialize variable fact equal to 1<br />
i=1                      # Initialize variable i equal to 1<br />
while i &lt;= 5:    # Check the condition<br />
fact *= i              # Calculate factorial<br />
i += 1                  # Increment the variable i</p>
<p>print(fact)        # Print Factorial</p>
<p>

 

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

 

Related Posts  Binary Search Program in CPP(C++)

</p>
<p>import math</p>
<p>print(math.fmod(35,4))</p>
<p>

 

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

</p>
<p>import math as m</p>
<p>print(m.fmod(35,4))</p>
<p>

Have any Question or Comment?

Leave a Reply

Subscribe to Our Newsletter