0% found this document useful (0 votes)
1 views

Python Basic (3)

The document provides an overview of programming concepts, focusing on Python, including definitions of programs, programming languages, source code, and the role of language translators. It explains the differences between interpreters and compilers, execution modes in Python, character sets, tokens, and various types of operators. Additionally, it covers expressions, statements, and the input/output functions in Python.

Uploaded by

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

Python Basic (3)

The document provides an overview of programming concepts, focusing on Python, including definitions of programs, programming languages, source code, and the role of language translators. It explains the differences between interpreters and compilers, execution modes in Python, character sets, tokens, and various types of operators. Additionally, it covers expressions, statements, and the input/output functions in Python.

Uploaded by

kiran04051985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1: Program An ordered set of instructions to be executed by a computer to carry

out a specific task is called a program.

2: Programming language - The language used to specify this set of instructions to


the computer is called a programming language.

3: Source Code - A program written in a high-level language is called source


code.

4: Language translators like compilers and interpreters are needed to


translate the source code into machine language.
Note: Python uses an interpreter to convert its instructions into machine language.

5: Difference between Interpreter and Compiler


Interpreter Compiler
An interpreter processes the program a compiler translates the entire source
statements one by one, first translating code, as a whole, into the object code.
and then executing. This process is After scanning the whole program, it
continued until an error is encountered generates error messages, if any.
or the whole program is executed
successfully. In both the cases, program
execution will stop.

6: Working with Python


To write and run (execute) a Python program, we need to have a Python interpreter
installed on our computer or we can use any online Python interpreter.

In the above screen, the symbol >>> is the Python prompt, which indicates that the
interpreter is ready to take instructions.
7: Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode - Interactive mode allows execution of individual statement
instantaneously.
b) Script mode- Script mode allows us to write more than one instruction in a file
called Python source code file that can be executed.

8: Character set
A character set is a set of valid characters acceptable by a programming language
in scripting. the Python character set is a valid set of characters recognized by the
Python language. Python supports all ASCII / Unicode characters that include:
 Alphabets: All capital (A-Z) and small (a-z) alphabets.
 Digits: All digits 0-9.
 Special Symbols: Python supports all kind of special symbols like, ” ‘ l ; : ! ~
@#$%^`&*()_+–={}[]\.
 White Spaces: White spaces like tab space, blank space, newline, and
carriage return.
9: Tokens
A token is the smallest individual unit in a python program. All statements and
instructions in a program are built with tokens. The various tokens in python are :
 PYTHON KEYWORDS - Keywords are reserved words. Each keyword
has a specific meaning to the Python interpreter, and we can use a keyword
in our program only for the purpose for which it has been defined. As
Python is case sensitive. Example : int , float etc

 IDENTIFIERS - In programming languages, identifiers are names used to


identify a variable, function, or other entities in a program. The rules for
naming an identifier in Python are as follows:
 Take any combination of letters, Digits and underscore.
 It cannot starts with digit.
 It can be of any length.
 It should not be a keyword or reserved word.
 We cannot use special symbols like !, @, #, $, %, etc., in identifiers.

 OPERATORS - An operator is used to perform specific mathematical or


logical operation on values. The values that the operators work on are called
operands.
1: Arithmetic Operators- Python supports seven types of arithmetic operators.
Operator Description Example
1: Adds the two numeric values >>>2+3
5
+ 2: Used to concatenate two strings >>>”Hello”+”India”
HelloIndia
Subtracts the operand on >>>5-3
- 2
1: Multiplies the two values >>>2*3
* 6
2: Produces the n number of copies >>>”Hello”*2
of a string, n may be any integer. HelloHello
Divides the operand returns the >>>5/2
/ quotient 2.5
Divides the operand returns the >>>5%2
% remainder 1
Divides the operand returns the >>> 5//2
// quotient by removing the decimal 2
part. It is sometimes also called
integer division.
Performs exponential (power) >>>2**3
** calculation on operands. 8

2: Relational Operators
Relational operator compares the values of the operands on its either side and
determines the relationship among them. There are six types of relational operators
Ex:
Operator Description Operator Description
== Check two values are equal != Check two values are not
equal
> Check left side value greater < Check left side value smaller
than right side value than right side value
>= Check left side value greater <= Check left side value less than
or equal to the right side or equal to the right side
value value

3: Logical Operators
There are three logical operators supported by Python. These operators (and, or,
not) are to be written in lower case only. The logical operator evaluates to either
True or False based on the logical operands on either side.
Operator Description
and If both the operands are True, then >>> num1 = 10
condition becomes True >>> num2 = -20
>>> bool(num1 and num2)
True
or If any of the two operands are True, >>> True or True
then condition True
becomes True
not Used to reverse the logicalstate of its >>> num1 = 10
operand >>> bool(num1)
True
>>> not num1
>>> bool(num1)
False

4: Assignment Operators - Assignment operator assigns or changes the value of the


variable on its left.
Operator Description Example
== Assigns value from right-side >>> n1=2
operand
+= x += y is same as x = x + y >>> n1 = 10
>>> n2 = 2
>>> n1 += n2
>>> n1
12
>>> n2
2
-= x -= y is same as x = x - y >>> n1 = 10
>>> n2 = 2
>>> n1 -= n2
>>> n1
8
*= x *= y is same as x = x * y >>> n1 = 10
>>> n2 = 2
>>> n1 *= n2
>>> n1
20
/= x /= y is same as x = x / y >>> n1 = 10
>>> n2 = 2
>>> n1 /= n2
>>> n1
5
%= x %= y is same as x = x % y >>> n1 = 10
>>> n2 = 2
>>> n1 %= n2
>>> n1
0
//= x //= y is same as x = x // y >>> n1 = 11
>>> n2 = 2
>>> n1 //= n2
>>> n1
5
**= x **= y is same as x = x ** y >>> n1 = 10
>>> n2 = 2
>>> n1 **= n2
>>> n1
100

10. EXPRESSIONS - An expression is defined as a combination of constants,


variables, and operators. Some examples of valid expressions are given below.
(i) 100 (ii) 3.0 + 3.14

Precedence of Operators - Evaluation of the expression is based on


precedence of operators. It is used to define the order of evaluation of
operators. Used to follow the given order of evaluation( P E M D A S ) .

Q: How will the following expression be evaluated in Python?


15.0 / 4 + (8 + 3.0)
Solution:
= 15.0 / 4 + (8.0 + 3.0) #Step 1
= 15.0 / 4.0 + 11.0 #Step 2
= 3.75 + 11.0 #Step 3
= 14.75 #Step 4

11: STATEMENT - In Python, a statement is a unit of code that the Python


interpreter can execute.
Example:
>>> x = 4 #assignment statement
>>> cube = x ** 3 #assignment statement
>>> print (x, cube) #print statement

12: INPUT AND OUTPUT


In Python, we have the input() function for taking the user input. The input()
function prompts the user to enter data. It accepts all user input as string.
Syntax : input ([Prompt])
Prompt is the string we may like to display on the screen prior to taking the input,
and it is optional.
Example:
>>> fname = input("Enter your first name: ")
Enter your first name: Arnab

Note:
 int(): Used to convert string data type to integer.
 float():to convert string data type to a floating-point number.

You might also like