Skip to main content

Using Python as calculator and some operators

Using Python as calculator
Python has the very large number of libraries and each library has its own  specific use. But before heading towards advance Python, we must be knowing the basics of it.

What you have to do is to simply open your Python Console in pycharm and we are all set to go.In the Python Console as well as in our examples , input and output are distinguished by the presence or absence of prompts (>>> and ): to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with the prompt are output from terminal .And we will be giving comments with #. Click here to know more about comments.

Now, open your python console and wait for the prompt(>>> ) to appear.(It must not take too long to appear)

eg.

#Some basic operations

>>> 67+89 ↵  #Input
156                 #Output

>>> (659+567)/(456-345)*6 
 66.27027027027027             Output Can also generate floating point output

# Now using /, // and % operators.

>>> 17/10 ↵  # simple division operation
1.7

>>> 17//10 ↵  # // returns floor value(discard fractional part)
1


>>> 17%10 ↵  # // return remainder  

7

# Now using ** operators.

>>> 17**2 ↵  # return power of a number
1.7

# You can also give floating point numbers(Decimal numbers) as input .

>>> 18.0/2.0 ↵  # return floating point number
9.0

# You can also store numbers in a variable and then perform the same operations.

>>> a=2.0 ↵  # assign 2.0 to a
>>> b=3.0 ↵  assign 3.0 to b
>>> a**b   ↵  calculating a to the power b
8.0              Output

# We can also do above operations in this way:

>>> a=2.0    ↵  # assign 2.0 to a
>>> b=3.0    ↵  assign 3.0 to b
>>> c=a**b   ↵  calculating a to the power b
>>> c          
>>> 8.0           Output

# An error will occur if you try to access undefined variables

>>> k

Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'k' is not defined

# We can also use floating point and integer number together

>>> 8.0/3
2.6666666666665

NOTE: Remember that we cannot use ++ or -- operators in python as they are not supported and when used throw an error 










Comments

Popular posts from this blog

How to install python IDE ? IDLE and Pycharm

How to install Python IDE on Windows 10(Pycharm and IDLE)? Pycharm is the best Python IDE available.It comes with many in -built features. We will discuss its shortcuts in our next blogs.Pycharm is a cross-platform editor developed by jetbrains.   Installing Python Step 1) Visit the official website of python or click the below link              https://www.python.org/downloads/  .  Step 2) Click on Download Python               Step 3) After the download is complete ,open the exe file and click               on Install now  Step 4) After the successful installation it will show you a message         Setup was successful Step 5) Now go to your windows search bar and type "IDLE"              Press Enter and you will see that idle is opened Installing Pycharm St...

String Slicing in python

String Slicing Python  Slicing refers to extracting some words from a string. As in many other languages there are many different concepts for extracting a letters or words from a string ,in python we are having the concept of slicing. We can also the same things using looping, we will learn about it in our next tutorials. Indexing in Strings Before starting slicing, we need to understand the indexing in python. We have the concept of positive indexing  and  negative indexing.  Positive indexing starts from 0 to (length of string).It goes from forward to backward. Negative indexing starts from -1 to -(length of string + 1). It is illustrated in below example Below are some examples of how to use string slicing to extract characters from string. Accessing the characters in a string using positive index eg. string= "We are studying string slicing" s=string[ 0 ] # A single character will be printed present at index 0 w=string[ 4 ] # A...