1.RESERVED WORDS: Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.In Python, keywords are case sensitive (upper case letters are different from lower case letters). There are 33 keywords in Python 3.7. This number can vary slightly over the course of time. All the keywords except True
, False
and None
are in lowercase and they must be written as they are. The list of all the keywords is given below.
False | await | else | import | pass |
None | break | except | in | raise |
True | class | finally | is | return |
and | continue | for | lambda | try |
as | def | from | nonlocal | while |
assert | del | global | not | with |
async | elif | if | or | yield |
2.PYTHON IDENTIFIERS: An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.
Rules for writing identifiers :
- Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore
_
. Names likemyClass
,var_1
andprint_this_to_screen
, all are valid example. - An identifier cannot start with a digit.
1variable
is invalid, butvariable1
is a valid name. - Keywords cannot be used as identifiers. Eg: global = 1 is invalid
- Output
File "<interactive input>", line 1 global = 1 ^ SyntaxError: invalid syntax
- We cannot use special symbols like !, @, #, $, % etc. in our identifier.
a@ = 0
- Output
File "<interactive input>", line 1 a@ = 0 ^ SyntaxError: invalid syntax
- An identifier can be of any length.
3.LINES AND INDENTATIONS: Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example −
if True:
print "True"
else:
print "False"
However, the following block generates an error −
if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"
Thus, in Python all the continuous lines indented with same number of spaces would form a block.
4.INPUT OUTPUT FUNCTIONS:
Python
provides numerous built-in functions that are readily
available to us at the Python prompt.
Some of
the functions like input() and print() are
widely used for standard input and output operations respectively. Let us see
the output section first.
Python Output Using print() function
We use
the print() function to output data to the standard
output device (screen). An
example of its use is given below.
print('This sentence is
output to the screen')
Output
This sentence is output to the screen
Another
example is given below:
a = 5
print('The value of a
is', a)
Output
The value of a is 5
In the
second print() statement, we can notice
that space was added between the string and the value of variable a. This is by default, but we can
change it.
The
actual syntax of the print() function is:
print(*objects,
sep=' ', end='\n', file=sys.stdout, flush=False)
Here, objects is
the value(s) to be printed.
The sep separator
is used between the values. It defaults into a space character.
After all
values are printed, end is printed. It defaults
into a new line.
The file is
the object where the values are printed and its default value is sys.stdout (screen).
Here is an example to illustrate this.
print(1, 2, 3, 4)
print(1, 2, 3, 4, sep='*')
print(1, 2, 3, 4, sep='#', end='&')
Output
1 2 3 4
1*2*3*4
1#2#3#4&
Output formatting
Sometimes
we would like to format our output to make it look attractive. This can be done
by using the str.format() method. This method is
visible to any string object.
>>> x = 5; y = 10
>>> print('The value of x
is {} and y is {}'.format(x,y))
The value of x is 5 and y is 10
Here, the
curly braces {} are used as placeholders.
We can specify the order in which they are printed by using numbers (tuple
index).
print('I love {0} and
{1}'.format('bread','butter'))
print('I love {1} and
{0}'.format('bread','butter'))
Output
I love bread and butter
I love butter and bread
We can
even use keyword arguments to format the string.
>>> print('Hello {name},
{greeting}'.format(greeting
= 'Goodmorning', name = 'John'))
Hello John, Goodmorning
We can
also format strings like the old sprintf() style used in C
programming language. We use the % operator
to accomplish this.
>>> x = 12.3456789
>>> print('The value of x
is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x
is %3.4f' %x)
The value of x is 12.3457
Python Input
Up until
now, our programs were static. The value of variables was defined or hard coded
into the source code.
To allow
flexibility, we might want to take the input from the user. In Python, we have
the input() function to allow this. The syntax for input() is:
input([prompt])
where prompt is
the string we wish to display on the screen. It is optional.
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
Here, we
can see that the entered value 10 is a string, not a number.
To convert this into a number we can use int() or float() functions.
>>> int('10')
10
>>> float('10')
10.0
This same
operation can be performed using the eval() function. But eval takes
it further. It can evaluate even expressions, provided the input is a string
>>> int('2+3')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5
Comments
Post a Comment