Showing posts with label Python: basic fundamental. Show all posts
Showing posts with label Python: basic fundamental. Show all posts

Python: Basic Fundamental

 Under the title 'Fundamental of Python' we will discuss two basics of Python program. These are the building blocks of Python program.

1. Tokens and     2. Comments 

1. Tokens: The smallest individual unit in a program is referred as 'Tokens'. It can be categorised as:



Identifiers: Identifiers are the basic building blocks of a program. These are the names of the different elements in a program such as variables. These elements have specific properties that help in making a program.

There are some rules to create identifiers in Python. These are

i. These may be the combination of alphabet in lowercase (a-z), uppercase (A-Z), digits (0-9) and underscore (_).

ii. The first character of an identifier must be a letter or an underscore, it must not be a digit.

iii. It must not contain any special character (like $ % ^ & *) except  underscore (_).

iv. It must not be a 'keyword'.

Some examples of valid identifiers are: hello, _hello, Hello, _123, a123, _Hello etc.

Here are some examples of invalid identifiers: 1Hello, @hello, return

Can you identify the reason, why these are invalid identifiers. Match you answer to click on answer button below:


 

Keywords: Keywords are the reserved words that have a special meaning or function in a program. These words are used while writing a program to get the desired output. The keywords are executed as per their purpose/action that has already been defined inside the language interpreter.
Keywords cannot be used as variable names, because by doing so, it will  try to change the meaning of the keyword, which is not allowed. All keywords are written in lower case and there are 35 keywords used in Python. These are:

false

return

else

lambda

yield

from

def

none

import

is

with

break

or

if

true

elif

while

assert

for

continue

raise

and

in

as

finally

not

global

async

del

try

except

nonlocal

class

pass

await


Literals: Literals are data items that never change their value during the execution of program. These are commonly termed as Literals. In Python there are four types of literals. These are:
  • Integer Literals: Integer Literals are whole number without any fractional part. It may be positive or negative. For example: 5, 15, -150 etc
  • Character Literals: Character Literals are single characters that are enclosed in single quotes. For example: 'x', '9' etc
  • Floating Literals: Floating Literals are fractional numbers. These may be positive or negative. For example: 34.876, 9.8766, -1.344 etc
  • String Literals: A string literal is a sequence of characters enclosed within single or double quotes. For example: 'Hello', " Welcome to Python" etc.
Punctuators: Punctuators are used as marks or as separators in a program. These are the punctuators "@ # , ; ' : / {} [] () etc.

Operators: Operators are tokens that does calculation with designated or given values in an expression. Operators when applied with operands form an expression. 
Operators can be classified as Arithmetic, Relational, Assignment, Logical, Bitwise etc.
Arithmetic Operators: Arithmetic operators are used for arithmetic or mathematical calculations. In Python, the arithmetic operators are:

Operator

Name of Operator

Purpose

Example I

Example II

+

Addition

Sum of two operands (a+b). Concatenation or joining of two string operands.

>>>6+9

Output: 15

>>>’Hello’ + ‘World’

Output: HelloWorld

-

Subtraction

Difference between the two operands (a-b).

>>>7-4

Output: 3

>>>4-7

Output: -3

*

Multiplication

Product of two operands (a*b) or print the same string as given number of times.

>>>5*6

Output: 30

>>>’Hello’*3       Output: HelloHelloHello

/

Division

Quotient of two operands (a/b)

>>>5/4       Output: 1

>>>13/3               Output: 4.33333

//

Floor Division

Quotient of the two operands without fractional part (a//b)

>>>5//4       Output: 1

>>>4.3//3    Output: 1.0

%

Modulus

Integer Remainder: after division of ‘a’ by ‘b’ (a%b)

>>>5%4   Output: 1

>>>7%2            Output: 1

**

Exponent

Product of ‘a’ by itself ‘b’ times (a to the power of b) (a**b)

>>>5**4  Output: 625

>>>4**.5  Output: 2.0


Relational Operators: Relational Operators are the operators used to compare two arithmetic or logical values. These are also known as 'Comparison Operators'. 

Name of Relational Operator

Symbol used in Python

Meaning

Example

Greater than

> 

True if left operand is greater than the right

x > y

Less than

< 

True if left operand is less than the right

y < x

Equal to

= =

True if left operand is equal to the right

x = = y

Not equal to

! =

True if left operand is not equal to the right

x ! = y

Greater than or equal to

> =

True if left operand is greater or equal to the right

x > = y

Less than or equal to

< =

True if left operand is less or equal to the right

y < = y


Logical operators: Logical Operators are used in the situation when two or more relational expressions are evaluated. Commonly three logical operators are used in Python: Logical 'and', Logical 'or' and Logical 'not'.

Name of Logical Operator

Denoted by

Example

Logical AND

and

a and b

Logical OR

or

a or b

Logical NOT

not

not a


Exrecise:
1. What is the smallest individual unit in a program called?
2. Which of the following operators are used for mathematical calculations.
a. Arithmetic            b. Relational
c. Loghical               d. None of these
3. Which among the following is a logical operator commonly used in Python?
a. Logical AND        b. Logical NAND
c. Logical XOR        d. Both a. and b.    
4. Greater than or equal to is a type of ____________ operator.
a. Logical                b. Relational
c. Arithmetic           d. None of these
5. What will be the output of this operation: 7%3.
a. 0                            b. 6
c. 1                            d. 2
6. Which among the following operators are used when there is a situation for evaluating two or more relational expressions?
a. Relational            b. Logical
c. Arithmetic           d. Mathematical
 

Python: Functions