Python
Python
Introduction: -
1. Python is a general purpose high level language.
2. Developed by Guido Van Rossum in the late 1980’s
3. Presently administered by Python Software Foundation.
4. Easy to understand and learn for beginners.
5. It is highly useful (powers) some of its world’s most complex applications and
websites.
6. Google, New York Stock Exchange, Industrial Light & Magic, Intel, Cisco, HP, IBM,
NASA, and I-robot.
Feature of Python: -
1. Python is free and open-source software. Source code is available for the user.
Available under the GNO, General Public License, GPL.
2. Easy to learn and use.
3. Interpreter based language, hence gives immediate results.
4. Used for both scientific and commercial purpose.
5. Uses simple, clear, concise instructions that are easy to read even by non-
programmers, hence easy to maintain, debug, or enhance.
6. Codes are comparatively simpler, shorter, and less verbose (wordy-too many words)
compared to other HLLs likes C++, hence easy to learn because of its simple syntax
and shorter codes.
7. It works on Windows, Linux/Unix, Mac, OSX, and other operating systems, even on
small devices. It runs on microcontrollers, (single-purpose connect, integrated circuit
designed for a specific purpose) used in devices/ appliances, toys, remote control and
embedded devices like washing machine.
8. It can be easily integrated with C++, C, CON, ActiveX, COBRA, JAVA.
DOWNLOADING PYTHON: -
Steps
Open Google Chrome or Internet Explorer.
Type https://www.python.org/download/ in the address bar and press enter
The download page of python appears
Download Python 3.8.0
The setup of Python starts to download on our computer system in the download folder
INSTALLING PYTHON: -
Open the download folder of your computer system. Click on the setup of Python that you
download.
Install Now. Select ‘Install launcher for all users’ option and click on the option ‘Install
now’
The setup process will start.
Once the setup is complete, the following message will appear. Click on close.
We get the message ‘Setup was successful’. Click on ‘close’.
The version of Python may differ according to the program that is installed in our computer.
Similarly, the interpreter may be 32 bit or 64 bit, depending upon our computer.
2. Python (IDLE) – Integrated Development and Learning Environment – is the
graphical IDE for Python.
Also called Script mode of python.
We can edit, run (execute), Browse and debug Python programs from a single
interface, making it easy to write programs.
Steps-
StartAll app Python 2.7 IDLE
Python shell appears, displaying the Python prompt.
Creating Simple Python Programs: -
>>> (Python prompt)
1. Enter a simple expression 3+2i.e.
>>> 3+2
5
>>> 3*2
6
Here single statement programs are executed by Python shell
2. Write a program to print, display “Hello, Python”
>>> Print (“Hello, Python”)
Hello, Python
>>> 5*2*12
120
To execute
Run Run module
If the code is written correctly, then the output is shown in the Python shell window.
To close a file
File Close
To open a file
File Open (a dialog box opens) locate the folder type in the name Open.
Python will open the program in Edit/Script window.
To exit the environment (Python)
File Exit
Save it
Run it to get the required output
2. Data Types – tell the compiler what kind of value a variable can hold.
The data types are Number, string and list.
(Numbers and string will be discussed in this chapter).
Numbers: When we enter a number, Python will interpret it as a number. We are not
(user) required to declare the data type while entering numbers.
Number
types of numbers.
Integers – are whole numbers that are either positive, zero or negative. They do not carry
fractional or decimal (significant) part. E.g. (-2,0,2)
Program>>>newnum = 10
>>>print newnum
10
Floating data type – means number with decimal point. E.g. 0.7, 1.2 etc
Program >>>newfloat = 99.5 or 99.0
>>> print newfloat
99.5
String data type–Continuous set of characters in between single quotes or double quotes.
>>>mystring = ‘Hello World!!!’
>>> print (mystring)
Hello World!!!
>>> mystring1 = “Hello Everyone”
>>> print mystring1
Hello Everyone
When we use double quotes then apostrophe can be included as character
3. Variable and assignment in Python: -
Assignment statement has the form variable name = expression/value
The expression on the RHS is first calculated and then the result is assigned to the variable.
e.g.
o Name = “India” # string type.
o a = b = c = 5.0 # multiple variables assigned same value in a single statement.
o a, b, c = 1, 5.0, “abc”
i.e. int a = 1
float b = 5.0
str c = “abc”
Note:
1. This execution produces no output. The purpose is to make association between a
variable and its value.
2. Print the output we must print it with print statement or print function
Program >>> = 2+2
>>> print ()
4
Program>>> = 380.5
>>> print ()
380.5
>>> = 2*
>>> print (x)
>>> 380.5
>>> print ()
761
In computer programming = +1 means add 1 to the present value of make reassign the
result of . That is increment of by 1.
Program>>> = 10
>>> = +1
>>> print ()
11
Rules for naming variables.: -
Variable names can contain a to z, A to Z, 0 to 9 and _ (underscore)
Variable name should not begin with a digit.
Variable names are case sensitive. It is recommended to use small letters.
Keywords/reserve words cannot be used as variable names e.g.: if, else, print, while
etc.
Variable names can be of any length but cannot include blank spaces.