Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is Python?
Welcome to Python!
Python is a high-level programming language, with applications in numerous
areas, including web programming, scripting, scientific computing, and artificial
intelligence.
It is very popular and used by organizations such as Google, NASA, the CIA and
Disney.
The three major version of Python are 1.x, 2.x and 3.x. These are subdivided
into minor versions, such as 2.7 and 3.3.
Backwards compatible changes are only made between major versions; code
written in Python 3.x is guaranteed to work in all future versions. Both Python
Versions 2.x and 3.x are used currently.
>>> quit()
Simple Operations
1. Simple Operations can be done on console directly.
>>> 2 + 2
4
>>> 5 + 4 – 3
6
* : Multiplication / : Division
2. Use of parentheses for determining which operations are performed
first.
>>> 2 * (3 + 4)
14
>>> 11/0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
11/0
ZeroDivisionError: integer division or modulo by zero
In Python, the last line of an error message indicates the error’s type. In
further examples only last line of error is written.
Floats
Floats are used in Python to represent numbers that aren’t integers. Extra
zeros at the number’s end are ignored.
A float can be added to an integer, because Python silently converts the
integer to float. This conversion is the exception rather the rule in Python –
usually you have to convert values manually if you want to operate on them.
>>> 2**5
32
>>> 9** (1/2)
3.0
>>> 20 // 6
3
>>> 1.25 % 0.5
0.25
Strings
A String is created by entering text between two single or double quotation
marks.
Some characters can’t be included in string, they must be escaped by placing
backslash before them.
Manually writing “\n” can be avoided with writing the whole string between
three sets of quotes.
>>> print(1 + 1)
2
To get input from user, we can use input function. Which prompts user for i/p.
Returns what user enters as a string (with contents automatically escaped).
String Operations
Strings in python can be added using Concatenation using “+” operator.
>>> "Sumit" * 3
'SumitSumitSumit'
>>> 4 * '2'
'2222'
>>>"PythonIsFun" * 7.0
TypeError: can't multiply sequence by non-int of type 'float'
Type Conversion
You can’t add two strings containing numbers together to produce the integer
result. The solution is type conversion.
>>> int("2")+int("3")
5
Variables
A variable allows you to store a value by assigning it to a name, which can be
used to refer to the value later in the program.
To assign a variable, “=” is used.
>>> x=9
>>> print(2*x)
18
>>> x=123
>>> print(x)
123
>>> x='String'
>>> print(x+'!')
String!
For Variable names only characters that are allowed are letters, numbers and
underscores. Numbers are not allowed at the start. Spaces are not allowed in
variable name.
Invalid variable name generates error.
>>> 2ndNo = 22
SyntaxError: invalid syntax
You can also take the value of the variable from user input.
In-Place Operators
In-place operators allow you to write code like ’x = x + 3’ more concisely,
as ‘x +=3’. The same thing is possible with other operators such as - , * , / and
% as well.
>>> x=25
>>> x+=30
>>> print(x)
55
>>> x = "Learn"
>>> print(x)
Learn
>>> x+= "Python"
>>> print(x)
LearnPython
Many languages have special operators such as ‘++’ as a shortcut for ‘x+=1’.
Python does not have these.
Dissecting Programs
Comments
Single Line Comments
#This Single Line Comment
Multi-line Comments
'''
This is another type of comment
This is multi-line comment.
'''
print(‘Hello World!’)
You can also use this function to put a blank line on the screen; just call
print() with nothing in between the parentheses.
myName = input()
This function call evaluates to a string equal to the user’s text, and the previous
line of code assigns the myName variable to this string value.
You can think of the input() function call as an expression that evaluates to
whatever string the user typed in.
>>> len('hello')
5
>>> str(21)
'21'
>>> print('I am ' + str(21) + ' years old.')
I am 21 years old
The str(), int(), and float() functions will evaluate to the string, integer, and
floating-point forms of the value you pass, respectively.
>>> str(0)
'0'
>>> str(-3.14)
'-3.14'
>>> int('42')
42
>>> int('-99')
-99
>>> int(1.25)
1
>>> int(1.99)
1
>>> float('3.14')
3.14
>>> float(10)
10.0