11computer Science-Computational Thinking & Getting Started With Python - Notes and Video Link
11computer Science-Computational Thinking & Getting Started With Python - Notes and Video Link
Computer Science
GENERALIZATION
It refers to identifying common or shared characteristics between two domains or
problem such that models or solutions of one could be adapted or applied to the other.
ALGORITHM
An algorithm is a sequence of steps that solves a problem by working on some input data
and producing a desired outcome. Algorithm design involves both creation and execution
of an algorithm. Algorithm Design is done by
1) What steps are needed to solve ?
2) How can the steps best be organized?
MEMORY MAP
Python is object oriented high level computer language developed by Guido Van
Rossum in 1991 . It was named after a British TV show namely ‘Monty Python’s Flying
circus’. As we all know that the computers understand the language of 0s and 1s which
is called machine language or low level language. However it is difficult for human to
write or comprehend instructions using 0s and 1s. This led to invent of high level
languages like python, C, C++, PHP, Java, Visual Basic that are easier to manage by
human but are not directly understood by the computer.
A program written in a high level language is called source
code. The software that converts the source code to object code is known as language
translators. There are three types of language translators: Compiler, Interpreter and
Assembler. Python uses interpreter to convert its instruction into machine code. An
interpreter processes the program statements one by one, first translating and then
executing. This process is continued until an error is encountered or the whole program
is executed successfully. On the contrary, a compiler translates the entire source code as
a whole, into the object code. After scanning the whole program, it generates error
messages, if any.
Features of Python:
To write and run a python program, we need to have a python interpreter installed on our
computer or we can use any online python interpreter. The interpreter is also called
python shell. A sample screen of python interpreter is shown below:
In the above screen, the symbol >>> is the python prompt, which indicates that the
interpreter is ready to take instructions. We can type commands or statements on this
prompt to execute them using a python interpreter.
Execution modes :
You can work in python in following ways:
1) In interactive mode
2) In script mode
Interactive modes do not save commands in form of a program and also output is
sandwiched between commands. It is suitable for testing codes. It allows us to interact
with operating system. Hear, when we type python statement, interpreter displays the
result immediately. That means, when we type expression/statement/command after the
prompt (>>>) , the python immediately responses with the output of it. Let see what will
happen when we type “welcome to python programming” after the prompt.
>>>print(“welcome to python programming”)
welcome to python programming
EXAMPLE:
>>> print 4+5
9
>>>x=10
>>>y=20
>>>print x*y
200
A sample code written in interactive mode is shown below:
Script mode is useful for creating programs and then run the program later and gets
computer output. In script mode, we type python program in a file and then use
interpreter to execute the content of the file. By default, the python scripts are saved in
the python installation folder with extension .py . To execute a script, we can either
1. Type the file name along with the path at the prompt . For example if the name of the
file is sample.py , we type sample.py . We can otherwise open the program directly
from IDE.
2. While working in the script mode, after saving the file, click [Run]->[Run Module]
from the menu.
Comparison between interactive mode and script mode :
Working in interactive mode is convenient for beginners and for testing small piece of
code, as one can test them immediately. But for coding of more than few lines, we should
always save our code so that it can be modified and reused. Python interactive interpreter
is also called Python shell.
Python, in interactive mode, is good enough to learn experiment or explore, but its only
drawback is that we cannot save the statements and have to retype all the statements once
again to be run again. A sample code of program written in script code is shown below:
You can also refer the video given below for interactive and script mode of python:
https://youtu.be/alvukR88UTw
A basic program in python
(1) In interactive mode
>>> print(“hello”)
(2) In script mode
FileNew
Print(“hello”)
Click Filesave with extension .py
Chooserun module
Output will be displayed as hello in a separate shell window
print() is a function in python.
Simple programs in python
PROGRAM :1 Write a program to input user name and print it.
First read the instructions:
You can write the program in interactive mode or in script mode.
Now see the code :
In interactive mode :
>>>name = input( “ What is your name ?”) #press enter key
What is your name ? | (cursor will blink here)
The value that you type in front of the displayed prompt will be
assigned to given variable, name in above case.
>>>print(name)
------------------- (entered value will be printed here)
Python will enclose the value of name in quotes while displaying it
because whatever value we enter through input( ) function it is treated
as a string.
The same program can be done in script mode also as per the
instructions given in the chapter.
PROGRAM:2 Write a program to input your age and print it.
In interactive mode :
>>> age = int (input (“ Enter your age”))
Enter your age |
>>>print(age)
The value that you type in front of the displayed prompt will be
assigned to the given variable age .
Here we have used int ( ) function to convert string to integer.
Now the value of age will not be displayed in quotes. It will be
displayed as integer.
NOTE
The input( ) function always returns a value in string type.
The int( ) function is used to convert string type to integer.