Getting_Started_with_Python
Getting_Started_with_Python
Hopefully, all of you go through the previous uploaded materials. In this material we discuss about the introduction
of Python.
As all of you are the beginners ……….so go through the whole discussion very carefully……..try
to understand the matter………..and think two things when you try to write program 1) what to
do 2) how to do………….as “writing program” is not based on memorizing, is totally based on
clear understanding…………………..
Python programming language was developed by Guido Van Rossum in February 1991.
The programming language was named after famous BBC comedy show namely Monty Python’s Flying
Circus.
Not the fastest language – as python is an interpreted language so it not as faster like a compiled language
Lesser libraries than other language – Python offers library support for almost all computing programs,
but its library is still not competent with languages like C, Java, Perl etc.
Not strong on Type binding – Python interpreter is not very strong on ‘Type mismatch’ issues. for example
if you declare a variable as integer but later you store a string value in it. python won’t complain you.
Not easily convertible – Translating a Python programs into other programming language is not so easy.
Installing Python:
To install Python in your computer, you must first download the installation package from following website link:
https://www.python.org/downloads/
You can download any version like python 3.6, or python 3.7 or python 3.8…………….no problem
(Note: Just remember one thing…..if you are using windows 7 then to install python 3.6 or above it requires
service pack 1……so first install service pack 1 then install python……..for other operating system like
windows 8 or windows 10…it creates no problem)
After download the executable installer(32 bit / 64 bit as per your system)…………..then open the downloaded file
to install it on your computer.
To write and run our python programs interactively, we can use either command line window or the IDLE. IDLE is a
simple Integrated Developing Learning Environment that comes with Python installation.
Click on start menu on your computer then find the python apps (or click all programs – for windows 7) click
on IDLE option.
When you click on IDLE option you can see the following Python shell which looks like following:
Python shell – It is an interactive window where you can type the python code or commands and see the
output in the same window. It is an interface between Python commands and the operating system.
In the above figure, we type the “Welcome” in front of >>> symbol(called python prompt) and then press Enter.
As soon as we press the Enter key, the command gets executed immediately and the output is displayed then and
there in the next line.
In the above window i.e. on Python shell there is not the provision for holding or saving commands that we typed in
python prompt. All these commands, along with their output, get cleared as soon as we close the python shell
window.
This limitation can be overcome by working in python script mode. In Python script mode we can save all
commands we are typing.
Click on File menu then click on new file option on python shell………
When you click on New File option…..it opens a new window…where you can write the python commands:
After typing your commands then we save the file by clicking File menu then Save option
Then choose the location where you save your program then give the file name like example.py then click on save
button.
Now, to see the output of your program……you should run the program as following:
We can write our python programs either in interactive mode or in script mode.
[ You can run your python program by using another software named Anaconda Distribution. Anaconda distribution
provides the following tools that you can use to work in python. 1) Jupyter notebook – It is a web based, interactive
computing environment and 2) Spyder – It is a powerful Python IDE with many useful features. ]
Now, to write our own program, we have to know about two things, (i) How to display output in python and
(ii) How to take input from the user.
[Note: Don’t mix up the two terms – programmer and user…………..programmer are the persons who write
programs using some programming language and knows everything about the program.
whereas ….user are those persons who just use the program without any knowledge of programming…….]
print( ) – to print or display output in python, we have to use the pre-defined function print( ).
Example:
print(“HELLO World”)
HELLO World
(Note: remember one thing – python is a case sensitive language………so if your write PRINT( ) or Print( ) in
place of print( ), it will shows error. )
Program:
Output:
Program:
Output:
So, it is clear that print( ) is used to display output and every print( ) displaying output in separate line.
Now, if we want to display the output of the above program in same line then our program is:
Output:
Explanation:
on the above statement, end is used within print( ) function, it is known as argument or parameter of print( )
function.
So, end argument is used to specify the end character of every print statement.
On the above example I specified three spaces using end argument so it gives the output by separating “HELLO
WORLD” and “Welcome to python” with three spaces but in single line.
output is:
Means, if you not mention sep argument then words are separated by single space normally.
Now, if we write:
print( ) function is used for displaying output on screen in python and it supports two arguments (i) end and
(ii) sep.
So, after taking the input from the user we must store the value for further use. In this point the concept of variable
comes in program.
variable is a named storage location whose value will vary during the program execution.
if I write: x2
then ‘x’ is a variable here, and whatever value of x you specify every time, it will produce the output based on the
given value.
you can not use any reserved word of python as variable name.
you can not use any special symbol in the variable name except underscore( _ ).
you can use digits in variable names but not as the first charcter.
On the above statement whatever values given by the user that must be stored in a variable named as ‘n’.
Program Example: Take the name from the user as input and print his/her name with a welcome message.
Output:
Now, if we want to solve the following problem:
Program Example: take the user’s age then print it after adding 10 with the age.
Then if you run the above program, after entering the age and when you press Enter
key…………………………….it shows an error………………instead of showing 25
Think,………….. why?
The reason is input( ) function always takes string type values. string means combination of characters……..
we given 15 as age but 15 is a number for ourselves not for the computer. It treats 15 as a string value……..so
addition of string is not possible…………
we should convert the string type into numeric type at the time of taking input from the user.
if you want to accept decimal number from user then your code should be:
(I will discuss other data types in details in my other material………………….now, the concept of three data
types is enough to write our programs at the initial level………..
print(“HELLO World”)
both indicates string type value
OR
print(‘HELLO World’)
so, in Python string must be enclosed within single quote(‘ ’) or double quotes(“ ”).
Program Example: take the user’s age then print it after adding 10 with the age.
Solution)
Output:
Program Example: take the user’s age(in decimal points) then print it after adding 10 with the age.
Output:
If you want to display some message in output then you change your program as follows:
Output:
Some operators used in python(only for developing introductory programs……I will discuss all operators
later on)
+ - for addition
- - for subtraction
* - for multiplication
/ - for division
% - for remainder
= - for assignment
Now, try to solve some programs which I discussed in my previous materials (on flowcharts):
Flowchart:
Program:
Output:
Task: Write python programs: