1. Python Introduction

Python is a cross-platform programming langauge that run on mulitple OS like Windows, Mac OS X, Linux and Unix. Today's most Linux and Mac have Python preinstalled, but the version might be out-of-date. So, it is recommended to install the latest version of Python. In Linux/Unix, to launch the interpreter you need to type $ Python3x, where x refer to the version that you used, while in Windows, first you need to add C:\Python3x in the search path by using the command prompt and then type set path=%path%;c:\python3x, if you already set the path for windows then you can launch Python by typing Python3x from windows prompt shell.

Python Shell

Alright now that you already have Python installed on your system, you can start Python with interactive mode or standard mode which usually called script mode. The interactive mode is meant for experimenting your code one line or one expression at a time. Example

>>> 2 + 3
5
>>> (4 * 5) / 2
10.0
>>> (9 - 8) * (2 + 3)
5
>>> "Hello " + "World"
'Hello World'

In contrast, the standard mode is ideal for running your programs or script from start. I often alternate between these two modes. If the task is relatively easy, I write the code directly in the standard mode. But, if the code is more challenging, I spend more time in the interactive mode. That way we can perfect every line before moving to standard mode. Example 

print(2 + 3)
print((4 * 5) / 2) 
print((9 - 8) * (2 + 3))
print("Hello " + "World")

Interactive Mode

In next tutorials, the code will be written in standard mode, but let's take a look what we can do with interacitve mode. When you launched Python interpreter in interactive mode it will shown like this

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Here >>> symbol is the prompt shell. The interpreter can act as simple calculator with +, -, *, and / , you can type the expression and write the value.

>>> 3 + 4
7
>>> 4 * 3
12
>>> 34 / 3
11.333333333333334
>>> (8 * 7) / 3
18.666666666666668
>>> (8 + 6) * 4
56

Not just numbers, Python can also manipulate strings. Strings can be enclosed with single quotes '...' or double quotes "...".

>>> 'hello world'
'hello world'
>>> 'hello world'  # using single quotes
'hello world'
>>> 'yes isn\'t'   # use \' to escape single quotes
"yes isn't"
>>> "yes isn't"    # or use double quotes
"yes isn't"
>>> '"Learn python is great," she said.'
'"Learn python is great," she said.'
>>> "\"Learn pyhotn is great,\" she said." # use \" to escape double quotes
'"Learn pyhotn is great," she said.'
>>> '"yes isn\'t"'
'"yes isn\'t"'

notice the symbol #, that's a comment, it will not be interpreted by Python and it's very useful to give a brief description what the code does for specific lines, and it is recommended that give comments on your code so it wil make everyone easier to understand your code.

Thera are a lot of things that you can try with interactive mode, but as the code grows larger, you will switch to standard mode, because the interactive is suit for testing one line expression. I think that's a little introduction to stard Python, if you guys have questions and feedback leave comment below.

Subscribe to receive free email updates:

0 Response to "1. Python Introduction"

Post a Comment