Introduction to Python – part 2

In the previous post  of Python series, I give you a brief introduction of Python and how to create a simple program using Visual Studio 2013.  In this post I am planning to cover some basic things about how to display output, get input from the user and strings and variables.  If you are wondering how to user or create python project using Visual Studio, please refer to Introduction to Python – Part 1.

How to use Strings and Input and Output Keywords

Lets start and learn some basic or fundamental stuff of Python. Create a new Python Project in Visual Studio and name it HelloPython. Let’s examine the Print statement. To output on console window Print keyword is used. To output Hello World we can use single or double quotes. We can use either  print('Hello World') or  print("Hello World") the output will be same. Python allows to use both single or double quote, but it is recommended to use one of them. This way your code looks more cleaner and readable. I am going to use double quotes in the examples. Another interesting thing is that Python accepts triple quotes as well. The difference between triple quotes is that it will display the output as it is written. Let’s say if you write the statement in the below fashion,

The output window will show the result as,


Now what if you want to display single quote within your string. Like  print('What's the update') . If you try to run the program, it will throw  syntax error. To overcome this issue use backslash. Now the same statement will look like this,  print('This\'s is single quote'). To create a new line we can use  \n . Now what if we want to use backslash in a string then we can place double backslash.  print("This string will\n display newline and \\backslash in a string").
To make application more interactive let us see how we can get input from the user. But before that one small thing, if we want to write down comment how we can do that in Python. We will use hash or number sign to write down comment.  #This line will be consider as comment in Python . To get input from the user “input” keyword is used. To store input value we will use variables to store inputs. In Python to define a variable there is no need to specify the data type or instantiate a variable.

In the above line of code you can see that we are asking user to give his first name and we are storing in the variable called firstName. And then displaying the user input stored in variable firstName. There are following rules you need to take care while defining variables in Python,

  • Variable name should not contain spaces
  • Variable name is case sensitive
  • Variable name cannot start with a number
  • To make code nice and clean it is recommended to use camelCasing or PascalCasing while defining variables.

There are many string functions available in Python. Few of them I mentioned below,

The output of the above code will be,


If you are using Visual Studio, you will observe that once you press dot after the variable name intellisense will give you list of string functions. You can try all of them and see the output.


Now lets look at how we can assign or use integers or float numbers in Python. Python is a very easy language. So if you are assigning a value to a variable without double quotes Python will consider this as a number.

The above code assign a integer value to a variable and then print that. Its fairly simple. You can perform math operations as well. Run the below code and observe the output,


In Python you can not concatenate number with a string using + symbol. Like print("A number is " + 50) will throw an exception “Can’t convert ‘int’ object to str implicitly”.  In the older version of python % symbol is used for formatting the numbers.  To concatenate number with string we can do it in following way print("Number %i" %50) .  Where as “i”  is representing that we are going to display an integer value. If you try to run the following line of code print("Number %i" %50.23457) , because we are accepting to concatenate the string with an integer value so the output will be Number 50. The numbers after decimal will be discarded. To display float value we need modify the code like this print("Number %f" %50.23457) . To display only two digits after the decimal we need to put .2  after % symbol, the code will be like this print("Number %.2f" %50.23457) .

In the newer version of Python 3, there is a very clean way to format numbers. format command is used curly braces to format numbers. Let’s look at the code below,

In the above code we assign integer value to a variable and then in the print statement we are using {0:d} to format the number and passing variable name to the format function. In the same way if we want to use float value the code will be like this  print("This is format function {0:f}".format(8.23457)) . To display the float value upto two decimal  print("This is format function {0:.2f}".format(8.23457)).

In the above string topic i show you that “input” keyword is used to get input from the user. Let’s write a program and get numbers from the user and add them. Write the below code and run the program,


Now observe the output of the program, I assign 100 and 50 and then add two variables, now the output should be 150 but it’s 10050. This is not a correct value. Whats wrong? The problem is that we are getting input and by default the input will be store as a string. So instead of adding two numbers Python concatenate them because the inputs are consider as string.  To fix the issue we need to modify our code,

In the above code I added “int” keyword, which will accept integer input from the user. Now if you run the program you will see the correct input. Now rerun the program and enter some float value in the first input and press enter. Let’s say we enter 10.2345 the moment we press enter the system will throw the following exception invalid literal for int() with base 10: ‘10.2354’.  This is because the program is accepting integer input value only. To accept float value we need to replace int with a float keyword.

In the above code, we are converting the user input values into float while adding them instead of converting or validating at user input level. To convert values from one data type to another following functions are used,

  1. int(value)  –  convert to an integer
  2. float(value)  –  convert to a float
  3. str(value) – convert to string


Tagged with: , ,