Showing posts with label Python import. Show all posts
Showing posts with label Python import. Show all posts

Friday, June 26, 2015

Writing python codes

So far all the codes I wrote was in Python Idle or terminal. Now is the time to write some code in file.
Open your favourite notepad or text editor or if you are using Python idle go to file ->new.
Write the following codes -
import math
a=9
b=math.sqrt(a)
print b



Save as ex1.py or with any other name you want with a .py extension.
For idle simply press f5 to run the program. For Linux go to the directory of the file by using cd command. And to run it type -
$python ex1.py
3
You will see 3 as output .
So what happened? Your first program worked. You called an inbuilt Python module 'math' by using import keyword of Python. I will cover more about import later but for now remember it is used to get previously made programs of Python to your current program. So the math module, as we call them , was already made by someone and added to Python for us. The math module has the function sqrt which we are using to get the root of the value a.
And then simply print the result with print b statement.

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...