Вы находитесь на странице: 1из 21

Python - Identifiers

Premier Embedded Training centre in the country


Python Identifiers
• A Python identifier is a name used to identify a variable,
function, class, module, or other object.
– Contains of letters, digits and underscores
– Must begin with a letter or an underscore
– Case sensitive
Examples:
• temp
• Num1
• Num_2
• _value
• _temp_1
Premier Embedded Training centre in the country
Naming conventions
• Class names start with an uppercase letter. All
other identifiers start with a lowercase letter.
• Starting an identifier with a single leading
underscore indicates that the identifier is private.
• If the identifier also ends with two trailing
underscores, the identifier is a language-defined
special name.

Premier Embedded Training centre in the country


Assigning Values to Variables
value_1 = 12
value_2 = 12.3
value_3 = hello
print (value_1)
print (value_2)
print (value_3)

Premier Embedded Training centre in the country


Multiple Assignment
• You can also assign to multiple names at the same time.
Exampe1:
>>> x, y = 2, 3
>>> x
2
>>> y
3
Example2:
>>> str ,str ,str = Hello , Is , Ba galore
>>>str1
Premier Embedded Training centre in the country
Multiple Assignment
• a,b,c,d = 1,2,3,4
• a,b,c,d=1,(2+3j),'Hello',3.44
a,b,c,d=1,(2+3j),'Hello',3.44
print (a+b)
print (a+b+c)
Output:
3+3j
6.44+3j
Premier Embedded Training centre in the country
Accessing Non-Existent Names
• If you try to a ess a a e efore it’s ee properly
created
y pla i g it o the left side of a assig e t , you’ll get a
error.
>>> y
Traceback (most recent call last):
File "<pyshell#16>", line 1, in -toplevely
NameError: a e y' is ot defi ed
>>> y = 3
>>> y
3
Premier Embedded Training centre in the country
Python - Keywords

Premier Embedded Training centre in the country


Reserved Words
• Reserved words are words that cannot be used
as identifiers (variables, functions, etc.),

Premier Embedded Training centre in the country


Comments
• Start o e ts ith # – the rest of line is ignored.

• Ca i lude a do u e tatio stri g as the first


line of any new function or class that you define.

• The de elop e t e iro e t, de ugger, a d other


tools use it: it s good style to i lude o e.

Premier Embedded Training centre in the country


Comments
Example1: #pri t Hello
pri t ISM
Example2:
a=1
b=2 Output:
c=3 a value is 1
print a alue is , a b value is 2
print alue is , b
#pri t alue is ,

Premier Embedded Training centre in the country


Lines and Indentation
• Blocks of code are denoted by line indentation

• No braces required to indicate

– Blocks of code

– Function definitions if True:


print "True
– Flow control
else:
print "False"
Premier Embedded Training centre in the country
Indentation Error
All statements within a block must be indented
the same amount
if True: if True:
print A s er print A s er
print "True print "True

else: else:
print A s er print A s er
print "False" print "False"

Premier Embedded Training centre in the country


Multi-Line Statements
• Statements in Python typically end with a new
line.
print 1+2+3 print 1+2+3+\
4+5+6 4+5+6

Output: Output:
6 21

Premier Embedded Training centre in the country


Quotation in Python
• To denote string literals python accepts
single ′, double " and triple ‴ quotes

Single = ord
Double = Se te e
Triple = Paragraph

Premier Embedded Training centre in the country


Example:
string1= Hello
Output:
string2= Hello World..!
Hello
string3= Hello World..!
Hello World..!
This is python
Hello World..!
Wel o e to ISM
This is python
print string1
Welcome to ISM
print string2
print string3

Premier Embedded Training centre in the country


Basic Data types(Immutable)
• Integers (default for numbers)
z = 5 / 2 # Answer is 2, integer division.
• Floats
x = 3.456
• Strings
si gle
se te e
paragraph

Premier Embedded Training centre in the country


Getting input value
Syntax:

• var_name = input()
Returns value as same data type.
• var_name = raw_input()
Returns value as string.

Premier Embedded Training centre in the country


Example 1:
a = input E ter the a alue:
b = input E ter the alue:
print The su is: , a+b

Output:
Enter the a value: 10
Enter the b value: 20
The sum is: 30

Premier Embedded Training centre in the country


Example 2:

a = raw_input E ter the a alue:


b = raw_input E ter the alue:
c = raw_input E ter the alue:
print The su is: , a+b+c
Output:
Enter the a value: 10
Enter the b value: 2.5
Enter the b value: ISM

The sum is: 102.5ISM


Premier Embedded Training centre in the country
Assignment:

• Get Name, Reg.No, fees as I/p and print as


Output.
• Get student name, roll no, marks for 5 subject
and print average.

Вам также может понравиться