Python is an interpreted, high-level and general purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace.


Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high-level dynamic data types, and dynamic typing. There are interfaces to
many system calls and libraries, as well as to various windowing systems. New built-in modules are easily written in C or C++ (or other languages, depending on the chosen
implementation). Python is also usable as an extension language for applications written in other languages that need easy-to-use scripting or automation interfaces.


Downloading Python:
https://www.python.org/downloads/

Numbers:
The interpreter acts as a simple calculator: you can type an expression at it, and it will write the value. Expression syntax is straightforward: the operators +, -, * and / work just like in most other languages (for example, Pascal or C); parentheses (()) can be used for grouping.
Example:

>>>4 + 5
9
>>>100 – 5*7

65

>>>100 – 5*7/ 5
13.0


>>>9 / 5 # division always returns a floating point number
1.8
Division (/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %:
>>>20 / 3 # classic division returns a float
6.666666666666667
>>>20// 3 # floor division discards the fractional part
6


The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:
>>>width = 10
>>>height = 4 * 8
>>>width * height
320

Strings:
Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes (‘…’) or double quotes (“… .”) .\ can be used to escape


Example:

>>> ‘Hello World’ # single quotes

‘Hello World’

>>>’Haven\’t’ # use \’ to escape the single
“haven’t”
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>>’Hello’ ‘World’
‘Hello World’
Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:
>>>word = ‘Welcome’
>>>word[0] # character in position 0
‘W’
>>>word[5] # character in position 5
‘m’
The built in function len() returns the length of a string:
>>>s = ‘welcometopythonworld’
>>>len(s)
20

Lists:
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.


Example:

>>> squares = [1, 4, 9, 16, 25, 36, 49, 64]

>>>squares
[1, 4, 9, 16, 25, 36, 49, 64]


lists can be indexed and sliced:
>>>squares[0] # indexing returns the item
1
>>>squares[-1]
64
>>>squares[-3:] # slicing returns a new list
[36, 49, 64]


You can also add new items at the end of the list, by using the append() method:

>>>squares.append (81) # add the cube of 6

>>>squares.append (10 ** 2) # and the cube of 7
squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Video Session Module:

Leave a comment

Trending