Learn Python Programming Language Basics – First Lesson

Python is deemed as the beginner friendly programming language on many forums so if you are want to start learning to program then Python is a good place to start .

First of all download the python package from python.org that will include its compiler IDLE that can be used to write python programs . Basically you can write a python program in notepad and save it with .py extension but we will be using IDLE that is provided with Python 3.5.2 bundle available at python.org.

Math Operators :

Once you have downloaded and installed python setup file open IDLE and a shell with open where you can enter python commands . As a first step lets just use this shell as a simple calculator . enter 2+2 in the shell and see the result. Also use multiplication( * ) ,division ( / )and subtraction ( – ) operators.

python1

As you can see mathematical operations are fairly simple in python so you should not have any problem , try using different combinations of math operators in a single line and hit Enter to execute . See the results.There are many other operators like Modulus ( % ) , Exponent (**)  that you can use in your program.

The order of operators is similar to that in mathematics , but it is a common practice to use brackets if you have multiple operations like ;

2-(3*1)+(6/3)

This will give you an answer of one which is same as 2 – 3 +2  or 2-3*1+6/3 .

Data Types :

As the name suggests data types are like different categories of data based on their type .For example  the number 1 is an integer . data type for any text will be a string .

We will be using different datatypes so you will get a better understanding about them and come to know that it is important to differentiate between different datatypes.

python1

In the above shown shell , you can see that when you use the text one  python gives an error because it is a string and hence must be enclosed in single quotes as ‘one ‘. Anything within single quotes will be considered as string even if it is a number as you can see from the last result in blue that it is enclosed in single quotes meaning it is a string.

String Concatenation:

Data operators when used with integers or floats act as simple mathematical functions .However if you use data operators with strings their meaning is changes . For Example 2 + 2 will give you 4 , However  ‘Apples’ + ‘Oranges’  will give you ‘ApplesOranges’ .

Exercise: Try using other data operators with different strings and see the results.

pythonStringConcatenation

Don’t forget to add single quotes whenever you are using a string . ‘Apples’ + 2  gives an error because here 2 is considered integer as there are no single quotes around it . Once you add single quotes around 2 then it is processed like a sting same as Oranges.

If you want to add space between Apples and Oranges then you can use something like following in the shell .

‘Apples’ + ‘   ‘  + ‘Oranges’

Look closely at the above line of code , it just using a empty space as a sting enclosed in single quotes . So basically the above line concatenates three strings .Alternatively you can just use space within first string like this : ‘Apples ‘  + ‘Oranges’ . Spaces within String quotes are treated as a space.

Variables :

Variables can have any value you assign them just like variables in mathematics where X and Y are usually used in algebra and different values are assigned to them .Each variable can be used to store single value in computer memory and these values are assigned using equal sign (also called assignment operator ) . For example if you want to create a variable x and assign a value 4 to it then you can do so in python simply by using x = 4 .Note that this variable has an Integer assigned to it so this variable should be treated as an integer . If you assign a string value to any variable as y = ‘Apples’  or y = ‘4’  then that variable (y) will be treated as a string .

>>> x = 4
>>> y = 4
>>> x + y
8
>>>x+1
5

Here we created two variables x and y with values 4 and 6 respectively . As we didn’t use single quotes around 4 and 6 so they are considered integers . and math operators can be used on variables x and y  just as they are used with integers. See that x+y gives 8 .

You can also use these variables for math operations with other integers as the values assigned to these variables are integers. Example , x+1 gives 5 .

If you assign any value to a variable and then in the next step you assign a different value to same variable then the first value will be discarded . For example;

>>> x = 4
>>> x
4
>>> x = 5
>>> x
5
>>>

Here we assigned value 4 to the integer and then value 5 . You can see that the current value of variable x is 5 and first value is replaced with second.

You can also assign Strings to variables and then that variable will be considered as a string.

> x = ‘Apples’
>>> x
‘Apples’

There are some rules regarding naming variables that should be followed or python will give you an error.

  1. Variable should be only one word (i.e no spaces)
  2. Only letters , numbers and underscore can be used.
  3. It should not begin with a number(First word can only be a alphabet or underscore)

Variable names and everything else in python is case sensitive , that means variable x is not equal to variable X . apples are not Apples.

Related Topics: Uncategorized