Learning Python: Programming and Data Structures- Tutorial 1: Saying Hello World and Basic Input/Output

PYTHON


1. Let's just say Hello World in Python

# Let's just say Hello World for now
print "Hello, World!"

Output from the program above:

:~/work/pythontutorials$ python HelloWorld.py 
Hello, World!

2. Let's accept the user name as input and greet him with a customized greeting.

#Let's import the sys module which will provide us with a way to read lines from STDIN
import sys

print "Hi, what's your name?" #Ask the user what's his/her name
name = sys.stdin.readline()  #Read a line from STDIN
print "Hello " + name.rstrip() #Strip the new line off from the end of the string

Output from the program above:
~/work/pythontutorials$ python InputOutput1.py 
Hi, what's your name?
Prashant
Hello Prashant



4. Another example, where we ask the user to enter his first name, second name and age separated by spaces.

#Let's import the sys module which will provide us with a way to read lines from STDIN
import sys

print "Hi, what's your first name, second name and age. Separate them by a space?" #Ask the user what's his/her first name, second name and age separated by a space

[first_name,second_name,age] = sys.stdin.readline().rstrip().split(' ')  #Read a line from STDIN
print "First Name: " + first_name.rstrip() #Strip the new line off from the end of the string
print "Second Name" + second_name.rstrip() #Strip the new line off from the end of the string
print "Age:"
print int(age)
print "Next Year, this time your age will be:"
print int(age) + 1

Output from the program above:
~/work/pythontutorials$ python InputOutput2.py 
Hi, what's your first name, second name and age. Separate them by a space?
Mickey Mouse 20
First Name: Mickey
Second NameMouse
Age:
20
Next Year, this time your age will be:
21

5. In this example we will also learn how to format output. How to convert an entered string to its equivalent float value, how to display results till a specified number of decimal places, how to print numbers. 
We will accept an input length, convert it to its float equivalent. We will compute the area of a circle having that length as its radius. We will also compute the area of a square having that length as its side.
#we'll import the value of pi
import math
from math import pi


#Let's import the sys module which will provide us with a way to read lines from STDIN
import sys

print "Enter a valid length"
# input any length
input_length = sys.stdin.readline().rstrip()

#Input is a string => we need to make a floating number out of it
r = float(input_length)

# Compute and display the area of a circle with radius r
area_of_circle = pi * r * r

# The .2f in the formatting string below indicates that we'd like to see just 2 decimal places, i.e. x.xx
print "Area of a square with radius of  %.2f units = %.2f square units" % (r,area_of_circle)

# Area of a square with side r
# Enclosing an integer or float variable name in reverse inverted commas is a way of 
# making a string out of it
area_of_square = r * r
print "Area of a square with length " + `r` + " units = " + `area_of_square` + " square units"

#Area of an equilateral triangle with side r
#Another way of printing and formatting strings
#Use math.sqrt to find the square root 
area_of_triangle = math.sqrt(3) * r * r /4
print 'Area of a triangle', area_of_triangle


Output from the above program:

~/work/pythontutorials$ python inputOutput3.py
Enter a valid length
5.25
Area of a square with radius of  5.25 units = 86.59 square units
Area of a square with length 5.25 units = 27.5625 square units
Area of a triangle 11.9349125959