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

Tuesday 3 October 2017

Print Area of a circle using Python

Let us write a small program today to print the area of circle in Python. The program will accept user input in the form of radius of the circle. There is a constant pi whose value we will be set to 3.142

Code :

print ('Program to calculate area of circle \n')

pi = 3.142
r = float(input('Enter radius : '))

a = pi*(r**2)

print ('Area of circle :', a )

Output :


OR


Sunday 6 March 2016

User Input example in Python

Every programming language has a way to accept user input. Like in Perl, you accept using STDIN. Similarly, Python interpreter has 2 inbuilt functions to read user input :

raw_input()
input()

Let's look into a simple python script which accepts user input and prints a statement.
Create a simple script - vi python_input.py

#!/usr/bin/python

name = raw_input('Please enter your name, visitor : ');

print ('Welcome to IroncladZone ' + name);

You see the variable "name" stores the input entered by the user. Accordingly we make use of this value in the next print statement.

Output :

./python_input.py

Please enter your name, visitor : Johnny

Welcome to IroncladZone Johnny

Wednesday 29 January 2014

Digital conversion using Python

Python can be used to solve a number of mathematical functions. This post will cover some basic in-built Python functions for digital conversion. Let's see some examples.

To convert an integer into a binary string, use the function bin(x)

Eg:

>>> bin(10)
'0b1010'

To convert an integer into a floating number, use the function float(x)

Eg:

>>> float(15)
15.0

To convert an integer into a hexadecimal string, use the function hex(x)

Eg:

>>> hex(51)
'0x33'

To convert a floating point number or decimal number to an integer, use the function int(x)

Eg:

>>> int(8.94943)
8

To convert an integer into an octal string, use the function oct(x)

Eg:

>>> oct(65)
'0101'

Python on Mac OSX

All Apple Macbooks come pre-loaded with Python. On Mac OSX, the Python interpreter is located at /usr/bin/python

You can enter the python prompt (denoted by >>>) by just typing 'python' in the terminal.

Also, in order to quit from the python interpreter (>>>) type quit() or press CTRL-D

Note that Python is case sensitive, so if you type QUIT or Quit instead of quit(), you would get the following error :

>>> Quit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Quit' is not defined
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker