Python Variables and Data types | Built in Operators

In this tutorial we will explain about Python variables and data types – Python variable is a location in memory to store the value you assign to that variable.

Separate declaration of variable is not required, Python internally takes care of variable declaration internally.

Python Variables

A variable is a location in memory used to store some value. Unique names are given to differentiate between different memory locations. A variable name can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).

In Python, we don’t have to declare a variable separately. Simply assign a value to a name and Python will take care of it. We don’t even have to declare type of the variable. Python handles it internally according to the value assigned to it.

Note– Choose your variable names according to intended use. final_result, counter, flag_check are some examples.

Variable assignment

We use assignment operator (=) to assign values to a variable. In Python, any type of value can be assigned in to a valid variable name.

final_result = 10
academy_name = 'RCV Academy'
floating_value = 10.5

Now, if you write print command to print value of variable, it will print the value assigned to that variable.

print(academy_name)

The output of the above statement will be

RCV Academy

Python will decide the type of variable according to the value assigned to it. For example, final_result will be an integer variable as integer value is assigned to it.

Python data types

Basic data type

We will discuss Python data types in two parts. First let’s look on some basic data types.

a. Numbers

Integers, Float and complex numbers falls under the category of Python numbers.

int_variable=10
float_variable=10.5
complex_variable=1+2j

print(type(int_variable))
print(type(float_variable))
print(type(complex_variable))

Here, we have defined three variables and assigned different type of numbers to them. To check that python has correctly interpreted these variables we can use a built-in function called type. It will show what kind of variable it is.

Output of the above code will be

<class 'int'>
<class 'float'>
<class 'complex'>

Note – Everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. We will discuss object-oriented programming in detail later.

b. Strings

A sequence of characters is called string. We can use single quote or double quote to represent strings.

string_variable_1="I am a string"
string_variable_2='I am also a string'
print(string_variable_1)
print(string_variable_2)

Output of the above code will be:

I am a string
I am also a string

Some built-in operators for strings

There are four main methods

  1. len() – returns the length of the string.
  2. lower() – replace all capital letters to lower letters.
  3. upper() – just opposite to above.
  4. str() – The str()method turns non-strings into strings.
test_string="rcvacademy"
test_integer=10
convert_to_capital=test_string.upper()
check_length=len(test_string)
convert_to_string=str(test_integer)

print(convert_to_capital)
print(check_length)
print(type(convert_to_string))

Output of the above code will be:

RCVACADEMY
10
<class 'str'>

c. Bool

This is a special data type provided by python. It only supports two values “True” and “False” as input.  This is useful to set some flag condition.

bool_variable=True
print(bool_variable)
bool_variable=False
print(bool_variable)

Output of the above code will be

True
False

Python Setup – Windows, MAC and Linux, Python Basics | First Python Script

This post explains about Python Setup on different OS like Windows, MAC & Linux. We will also learn about Python basics like commenting, PIP, PY charm and first Python script. Let us start step by step.

Python Setup on your machine

Python Setup on Windows

  1. Go to download python page on official site
    https://www.python.org/downloads/
  2. Click on Download python 3.6.4 (Version may change) and complete the download
  3. Double click on downloaded file. It will launch the python installer. Follow the instructions and you will see “The installation was successful” message when Python is successfully installed.

Python Setup on Mac OS

  1. Go to download python page on official site
    https://www.python.org/downloads/
  2. Click on Mac OS X below section “Looking for Python with a different OS? Python for Windows”
  3. Click on Mac OS X 64-bit/32-bit installer (Version may change) and complete the download
  4. Double click on downloaded file. It will launch the python installer. Follow the instructions and you will see “The installation was successful” message when Python is successfully installed.

Python Setup on Ubuntu

Python 3 is installed by default on modern versions of Ubuntu, so you should already have it installed. On command line run:

python3 -V

You should see current installed version of Python on your machine.

Introduction to PyCharm

By now you have successfully installed Python on your machine by following steps in previous section.

Now you need a good text editor to make and run your scripts. Windows installer of python comes with a default text editor called “IDLE”. You can surely use it but I personally prefer a text editor called PyCharm.

PyCharm can be used from making a small script to large professional application.

Go ahead, download and install PyCharm. We will soon use it to make our first script.

Basics of PIP command line

As a popular open source development project, Python has an active supporting community of contributors and users that also make their software available for other Python developers to use under open source license terms.

This allows Python users to share and collaborate effectively, benefiting from the solutions others have already created to common problems, as well as potentially contributing their own solutions to the common pool.

PIP is the preferred installer program. Starting with Python 3.4, it is included by default with the Python binary installers.

Open a command prompt or shell prompt depending on the OS you are using and type

pip -h

If python is installed correctly on your machine, then it should list all available commands for pip.

Let’s say you want to install Requests library to make http calls. Open command line and type:

pip install Requests

Successful completion of this command will install request library in your PC. You can now import and use it in your scripts.

Hands On

Culture of indentation

Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and stops.

name=input("Enter the academy name: ")

if name == 'rcvacademy':

print("Yes please")

else:

print("We are unable to find this academy")

We will discuss the syntax details later, here I just want to show you that unlike other languages python does not use brackets to mark the begin and end of a statement.

It uses “:” and indentation to show that we are in a code block.

These indentations will be automatically added by your python supported text editors. Advantage of this approach is, you will always get a well-structured code in the end.

How to comment in Python

It is a good practice to put comment in our program to make it more understandable.

Comments in the program are the statements which will not be executed. These are the pointers for the developer, that what a certain piece of code is supposed to do.

Python provides two types of commenting methods:

Single line comment in Python using hash (#)

print (“You are here”) #This is a comment

Multiline comment in Python

"""

This is

a

multiline comment

"""

Say hello to world (Make your first script)

OK let’s start with our first script.

Open PyCharm and click on File->New -> Then select python file -> Name your file like first_prog.py.

“py” extension indicates that it’s a python script.

Now write code as shown below and save it.

print ("Hello World")

Run this program by pressing the Run button. You should see Hello World on output prompt.

That’s it, this is all about Python basics and first Python program

Python Language Introduction – Importance and its Application

Introduction to Python Language

Python language is a widely used general-purpose, high-level programming language. It was developed by  Guido van Rossum at CWI in the Netherlands.

Python 1.0 was released in 1994. Since then language have seen many changes and improvement so far. Python is an open source language. It is driven by a great community of developers and testers from all over the worlds.

Here are few pointers about “Why you should learn python language”.

  • One of the major advantages of python is its syntax and readability. It has an intuitive syntax which makes user to focus on logic rather than tiny language details.
  • Python language supports all major Operating systems available, like Windows, MAC and Linux.
  • Python efficiently handles lower level operations like garbage collection and memory management by itself. You don’t have to worry about these things.
  • Python has a vast collection of standard libraries which speeds up the development process.
  • Python can seamlessly collaborate with other important languages Like C, Java. For example- Jython is a flavour of python language which can import a JAVA classes and use them.

Importance of Python Language in current technical space

Python is showing an ever-growing demand in many technical areas. Some important areas are as follows:

Web Development

Frameworks such as Django  and Flask made python very popular in the area of web development. Their ease of use and extensibility has made them a favourite among newbie’s as well as experienced professionals.

Scientific and Numeric

Python is a first choice for scientific calculations and data crunching. It has some superb libraries to perform these kinds of tasks.

  • SciPy is a collection of packages for mathematics, science, and engineering.
  • Pandas is a data analysis and modelling library.

Automation

Python is being used heavily in automation domain. Major automation frameworks like Selenium and Appium supports python. Python have some famous native automation tools also like robot

Education

Python’s elegant and simple syntax has made it very popular in the academic sector. Recent trend has shown that Python past Java as first choice of programming language.

Universities like MIT now teach programming in Python language.

https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/