The document provides a comprehensive guide on installing and using Python, including how to download it, use the interactive mode, and write programs in script mode. It covers key concepts such as variables, data types, and the print() and input() functions, along with examples of valid variable names and how to perform basic arithmetic operations. Additionally, it explains how to create, save, and run Python scripts using IDLE.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views5 pages
python
The document provides a comprehensive guide on installing and using Python, including how to download it, use the interactive mode, and write programs in script mode. It covers key concepts such as variables, data types, and the print() and input() functions, along with examples of valid variable names and how to perform basic arithmetic operations. Additionally, it explains how to create, save, and run Python scripts using IDLE.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Login’
Python Annexure ®
Address
Kips Learning Pvt. Ltd
C-12, Block C, Sector 3, Noida 201301, Uttar Pradesh
E-mail: [email protected] Website: www.kips.in“‘Introduction!toyPython
To download Python, visit the site: www.python.org/downloads and click on the Download Python
button under ‘Download the latest version for Windows’. You will get an .exe file. Double-click
on the .exe file. Click on Run or Install Now in the dialog box to start the installation.
Keep following the instructions to install Python.
{Ez Interactive mode
> Click on Start > Python 3.7 > IDLE (Python
3.7). It will open the Python Shell window
(interactive mode) where you will see the
Python prompt (>>>).
> Type the commands next to Python prompt
one-by-one as shown in figure given below,
and press Enter. Python will immediately give
the output for each of them.
oo Ss
Printing a message eae SP peapeiea TAS
The print( ) function is used to display the output of any command _|ryp="=!m", “on rights,
on the screen. It can also be used to print the specified messages. fmnwss
For example: ——EE a
Soprint (He oT ee re SA Oe DD Bae
Hello World
‘Youcanalso pass more than one argument
to the print() function, In such a case,
the arguments are separated by commas.
[E& Using IDLE as a calculator
To evaluate an arithmetic expression, it is not necessary to [8 Pen 5 aasssoize,
use the print( ) function. If you type an arithmetic expression |(fotei) oi winx
at the Python command prompt and press the Enter key, the urge "Y"098 Tr “rattan
interpreter automatically evaluates it and shows the result. |! <<
In this case, the interpreter acts as a simple calculator. The |’-* |,
expressions are evaluated using operators, like +, - *, /, |)
etc, The basic BODMAS rules are followed for performing [:2.,')°"”
calculations. pel
rat —
Tt Kies —————————————_- 3) ———_- ‘‘fz Variables
When you are working with the values in Python, you require some storage location to hold the
values for later use. Named storage locations in the computer memory, which are used to store
data, are called variables. A variable can store only one data value at a time. When a new value
is stored ina variable, its previous value gets overwritten.
Assigning values to variables
Values are assigned to variables using the assignment operator (~
use of the assignment operators:
LS
10 FVauelOmanignediotievamblen
b-20 # Value 20 is assigned to the variable b.
# Value 20 (30—10) is assigned to the variable a. So, now the value of the variable a is
20, and not 10.
‘The following table shows the
arc10
i Rules to write the variable names.
There are certain rules in Python that have to be followed to form valid variable names:
A variable name must start with an alphabet (capital or small) or an underscore (_).
A variable name can consist of letters, digits, and underscore. No other character is allowed.
A keyword cannot be used as a variable name.
A variable name can be of any length.
eeu
Variable names are cas
-sensitive (eg., Age and age are different variable names).
Examples of some valid variable names are:
Class, emp_code, totsalary, minvalue, bal_fee, age1980, aasr
Examples of some invalid variable names are:
code Variable name cannot start with a digit.
ABCaltd Special characters are not allowed in a variable name.
[82 Working in script mode
For writing lengthy programs in Python, the Script mode is used. Using this mode, you can
create and edit Python programs. In this mode, you can also save your files so that they can be
used later. The complete script is written in an editor.
R —.
Creating a module/script/program file
To start with the Script mode for creating a module or program, follow the given steps:
Open IDLE Python Shell, ie, the Interactive mode.
Click on File > New File in IDLE Python Shell
In the new window that opens, type the commands you want to save as a program.
Click on File > Save, and save the file with the name ‘progl.py’ where .py is the extension
for Python files.
vee
Running a module/script/program file
After the Python program file is created, you can run the program by following the given steps:
> Open IDLE Python Shell.
> Click on File > Open. ([f the file is already open, you can directly follow the next step).
> Click on Run > Run Module or press the F5 key.
> [twill execute all the commands.
> The output will be generated in the Python Shell window.
Data types
In Python, a data type represents the type of (Pyther
data stored in a variable. The data stored in im
memory can be of many types. (Cink) CHeay) Cae) GD) equa)
Python has standard data types: ype
In this chapter, we are going to study (Integer) (Complex) (Float) " |
flowingdictre Gh” Va ~~) = ana em
Integer(int): It contains positive or negative whole numbers (without fraction or decimal).
float: It is a real number with floating point representation. It is specified by a decimal point.
Some examples of floating point numbers are 3.14, ~48.6, 18.0, etc.
String(str): A string is a collection of one or more characters put in a single quote, double-quote,
or triple quote. In python there is no character data type, a character is a string of length one. Itis,
represented by str class. Examples of strings are ‘Hello’, 'Myname’, '218;, ‘Peace’, etc.
‘You can assign a multiline string to a variable by using three quotes (’””).
For example ‘A data type represents the type of data stored in a variable.
TR RR sme aay
Using input() function
‘The input() function is used to accept the value
for a variable from the user. To input integer [Fe-tsr set Deus eon inden He
and float values, you can use int() or float( ) Baa ae aes 3e09359112e, Jul
: it (ingen) } on vin
along with the input() function. [Type "help", "copyright", "credits" or “lic«
In Python, the input() function takes one string [>>> a=input ("enter the first value:™)
argument. This means that whatever value is |@nter the first value: 200
g s Sp> beinput ("enter the second value:
being entered by the user, it will be taken asa [2nter tne second value: 300
string argument. So
KipsObserve in the above program, when you add the two numbers, you do not get the sum. Rather
the numbers are joined together and are printed as a string.
When you try to multiply these two values, you get an error. This is because the strings values
cannot be multiplied together.
‘You can use the int() function, to convert the values to integers and use them for calculations.
Example: Write a program to add two numbers.
lPython 9.7.4 (tags/v3.7.476093591126, Jul 20)
js2 bit (inter) } on win3e
lzype "neip*, "copyright", “credits” or *nicense
p>> sine Cnput ("inter the first muRber?™}]
lenter the first number: 100
b> beint (input ("enter ‘the second nunber:"))
lenter the second number: 300
>> peintirme sua of the numbers {s:%, a¢b)
[rhe Sum of the numere is: ¢00
Using print() function
The print() function is used to print a message or value.
Using separators with print() function
Using (,) operator: When you use '' operator as a separator among the values, the values are
displayed with a space between them.
Using tab space (\t): When you use '\t' escape sequence as a separator among the values, the
values are displayed with a tab space between them.
Using newline character ('\n’): A newline character 'in' Python is used to end a line and start a
new line. In Python, the new line character can be used with the input() function and with the
print() function.
2 eee