By Yoav D | 7/27/2016 | Python

Introduction To Python

Pyhton is a great programming language, used today in many areas like:

  • Web development
  • Scientific 
  • User applications
  • System and administrative scripts
  • many more

introduction to python

A few things we should know first:

There are currently two main versions of Python – 2.7 & 3.5. The former is compatible with more environments and programs, while the latter has the newest features. Before we choose which version to install, we must keep in mind that version 3.5 is not backwards compatible and therefore won’t work with older software.

 

Installation:

First, we go to the download page, choose the OS which we are planning to install Python on, and then the download of the installation file will begin.  

 

Using the Shell - Basic actions & commands:

If order to display an output on the shell’s screen, we’ll use the ‘print’ command:

In version 2.7 it will be like this:

>>> print ‘YourText’

In version 3.5 it will be like this:

>>> print (‘YourText’)

‘print’ is the only command which has a different writing syntax between the two versions (The rest of the commands are the same).

If we want to repeat the last command that was used during the current session, we will simply hit the ↑ button once on the keyboard, and then press the ‘Enter’ button.

* We can also hit the ↑ button twice or more to repeat previously used commands/functions. Just hit the ↑ button a few times, until you see the desired command/function. The order in which the shell will display the previously used commands to us is from the latest to the earliest.

If we want to define a variable, we will write it like this:

>>> my_variable = 1234

In this case I named the variable as “my_variable” and gave it the numeric value of 1234, but you can change those according to your needs.

You can also give your variables a string value (A value with letters), but in order to do so you’ll have to put quotation marks before and after the desired value.

It will be like this:

>>> my_workplace = “DiscoverSDK”

You can also give a variable the value of another variable. It is done like this:

>>> my_variable = my_workplace

* In this kind of use we will not put quotation marks like we did with the string value, because we are copying a value between two variables.

** Please note that the variable which you want to copy the value from has to be already defined.

 

We can also use more than a single variable with the ‘print’ function, for example:

 

>>> age = 24

>>> hair_style = “curly”

>>> print (“Hello, I am %d years old, and my hair is %s” % (age, hair_style))

And the output we will get is:

Hello, I am 24 years old, and my hair is curly

 

We can also use Python as a simple calculator. In order to do so, we simply type the math exercise and press the ‘Enter’ button right after that. It will be like this:

>>> 6.725 + 5 * 4 

And the output will be:

26.725

* Note that the calculator will work according to the order of operations in math, and not necessarily by the order which we wrote the numbers in.

 

Let’s take it even one step further. We can use the last result we got by typing _ as if it were a number. For this example, we will use the 26.725 we got before as the last result we got. The use of the _ function will be like this:

>>> 6.725 + 5 * 4
26.725
>>> _ * 5.67245

And the output we will get is:

151.59622625000003

 

If we want to use strings, we will do it like this:

>>> ”Hey there!” + “ “ + “How are you doing?”

And the output we will receive will be:
‘Hey there! How are you doing?’

We can also multiply the number of times the string will be displayed. In order to do so, we just add a * and a number right after it, which will determine how many times the phrase will be displayed. For example:

>>> “ABCD.” * 8

And the output will be:
ABCD.ABCD.ABCD.ABCD.ABCD.ABCD.ABCD.ABCD.

 

Another useful function Python has to offer is the ‘Multiline Statement’ function. In this function we are able to use more than one line for a single function. In order to do so, we will need to first define a variable with the value of three quotation marks. In the new empty line (which starts with three dots, instead of the regular three arrows), we write the desired phrase, and then hit the ‘Enter’ button. Now a new empty line will appear, starting with three dots as well (We can keep writing as many phrases as we want). When you are done, input three quotation marks and hit the ‘Enter button’. We will do it like this (For this example, I will use ‘print’ as well to show the result):

>>> music = “””
… Freddie mercury is the king of rock,
… and don’t let anyone tell you otherwise!
… Michael Jackson is the king of pop,
… and nobody dares to doubt that.
… “””
>>> print (music)

And the output will be:
Freddie mercury is the king of rock,
and don’t let anyone tell you otherwise!
Michael Jackson is the king of pop,
and nobody dares to doubt that.

* Note that unlike when using strings, we don’t need quotations marks to input the phrases themselves in this case.

 

One more function that can be useful is the using Python with Windows’ Command Prompt (CMD). In order to do so, we create a text file in our computer, write the commands/functions we desire and save the file with the extension of *.py (replace “*” with the name you want the file to be under). Now we open Windows’ Command Prompt and navigate to the *.py file’s location in our computer. To open it, we type the command “python” and the *.py file’s name (with the py file extension) right after it and hit the ‘Enter’ button. It will be like this:

python YourFilesName.py

 

 

 

 

 

 

Top 10 Python IDEs

  1. Pydev with Eclipse
  2. PyCharm
  3. Wing IDE
  4. Spyder Python
  5. Komodo IDE
  6. PTVS
  7. Eric Python
  8. Anaconda Python IDE (Sublime Text 3)
  9. Thonny
  10. Pyzo

 

 

 

 

 

 

Top 10 Python Libraries

  1. NumPy
  2. Scrapy
  3. wxPython
  4. Pillow
  5. SQLAlchemy
  6. pyglet
  7. Twisted
  8. Requests
  9. SciPy
  10. Matplotlib

 

 

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now