Skip to main content

Intro to Python Programming - 2. Basic Output

Hello everyone!

          Considering the lot of requests I received from my dear friends and viewers, I shall now start posting step by step tutorials for mastering Python programming. I proceed with the assumption that you have a basic foundation of the C programming language. If not much of it, at least having some idea about the type specifiers such as ‘%d for integer, %f for floating point variables etc.’ and also, some basic ideas about data types such as int, float, char etc. would be of great help. :)

          Before we begin with the coding part of python, I want you to understand a few basic concepts, like:
1.     Python is scripting language.
2.     Python programs do not make use of statement terminators like semicolons ‘;’ as in C, C++ and Java.
3.     A basic Python program does not require any header file.
e.g. We use #include<stdio.h> in C to include standard I/O library
We import libraries in Python only for some using some special functions like math operations.
4.     Python makes use of data types such as int, float, char etc. similar to C.
5.     Python has additional data types such as list[ ] and tuples( ), which are similar but more compatible and flexible than arrays in C.
6.     There is no function main( ) in Python as in C, C++ and Java.
7.     Python is an Object Oriented Programming language.
I hope I have cleared your doubts regarding the difference and similarities between Python and other programming languages. So let us proceed towards the coding part.
Let us start with our first program in Python. It’s very easy. The program to print “Hello World!” on the standard Output device (Screen).
The code is as follows:
print "Hello World!"

The above code has the following output:
Hello World!
          Now you have to be careful and understand which version of python you are using. I strongly recommend Python Version 2.7, since I feel that is more compatible and flexible compared to Python 3.5.
          If you are using Python 3.5, then the printing can be done using either of the two ways given below:
print("Hello All!")
 
print('Hello Everyone\n')

Remember, The parenthesis ‘(‘ and ‘)’ is mandatory in Python 3.
But, in case you are using Python 2, then you have the liberty to print onto standard output using either of the 4 ways given below:
                           print "Hello World!"

                      
                           print("Hello All!")



                           print 'Hello Universe!'



                           print('Hello Everyone\n')

It is important to remember that, in Python programming you have the liberty to use either Single quotes ( ‘ and ’ ) or double quotes like ( “ and ” )  within your print function. But make sure that you’re using either of the two at a given time. Mixing them would give you syntax errors.
For example, here’s some example for illegal print statements:
print "Hi there'



    AND



print 'Wassup?"

I hope you are now clear with basic output operation in Python programming.

Printing values contained in variables and constants:

So far we have seen the print sequence of string constants or messages onto the standard output device (Screen). It’s about time we saw how to print the values contained in the variables or constants onto the screen.
Consider printing the details of a student named Jayanth. He has attributes such as age, marks scored, grade and address.
We can initialize them as follows:
name = "Jayanth"

age = 18

marks = 92.73

grade = 'A'

address = " Koramangala, Bangalore- 5600034"
 
Now to print these details on to the screen we have two methods.

The first method is:

print "Name of the student is: %s"%(name)

print "Age of the student is: %d"%(age)

print "Marks scored by the student is: %f"%(marks)

print "Grade of the student is: %c"%(grade)

print "Address is: %s"%(address)

The above code gives us the following output:
                              Name of the student is: Jayanth
Age of the student is: 18
Marks scored by the student is: 92.730000
Grade of the student is: A
Address is: Koramangala, Bangalore- 5600034

This method makes use of type specifiers such as:
%d or %i for integer type or int
%f for floating point or float
%c for character type or char, and
%s for string type
Please make sure that after closing the quotes, you enclose the corresponding variable with %( variable_name) format.

NOTE: Please Do not use a comma after closing the quotes in method 1. It will give you an error!

          You can print more than one values using this method in the same print statement as follows:
i = 100

f = 3.1416
 
                       print "The values I need are:\t%d\t%f"%(i,f)
 
      
   Output:
The value of i and f are:  100 3.1416
          You can also make explicit type conversion (changing from one data type to another) in python using method 1 like this:
                          print "The floating point value of i is:\t%f "%(i)
Output:
The floating point value of i is: 100.000000

The second method would be:

print "Name of the student is: ",name

print "Age of the student is: ",age

print "Marks scored by the student is: ",(marks) #**

print "Grade of the student is: ",grade

print "Address is: ",address
 
          And yet the output would be the same as the output of the previous method.

          Note the ** marked print statement. Here I have written (marks) instead of marks. This does not create any problem and does not give you any error!

Most python programmers make use of the second method since it is very easy to implement. I would personally recommend the same.

          To print more than one values using this method:
i = 100

f = 3.1416

ch = 's'

strr = "Python is awesome!"
        
               print "The value of i and f are:\t",i,f

          Output:
The value of i and f are:  100 3.1416

You can insert strings in between the values in this method as:
print "The integer value is:",i,"and the character value is:",ch,"\n"

Output:
The integer value is: 100 and the character value is: s

I hope you like the tutorial on Standard output in Python programming language. I will be uploading more tutorials soon. Until then, please stay tuned on our blog.
Please don’t forget to subscribe and follow us! It means a lot.

Thank you very much.
Here is a program to summarize the entire concept of this post:

#Different Printing onto standard output in Python 2.7



print "Hello World!"

print("Hello All!")

print 'Hello Universe!'

print('Hello Everyone\n')



#Initializing some veriables

i = 100

f = 3.1416

ch = 's'

strr = "Python is awesome!"


#METHOD 1

#Printing the initialized values



print "\nThe value of i is:\t%d "%(i)

print "The value of i is:\t%i "%(i)

print "The value of i is:\t%f "%(i)

print "The values I need are:\t%d\t%f"%(i,f)

print "The character I used is: %c"%(ch)

print "The String I used is: %s \n"%(strr)



'''

**************ILLEGAL PRINT STATEMENTS***************

print "Hi there'

    AND

print 'Wassup?"


'''



#METHOD 2

#Easier method for printing data in python



print "The value of i and f are:\t",i,f

#Inserting string in between two values:

print "The integer value is:",i,"and the character value is:",ch,"\n"

#Printing strings

print "The string I used is: ",strr

print "\n****************************************************************************\n"



name = "Jayanth"

age = 18

marks = 92.73

grade = 'A'

address = "Koramangala, Bangalore- 5600034"



#PRINT DATA USING METHOD 1

print "Name of the student is: %s"%(name)

print "Age of the student is: %d"%(age)

print "Marks scored by the student is: %f"%(marks)

print "Grade of the student is: %c"%(grade)

print "Address is: %s\n"%(address)



#PRINT DATA USING METHOD 2

print "\nName of the student is: ",name

print "Age of the student is: ",age

print "Marks scored by the student is: ",(marks)

print "Grade of the student is: ",grade

print "Address is: ",address

Output:
Hello World!
Hello All!
Hello Universe!
Hello Everyone


The value of i is:  100
The value of i is:  100
The value of i is:  100.000000
The values I need are:     100    3.141600
The character I used is: s
The String I used is: Python is awesome!

The value of i and f are:  100 3.1416
The integer value is: 100 and the character value is: s

The string I used is:  Python is awesome!

****************************************************************************

Name of the student is: Jayanth
Age of the student is: 18
Marks scored by the student is: 92.730000
Grade of the student is: A
Address is: Koramangala, Bangalore- 5600034


Name of the student is:  Jayanth
Age of the student is:  18
Marks scored by the student is:  92.73
Grade of the student is:  A
Address is:  Koramangala, Bangalore- 5600034


You're valuable comments are well appreciated.
Please don not forget to subscribe and follow us! It means a lot.

Thank you very much. :) :D


Comments

Popular posts from this blog

Printing the text horizontally in Python Programming language

Hi guys,         We know that the "print" function is used to print the data to the standard output device in Python Programming language. But, the print function automatically adds a newline character every time we cal it.        For example, consider a program segment to display numbers from 1 to 5:                                                                            for n in range ( 1 , 6 ):                                     print n           The output of the code will be:                               1                               2                               3                               4                               5   But what if we want the output to be something like:                 1 2 3 4 5    How do we go about it?     The answer to this is indeed very simple. The key is to add a "," after the variable  at the end of the print statement. Consider the following code to print the even a

Kotlin :A New Beginning

Hello guys, You might have heard about a new language supported for the android Officially by Google,Named as Kotlin. Kotlin is a statically typed Programming language.By statically typed language i mean that the type checking is done during complile time. It was developed in July 2011 by Jetbrains in Russia,In 2017 Google declared Kotlin as the official langauge for android. However the Traditional Java developers are sticking to java. New programmers who want to make android apps,Web apps can use Kotlin as a language of preference. With this Post i welcome you to the world of Kotlin. There is no good book for kotlin but there is the official documentation on Kotlin.I refer the official Kotlin documentation. The main aim of this post if to Introduce you Kotlin and its Uses and also how to write programs in Kotlin. The official documentation can be found on the website   KOTLIN I belive that this post will help you to write codes in Kotlin. So lets begin Firstly