Python – Basic Concepts

What is Python ?

Python is a high-level programming language, with application in numerous areas, including web programming, scripting, scientific computing, and artificial intelligence.  Python language is very popular and used by organizations such as Google, NASA, CIA, and Disney.

There are mainly three major versions of Python, 1.x, 2.x, and 3.x. They are subdivided into several minor versions, such as 2.7 and 3.6. Both Python version 2.x and 3.x are used currently. In maximum cases of examples, we will use Python version 3.6.


Our First Program

Let’s start with Python by a simple program which prints your name:

# To print something in python just type
# print("") and then inside the quotes ("") type what you want to print
# For better understanding consider the below example 
# we are printing Rajesh Sinha

print("Rajesh Sinha");

# semicolons are not mandatory in python
# after typing the print statement hit enter for output

After typing the print statement just hit the enter button and the thing inside the quotes (” “) will be printed on the screen. In our case output will be:

>>>Rajesh Sinha

Note: In the above output >>> are the prompt symbol of python.

Some Simple Operations

Python has the ability to carry out calculations directly in Python console. Just enter a calculation in Python consle and it will output the answer. As example:

>>>5*6-3

27

>>>8+6-9*2/4

9.5

>>>2**3

8

# ** is used to calculate power

In order to define priorities we can use paratheses also:

>>>5*(6-3)

15

>>>8+((6-9)*(2/4))

6.5

Note: If we use a single slash (/) to divide numbers it will produce a floating point result. If we want integer result we have to use double slash (//). As example:

>>>8/2

4.0

>>>8//2

4

Note: In any calculation in python (for multiplication, subtraction, and addition), if one of the values is float then the ultimate result will also be in float. Because python implicitly converts every integer value to a float in calculations.


Strings in Python

In Python, if we want to use a text, then we have to use the text as a String. In python to create a String, we have to enter the text inside a pair of single or double quotation marks. As example:

>>>"Rajesh Sinha"

'Rajesh Sinha'

Some characters can’t be directly included in a String. As an example, double quotes can’t be directly included in a double quote String, or this will cause to end the String prematurely.

To insert characters like these, they must be escaped by placing a backslash (\) before them. The same rule is also applied to single quotes for single quoted Strings. As example:

# We will print Rajesh's Internship

# This is the wrong approach

>>>'Rajesh's Internship'

  File "<stdin>", line 1
    'Rajesh's Internship'
            ^
SyntaxError: invalid syntax

# If we write the command in the above way we will get the above error

# Now this is the correct approach

>>>'Rajesh\'s Internship'

"Rajesh's Internship"

# We will get our desired output

Taking input from the user in Python

To take input from the user we use input command in python:

>>> input("Enter Something : ")

Enter Something : Rajesh

'Rajesh'

Now if we want to store the user input then we just have to assign the input to a variable as:

>>> a = input("Enter Something : ")

Enter Something : Rajesh

>>>print(a)

Rajesh

Note: Whenever we take input from a user, by default the input value captured by Python as a String. Now if we want that input value in any other format then we have to typecast it accordingly. As example:

# Taking input from user

>>> a = input("Enter Something: ")

Enter Something: 5

# Checking type of input

>>> type(a)

<class 'str'>

# That means by default the input value is of String type
# We are typecasting the input to integer

>>> b=int(input("Enter Something: "))

Enter Something: 5

# Again checking the type of the input value

>>> type(b)

<class 'int'>

# Note that now the type is Integer type
# Now we are typecasting to float type

>>> c=float(input("Enter Something: "))

Enter Something: 5

# Checking the type of the input value

>>> type(c)

<class 'float'>

#Note that now input value is of float type

Variables in Python

Variables play a very important role in most of the programming languages and Python is one of them. A variable allows us to store a value by assigning it to a name, which can be referred to use the value in future. In Python to assign a value to a variable, just use one equal sign. As example:

# Assigning the value 10 to the variable i

>>> i=10

# To see the value we have to print the variable

>>> print(i)

10

# We can do some mathematical operations also inside the print statement
# with the variable

>>>print(i//5)

2

# Though it will not change the value held by the variable

>>> print(i)

10

In python, while declaring a variable, we don’t have to specify the data type for that variable. Python will automatically typecast the variable according to the value stored in it. For this feature, we can reassign a variable as many time we want with a different type of data. Everytime Python will change the type of the variable as per the value will be stored. As example:

# Storing integer value in k

>>> k=10

# Checking type of k

>>>type(k)

<class 'int'>

# Now assigning float value in k

>>> k=5.5

# Again checking the type of k

>>> type(k)

<class 'float'>

# Again assigning String value in k

>>> k="Rajesh"

# Again checking the type of k

>>> type(k)

<class 'str'>

There are certain restrictions in Python with variable names. The rules are:

  • Variable names can only contain letters, numbers, and underscore.
  • Variable names can’t start with a number.
  • Spaces are not allowed in a variable name.
  • Python is a case-sensitive language, so in Python ‘r’ and ‘R’ are two different variables.
  • We can’t use special characters (like #,@,$,&) in any variable names.
  • Reserved words in Python (like def, if, str, int, float, and, or etc.) can’t be used as variable names.

This was all about the basic concepts of Python.

Leave a Comment

Your email address will not be published. Required fields are marked *