Python idle
Python idle
• Guido van Rossum is the creator of the Python programming language, first released
in the early 1990s.
1
• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
• Python can be used to handle big data and perform complex mathematics.
• Python can be used for rapid prototyping, or for production-ready software development.
1.1.2 IDE
Types of IDEs
✓ IDLE
✓ Pycharm
✓ Jupyter
✓ Spyder
✓ Sublime Text
IDLE
2
• The IDLE tool can be used on Mac OS, Windows, and Linux
• Features of IDLE:
✓ Interactive interpreter with syntax highlighting, and error and i/o messages
• First, to create a Python program file, select New Window from the File menu in the
Python shell.
• Type the following in the program window exactly as shown
• Next save the program file by selecting Save As under the File menu, and save in the
appropriate folder with the name MyFirstProgram.py. and execute it by pressing F5.
Advantage:
Disadvantage:
PyCharm
3
• This IDE is suitable for professional developers and facilitates the development of large
Python projects
• Features of PyCharm:
✓ Features for debugging, profiling, remote development, testing the code, auto
code completion, quick fixing, error detection and tools of the database.
Advantage:
• Executes edits and debugs Python code without any external requirements
Disadvantage:
• The default setting may require adjustment before existing projects can be used.
Jupyter Notebook
• It is easy to use, interactive and allows live code sharing and visualization
• It is easy to use, interactive data science IDE across many programming languages
• It is not work as an editor, but also as an educational tool or presentation.
• Features of Jupyter:
4
✓ Supports for the numerical calculations and machine learning workflow
✓ Inter generation of data science libraries like NumPy, Pandas, and Matplotlib
✓ It is one of the best Python IDE that supports for Numerical simulation, data
cleaning machine learning data visualization, and statistical modeling.
✓ Combine code, text, and images.
✓ Support for many programming languages.
✓ Integrated data science libraries (matplotlib, NumPy, Pandas).
Advantage:
• It combines code, text, images, videos, mathematical equations, plots, maps, graphical
user interface and widgets to a single document.
• It allows users to convert the notebooks into other formats such as HTML and PDF.
• It saved in the structured text files (JSON format), which makes them easily shareable.
Disadvantage:
• Less Security
Spyder
• It is also called Scientific Python Development IDE and it is the most lightweight IDE for
Python
• Spyder comes with Anaconda distribution, which is popular for data science and machine
learning.
• It allows you to access PostgreSQL, Oracle, MySQL, SQL Server, and many other
databases from the IDE.
• Features of Spyder:
5
✓ Support for automatic code completion and splitting
Advantage:
• Community support
• Rich in development tool features
• Complete documentation
Disadvantage:
• Execution dependencies
• Optional dependencies
Sublime Text
✓ Discreet, minimal interface: we must be able to focus on the text and not a
myriad of toolbars;
6
✓ It is high quality and powerful IDE.
✓ It incorporates most of the features of a basic Python text editor, including
customizable syntax highlighting.
Advantage:
Disadvantage:
• License required
1.2 Literals
Python Literals
✓ Numeric literals
✓ String literals
✓ Boolean literals
✓ Special literals
• A numeric literal is a literal containing only the digits 0–9, an optional sign character
and a possible decimal point. (The letter e is also used in exponential notation).
• If a numeric literal contains a decimal point, then it denotes a floating-point value , or
“ float ” (e.g., 10.24); otherwise, it denotes an integer value (e.g., 10).
• They are immutable and there are three types of numeric literal :
✓ Integer
✓ Float
7
✓ Complex.
• Integer :
✓ Both positive and negative numbers including 0. There should not be any
fractional part.
✓ There is no limit to the size of an integer that can be represented in Python
• Float
✓ These are real numbers having both integer and fractional parts.
✓ Floating-point values, however, have both a limited range and a limited
precision . Python uses a double-precision standard format providing a
range of 10-308 to 10308 with 16 to 17 digits of precision.
• Complex Literal
✓ The numerals will be in the form of a+bj, where ‘a‘ is the real part and ‘b‘
is the complex part.
• Example:
#Float Literal
float_1 = 10.5
float_2 = 1.5e2
#Complex Literal
x = 3.14j
print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)
Output
10 100 200 300
10.5 150.0
3.14j 3.14 0.0
• Built-in format Function
>>>12/5 >>>5/7
2.4 0.7142857142857143
>>>format(12/5, '.2f') >>>format(5/7, '.2f')
8
'2.40' '0.71'
>>>format(13402.25, ' , .2f')
13,402.24
s = 'hello'
m = ''' hello
world'''
print(s)
print(t)
print(m)
• Character literal is also a type of string literals where a single character surrounded by
single or double-quotes.
• Example:
a = 'h'
b = "h"
print(a)
print(b)
• The Representation of Character Values
9
'B' is encoded as 01000010 (66)
'0' is encoded as 00110000 (48)
'1' is encoded as 00110001 (49).
✓ The ord func gives the UTF-8 (ASCII) encoding of a given char.
ord('A') is 65.
✓ The chr function gives the character for a given encoding value.
chr(65) is 'A'.
• String Formatting
✓ The format function can be used to control how strings are displayed.
▪ format(value, format_specifi er)
✓ For example:
Python Variables
number = 10
• created a variable named number. We have assigned the value 10 to the variable.
number = 10
number = 1.1
• Initially, the value of number was 10. Later, it was changed to 1.1.
10
>>>id(num) >>>id(k)
505494040 505494040
• The id function produces a unique number identifying a specific value (object) in
memory.
• The same variable can be associated with values of different type during program
execution, as indicated below.
var = 12 integer
var = 12.45 float
var = 'Hello' string
Variable Assignment and Keyboard Input
website = "apple.com"
print(website)
Output
apple.com
11
website = "apple.com"
print(website)
print(website)
Output
apple.com
program.com
a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)
• If we want to assign the same value to multiple variables at once, we can do this as:
x = y = z = "hai"
print (x)
print (y)
print (z)
Identifier
• An identifier is a sequence of one or more characters used to provide a name for a given
program element.
12
• A Python identifier is a name used to identify a variable, function, class, module or other
object.
• An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or
more letters, underscores and digits (0 to 9).
• Python is case sensitive , thus, Line is different from line.
• Identifiers may contain letters and digits, but cannot begin with a digit.
• The underscore character, _, is also allowed, but it should not be used as the first
character, however, as identifiers beginning with an underscore have special meaning in
Python.
• Spaces are not allowed as part of an identifier.
To display the keywords, type help() in the Python shell, and then type keywords (type 'q' to
quit).
• A simple way to check whether a given identifi er is a keyword in Python is given below,
>>>'exit' in dir(__builtins__)
True
13
• Sequential control is an implicit form of control in which instructions are executed in
the order that they are written.
• A program consisting of only sequential control is referred to as a “straight-line
program.”
• Selection control is provided by a control statement that selectively executes instructions
• Iterative control is provided by an iterative control statement that repeatedly executes
instructions.
If Statement
• An if statement is a selection control statement based on the value of a given Boolean
expression.
•
14
if a%2==0:
print("It is Even Number")
else:
print(“It is odd Number”)
✓ Output
Enter a Number78
It is Even Number
Indentation in Python
• Indentation is simply used to align program lines to aid readability.
• In Python, however, indentation is used to associate and group statements
• The set of statements following a header in Python is called a suite (commonly called a
block ).
• The statements of a given suite must all be indented the same amount.
• A header and its associated suite are together referred to as a clause .
Multi-Way Selection
Two ways of constructing multi-way selection in Python:
1. nested if statements
2. elif headers.
Nested if Statements
• When selection among more than two sets of statements (suites) is needed.
• For such situations, if statements can be nested, resulting in multi-way selection.
15
✓ Example
var = 100
if var < 200:
if var == 150:
print ("Which is 150")
elif var == 100:
print ("Which is 100")
elif var == 50:
print ("Which is 50")
elif var < 50:
print ("Value is less than 50")
else:
print ("Values more than 200..")
• In the first if statement, if variable grade is greater than or equal to 90, then 'Grade of A'
is displayed.
• Therefore, its else suite is not executed, containing the remaining if statements.
• If grade is less than 90, the else suite is executed.
• If grade is greater than or equal to 80, 'Grade of B' is displayed and the rest of the if
statements in its else suite are skipped, and so on.
• The final else clause is executed only if all the previous conditions fail, displaying 'Grade
of F'. This is referred to as a catch-all case.
elif Header
16
• The elif keyword is pythons way of saying "if the previous conditions were not true, then
try this condition".
• An else statement can be combined with an if statement.
• An else statement contains the block of code that executes if the conditional expression in
the if statement resolves to 0 or a FALSE value.
Syntax: Example:
if expression1: a=int(input('Enter a Number'))
statement(s) if a>0:
elif expression2: print(a," is Positive Number")
statement(s) elif a<0:
elif expression3: print(a," is Negative Number")
statement(s) else:
else: print(a," is Zero")
statement(s) ✓ Output
Enter a Number-67
-67 is Negative Number
While Statement
17
i=i+1
print(s)
• Example:
#print 1 to 5
i=1
while i < 6:
print(i)
i=i+1
✓ Output:
1
2
3
4
5
#print even number until 10
j=1
print("Even Numbers are")
while j<=10:
if j%2==0:
print(j)
j=j+1
✓ Output:
Even Numbers are
2
4
6
8
10
For
✓ The for loop in Python is used to iterate the statements or a part of the program several
times.
✓ It is frequently used to traverse the data structures like list, tuple, or dictionary.
✓ syntax
for iterating_var in sequence:
statement(s)
✓ Example
#print natural numbers
i=1
n=int(input("Enter the number up to print the natural numbers : "))
for i in range(0,n):
18
print(i,end=' ')
✓ Output
Enter the number up to print the natural numbers : 5
01234
✓ Example
#print String
s="Hello World"
print("\n")
for c in s:
print(c,end=' ')
✓ Output
Hello World
✓ Example
#Multiplication Table
i=1;
print("\n")
num = int(input("Enter a number:"));
for i in range(1,11):
print("%d X %d = %d"%(num,i,num*i));
✓ Output
Enter a number:4
4X1=4
4X2=8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36
4 X 10 = 40
range() Function
✓ To loop through a set of code a specified number of times, we can use
the range() function,
✓ The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
✓ The range() function defaults to increment the sequence by 1, however it is possible to
specify the increment value by adding a third parameter: range(2, 30, 3):
✓ Program
19
for x in range(6):
print(x)
20