0% found this document useful (0 votes)
3 views8 pages

Python 2

The document explains how to execute Python scripts using the interpreter and discusses the advantages of using scripts over interactive mode for larger code. It covers various data types in Python, including integers, floats, booleans, strings, and lists, along with examples and rules for variable naming and assignment. Additionally, it highlights the flexibility of Python variables, which do not require explicit declaration and can change types dynamically.

Uploaded by

mpari0181
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Python 2

The document explains how to execute Python scripts using the interpreter and discusses the advantages of using scripts over interactive mode for larger code. It covers various data types in Python, including integers, floats, booleans, strings, and lists, along with examples and rules for variable naming and assignment. Additionally, it highlights the flexibility of Python variables, which do not require explicit declaration and can change types dynamically.

Uploaded by

mpari0181
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Alternatively,programmerscanstorePythonscriptsourcecodeinafilewith the.

pyextension, and
use the interpreter to execute the contents of the file. To execute the script by the interpreter,
you have to tell the interpreter the name of the file. For example, if you have a script
[Link] you're working on Unix, to run the script you have to type:

[Link]

Working with the interactive mode is better when Python programmers deal with small
pieces of code as you can type and execute them immediately,but when the code is more
than 2-4 lines, using the script for coding can help to modify and use the code in future.

Example:

Datatypes:

The data stored in memory can be of many types. For example, a student roll number is
stored asanumericvalueand his orher addressisstored [Link] has
various standard data types that are used to define the operations possible on them and the
storage method for each of them.

Int:

Int,orinteger,isawholenumber,positiveornegative, withoutdecimals,ofunlimited
length.

>>>print(24656354687654+2)
24656354687656
>>>print(20)
20
>>>print(0b10)
2
>>>print(0B10)
2
>>>print(0X20)
32
>>>20
20
>>>0b10
2
>>>a=10
>>>print(a)
10
#Toverify thetypeofanyobjectinPython,usethetype() function:

>>>type(10)
<class'int'>
>>>a=11
>>>print(type(a))
<class'int'>
Float:

Float, or "floating point number" is a number, positive or negative, containing one or more
decimals.

Floatcanalsobescientificnumberswithan"e"toindicatethepowerof10.

>>>y=2.8
>>>y
2.8
>>>y=2.8
>>>print(type(y))
<class'float'>
>>> type(.4)
<class'float'>
>>>2.
2.0
Example:
x=35e3
y=12E4
z =-87.7e100

print(type(x))
print(type(y))
print(type(z))

Output:

<class'float'>
<class'float'>
<class'float'>

Boolean:

ObjectsofBooleantypemayhaveoneoftwovalues,TrueorFalse:

>>>type(True)

<class'bool'>

>>>type(False)

<class'bool'>

String:

1. Strings in Python are identified as a contiguous set of characters represented in the


quotation marks. Python allows for either pairs of single or double quotes.

• 'hello'isthesameas"hello".

• [Link]:print("hello").

>>>print("mrcetcollege")

mrcet college

>>>type("mrcetcollege")

<class'str'>
>>>print('mrcetcollege')

mrcet college

>>>""

''

If you want to include either type of quote character within the string, the simplest way is to
delimit the string with the other type. If a string is to contain a single quote, delimit it with
double quotes and vice versa:

>>>print("mrcetisanautonomous(')college")

mrcet is an autonomous (') college

>>>print('mrcetisanautonomous(")college')

mrcet is an autonomous (") college

Suppressing Special Character:

Specifying a backslash (\) in front of the quote character in a string “escapes” it and causes
Python to suppress its usual special meaning. It is then interpreted simply as a literal single
quote character:

>>>print("mrcetisanautonomous(\')college") mrcet is

an autonomous (') college

>>>print('mrcetisanautonomous(\")college')

mrcet is an autonomous (") college

ThefollowingisatableofescapesequenceswhichcausePythontosuppresstheusual special
interpretation of a character in a string:

>>>print('a\
....b')
a. . .b
>>>print('a\
b\
c')
abc
>>>print('a\nb') a
b
>>>print("mrcet\ncollege") mrcet
college

Escape Usual Interpretation of


Sequence Character(s)AfterBackslash “Escaped”Interpretation
\' Terminates stringwith single quoteopeningdelimiter Literalsinglequote(')character
\" Terminatesstringwith doublequote openingdelimiter Literaldoublequote(") character
\newline Terminatesinputline Newlineisignored
\\ Introducesescape sequence Literalbackslash(\) character

InPython(andalmostallothercommoncomputerlanguages),atabcharactercanbe specified by the


escape sequence \t:

>>>print("a\tb")
a b
List:

 Itisageneralpurposemostwidelyusedindatastructures
 Listisacollectionwhichisorderedandchangeableandallowsduplicatemembers. (Grow
and shrink as needed, sequence type, sortable).
 Tousealist,[Link] values with
commas.
 Wecanconstruct/createlistinmanyways. Ex:
>>>list1=[1,2,3,'A','B',7,8,[10,11]]
>>>print(list1)
[1,2,3,'A','B',7, 8,[10, 11]]
>>>x=list()
>>>x
[]

>>>tuple1=(1,2,3,4)
>>>x=list(tuple1)
>>>x
[1,2,3,4]
Variables:

Variables are nothing but reserved memory locations to store values. This means that when
you create a variable you reserve some space in memory.

Based on the data type of a variable, the interpreter allocates memory and decides what can
be stored in the reserved memory. Therefore, by assigning different data types to variables,
you can store integers, decimals or characters in these variables.

RulesforPythonvariables:

• Avariablename muststartwithaletterortheunderscorecharacter

• Avariablenamecannotstartwithanumber

• Avariablenamecanonlycontainalpha-numericcharactersandunderscores(A-z,0-9, and _ )

• Variablenamesarecase-sensitive(age,AgeandAGEarethreedifferentvariables)

AssigningValuestoVariables:

Python variables do not need explicit declaration to reserve memory space. The declaration
happens automatically when you assign a value to a variable. The equal sign (=) is used to
assign values to variables.

The operand to the left of the = operator is the name of the variable and the operand to the
right of the = operator is the value stored in the variable.
Forexample−

a= 100 #Anintegerassignment

b= 1000.0 # Afloating point

c=

"John" #Astring print (a)

Thisproducesthefollowingresult−
100

1000.0

John

MultipleAssignment:

Pythonallowsyoutoassignasinglevaluetoseveralvariablessim

ultaneously. For example :

a= b =c=1

Here, an integer object is created with the value 1, and all three variables
are assigned to the same memory location. You can also assign multiple
objects to multiple variables.

Forexample−

a,b,c =1,2,"mrcet“

Here, two integer objects with values 1 and 2 are assigned to variables a
and b respectively, and one string object with the value "john" is assigned
to the variable c.

OutputVariables:

ThePythonprintstatementisoftenusedtooutputvariables.

Variables do not need to be declared with anyparticular type and can


even change type after they have been set.

You might also like