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

Python Fundamental

Python, developed by Guido van Rossum, is an easy-to-learn programming language with a clear syntax and a broad standard library. It features various data types, tokens, and operators, allowing for flexible programming, including support for GUI applications. The document also discusses concepts like variables, expressions, statements, and debugging techniques in Python.

Uploaded by

yashparganiha.ai
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)
4 views8 pages

Python Fundamental

Python, developed by Guido van Rossum, is an easy-to-learn programming language with a clear syntax and a broad standard library. It features various data types, tokens, and operators, allowing for flexible programming, including support for GUI applications. The document also discusses concepts like variables, expressions, statements, and debugging techniques in Python.

Uploaded by

yashparganiha.ai
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
You are on page 1/ 8

1

History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.
Python Features
Python's features include −
 Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This
allows the student to pick up the language quickly.
 Easy-to-read − Python code is more clearly defined and visible to the eyes.
 Easy-to-maintain − Python's source code is fairly easy-to-maintain.
 A broad standard library − Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
 Interactive Mode − Python has support for an interactive mode which allows interactive testing and
debugging of snippets of code.
 Portable − Python can run on a wide variety of hardware platforms and has the same interface on
all platforms.
 Extendable − You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
 GUI Programming − Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X
Window system of Unix.

Tokens(Lexical Units): The smallest individual unit in a program is known as a token. Python has following
types of token:”-
1. Keywords
2. Identifiers
3. Literals
4. Punctuators
5. Operators
1. Keywords: Keywords are the words that convey a special meaning to the language compiler. These are
reserved for special purpose and must not be used as normal identifier names. For ex: byte, if, True, False
etc.

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. Identifiers: These are fundamental building blocks of a program and are used as the general terminology for
the names given to different parts of the program like variables, objects, classes, functions, arrays etc.
Identifier forming some rules related to C++:
a. It can have alphabets, digits and underscore and dollar sign character.
b. They must not be a keyword or Boolean literal or null literal.
c. They must not begin with a digit.
2

d. They can be of any length.

Some valid identifier Some invalid identifier


Myfile DATA-REC (Contains special character -)
MYFILE 29clt (First character is digit)
_chk Break (It is a keyword)
A_to _Z My.file (Contains special character .)
$jhk
3. Literals: These are often referred to as constants are the data item that is fixed data values. C++ allows
several kinds of literals:
a. Boolean literal: It is represented by Boolean value true or false.
b. Integer Literal: These are whole number without any fractional part. For ex: 12, 15, 20 etc.
C++ allows three types of integer constants:
i. Decimal(base 10)
ii. Octal(base 8)
iii. Hexadecimal(base16)
c. Character Literal: It must contain one character and must be enclosed in single quotation marks. Like
‘s’, ‘t’.
d. Floating-literal: These are also called real literals. These having fractional parts. Like 2.0, 17.5, -1.25
e. String Literal: It is a sequence of 0 or more characters surrounded by quotes. Like “abc” or ‘xyz’.
Size of string:

‘\\’ size is 1( \\ is an escape sequence to represent backlash)


‘abc’ size is 3
“\ab” size is 2( \a is an escape sequence, thus one character.)
“Seema\’s pen” size is 11(For typing apostrophe(‘) sing, escape sequence
\ ‘ has been used.)
“Amy’s” size is 4 (Python allows a single quote (without escape
sequence) in double quoted string and vice-versa)
Special Literal - None: None is used to indicate absence of value. It is also used to indicate the end of lists in
Python.
4. Punctuators: The following nine ASCII character are the separators or punctuators.
( ) { } [ ] ; , .
5. Operators: Total 37 tokens are the operators formed from ASCII characters. Like <,>,=,== etc

Operators: The operations are represented by operators and the objects of the operations are referred
to as operands.
1. Unary Operators: Operators that act on one operand are referred to as Unary Operators.

& Address operator


* Indirection operator
+ Unary plus
3

- Unary minus
~ Bitwise complement
++ Increment operator
-- Decrement operator
! Logical negation

Unary +: Ex: if a = 5 then +a means 5.


Unary -: Ex: if a = 5 then -a means -5.

2. Binary Operators:

Arithmetic Operators Shift Operators


+ Addition << Shift Left
- Subtraction >> Shift Right
* Multiplication
/ Division
% Reminder/Modulus
Bitwise Operator Logical Operator

& Bitwise AND and Logical AND


^ Bitwise exclusive OR(XOR) or Logical OR
| Bitwise OR
Assignment Operators Relational Operator
= Assignment
< Less than
+= Assign sum
> Greater than
-= Assign difference
<= Less than or equal to
*= Assign product
>= Greater than or equal to
/= Assign quotient
== Equal to
%= Assign remainder
!= Not equal to
<<= Assign left shift
>>= Assign right shift
&= Assign bitwise AND
^= Assign bitwise XOR
Identity Operators Membership Operators
Is Is the identity same? in Whether variable in sequence?
Is not Is the identity not same? not in Whether variable not in sequence?

Data types
are the classification or categorization of data items. It represents the kind of value that tells what
operations can be performed on a particular data. Following are the standard or built-in data type of
Python:
4

Numeric
In Python, numeric data type represent the data which has numeric value. Numeric value can be
integer, floating number or even complex numbers. These values are defined
as int, float and complex class in Python.
 Integers – This value is represented by int class. It contains positive or negative whole numbers
(without fraction or decimal). In Python there is no limit to how long an integer value can be.
 Float – This value is represented by float class. It is a real number with floating point representation.
It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative
integer may be appended to specify scientific notation.
 Complex Numbers – Complex number is represented by complex class. It is specified as (real part)
+ (imaginary part)j. For example – 2+3j

Sequence Type
In Python, sequence is the ordered collection of similar or different data types. Sequences allows to
store multiple values in an organized and efficient fashion. There are several sequence types in Python
 String
 List
 Tuple
1) String
In Python, Strings are arrays of bytes representing Unicode characters. A string is a collection of one or
more characters put in a single quote, double-quote or triple quote. In python there is no character data
type, a character is a string of length one.

Creating String
Strings in Python can be created using single quotes or double quotes or even triple quotes.
Boolean
Data type with one of the two built-in values, True or False. Boolean objects that are equal to True
are truthy (true), and those equal to False are falsy (false). But non-Boolean objects can be evaluated in
Boolean context as well and determined to be true or false. It is denoted by the class bool.

Barebnes of a Python Program:


1. Expressions: An expression is any legal combination of symbols that represents a value.
15
2.9
a+5
(3+5)/4
2. Statements: A statement is a programming instruction that does something i.e., some action takes places.
a=15
b=a-10
print(a+3)
if (b<5) :
5
.
.
.

3. Comments: These are the additional readable information to clarify the source code. This is not a part
of execution of the code.
a. Single line comment: This starts with # symbol.
Ex: # this is my first program of python.
# python is simpler than C++
b. Multiline comment: Comments enclosed in triple qotes (“ “ “) or tiple apostropje(‘ ‘ ‘) are called
docstrings .
Ex: ‘’’ this is my first program of python.
python is simpler than C++ ‘’’

4. Functions: A function is a code that has a name and it can b resued by specifying its name in the program
where needed.
Ex: SeeYou()

5. Blocks and indentation: Some times a group of statements is part of another statement or function. Such
a group of one or more statements is called block or code-block or suit.
Ex:

if(b<5):
print(“Value of b is less than 5”)
print(“Thank you”)
6

Variable and Assignments:

Variable: A variable represents named location that refers to a value and whose values can be used and
processed during program run.
70
Ex: marks=70 marks

Creating a Variable: Python creates the variable of the type similar to the type of value assigned.

Ex: marks=70 #numerical type


balance=2354.25 #float type
students=”Jacob” #string type

PYTHON VARIABLES ARE NOT CREATED LIKE CONTAINER LIKE ANOHTER LANGUAGE LIKE C, C++

A variable is not created until some value is assigned to it.


x=10;
print (x)

Lvalues and
Rvalues: lvalue: expression that can come on the lhs(left hand side) of an assignment.
rvalue: expression that can come on the rhs(right hand side) of an assignment.
We can say
a=10
But We cannot say
10=a

Multiple assignment:

1. Assigning same value to multiple variables


Ex: a=b=c=10

2. Assigning multiple values to multiple variables


Ex: a, b, c=10, 20, 30

Simple Input and Output:

Input function: The input() function always returns a value of String type.

Ex: name=input(“What is your name”)


Output: What is your name
Reading Numbers: String values cannot be used for arithmetic or other numeric operations. For these we
need to have value of numeric types(integers or float).

Ex: Age= int(input(“Enter Age=”))


Amount= float(input(“Enter Amount=”))

Output Through print() Statement:

Ex: print(“Python is wonderful”)


Output: Python is wonderful

Ex: print(“Sum of 2 and 3 is = ”,2+3)


Output: Sum of 2 and 3 is =5
7

Ex: a=5
print(“Double of “,a , “is” ,a*2)
Output: Double of 5 is 10

In python we can break any statement by putting a \ is the end and pressing Enter key, then completing
the statement in next line.

Ex: print(“Hello “, \
How are you”)

Python Operator Precedence Rule

In Python, operator precedence rules specify the order in which operators are evaluated in an expression. Python has a
well-defined set of operator precedence rules that determine the order in which arithmetic and logical operators are
evaluated. Operators with higher precedence are evaluated first, and those with lower precedence are evaluated later.

The following table lists Python’s operators in order of precedence, from highest to lowest:

Operator Description

() Parenthisis

** Exponentiation

+x, -x Positive, negative

*, /, //,% Multiplication, division, floor division,


modulo
+, – Addition, subtraction

<> Bitwise shift operators

& Bitwise AND

^ Bitwise XOR

I Bitwise OR

<, , >=, !=, Comparison operators


==
is, is Identity operators
not
in, not Membership operators
in
not Logical NOT

and Logical AND

or Logical OR

>>>x = 5 + 3 * 2 ** 2 // 4
Output:
8

>>>7*(8/(5//2))
Output:
28.0

Type Conversion
In this article, we are discussing type conversion in Python. It converts the Py-type data into another
form of data. It is a conversion technique. Implicit type translation and explicit type converter are
Python's two basic categories of type conversion procedures.
8

a. Explicit Type Conversion-The programmer must perform this task manually.


b. Implicit Type Conversion-By the Python program automatically.

Implicit Type Conversion


Implicit character data conversion is used in Python when a data type conversion occurs, whether
during compilation or runtime.
a = 15
print("Data type of a:",type(a))
b = 7.6
print("Data type of b:",type(b))
c=a+b
print("The value of c:", c)
print("Data type of c:",type(c))
output-
Data type of a: <class 'int'>
Data type of b: <class 'float'>
The value of c: 22.6
Data type of c: <class 'float'>

Explicit Type Conversion:


Let us say we want to change a number from a higher data type to a lower data type. Implicit type conversion
is ineffective in this situation, as we observed in the previous section. Explicit type conversion, commonly
referred to as type casting
a=3.5
int(a)
Output:
3
Debugging
A programmer can make mistakes while writing a program, and hence, the program may
not execute or may generate wrong output. The process of identifying and removing such
mistakes, also known as bugs or errors, from a program is called debugging. Errors
occurring in programs can be categorised as:

i) Syntax errors-Python has itsown rules that determine its syntax. The interpreter
interprets the statements only if it is syntactically correct. If any syntax error is present, the
interpreter shows error message(s) and stops the execution there. For example,
parentheses must be in pairs, so the expression (10 + 12) is syntactically correct, whereas
(7 + 11 is not due to absence of right parenthesis. Such errors need to be removed before
the execution of the program.
ii) Logical errors-A logical error is a bug in the program that causes it to behave
incorrectly. A logical error produces an undesired output but without abrupt termination of
the execution of the program.
For example, if we wish to find the average of two numbers 10 and 12 and we write the
code as 10 + 12/2, it would run successfully and produce the result 16. Surely, 16 is not the
average of 10 and 12. The correct code to find the average should have been (10 + 12)/2 to
give the correct output as 11.
iii) Runtime errors-A runtime error causes abnormal termination of program while it is
executing. Runtime error is when the statement is correct syntactically, but the interpreter
cannot execute it.
For example, we have a statement having division operation in the program. By mistake, if
the denominator entered is zero then it will give a runtime error like “division by zero”.

You might also like