[go: up one dir, main page]

0% found this document useful (0 votes)
64 views6 pages

Python Fundamentals

The document discusses Python fundamentals including programs, character set, tokens, expressions, statements, functions, variables, data types, input/output and more. Key topics are programs and functions, the character set includes letters, digits and special symbols, tokens include keywords, identifiers, literals and operators, and variables can be assigned values dynamically.

Uploaded by

Prajjwal mishra
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)
64 views6 pages

Python Fundamentals

The document discusses Python fundamentals including programs, character set, tokens, expressions, statements, functions, variables, data types, input/output and more. Key topics are programs and functions, the character set includes letters, digits and special symbols, tokens include keywords, identifiers, literals and operators, and variables can be assigned values dynamically.

Uploaded by

Prajjwal mishra
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

PYTHON FUNDAMENTALS

PROGRAM : It is a set of instructions that govern the processing.


Example : Write a program in python to add two numbers.

def sum_two_numbers(a,b):
c=a+b
return c
d = sum_two_numbers(5,6)
print(“sum of given two number is ”,d)

PYTHON BASICS : Character set, Tokens, Expressions, Statements, Simple input and output etc.
PYTHON CHARACTER SET : It is a set of valid characters that a language can recognize. Python
supports Unicode encoding standard.
 Letters : A-Z, a-z
 Digits : 0-9
 Special Symbols : space, +, -, *, /, **, \, (), [], {}, = , !=, ==, <, > , ‘ ,“, “, ‘, ; , : , %
 Whitespaces : Blank space, Tabs, Carriage return, Newline.
 Other Characters : Python can process all ASCII and Unicode characters as part of data.
TOKENS / LEXICAL UNITS / LEXICAL ELEMENTS : The smallest individual unit in a program is
known as a token.
Python has following tokens :
a) Keyword : It is a word having special meaning reserved by programming language. They
cannot be used for normal identifier names.
b) Identifiers (Names) : It is defined as names given to different parts of the program (
variables, objects, classes, functions, list, dictionaries etc)
Rules :
 First character must be a letter or _ .
 Upper case and lower case letters are different.
 Digits 0-9 can be a part of the identifier except for the first character.
 An identifier must not be a keyword of python.
 An identifier cannot contain any special character except __ .
Valid Identifiers : Myfile, Date9_7, _sci.
Invalid Identifiers : Data – Record, 29Science, break, my.file.

c) Literals / Values : They are fixed values or constant values. Python supports several kinds
of literals.
1
 String Literal : It is defined as a sequence of characters surrounded by quotes ( single
or double or triple quotes).
Example : ‘Vishu’, “Priyanshu”,’’’Jiya’’’etc.
 Python allows certain nongraphic characters in string values.
 Nongraphic characters are those characters that cannot be typed directly
from keyboard such as backspace, tabs, carriage return.
 Nongraphic characters can be represented by using escape sequences, which
are represented by a backslash ( \ ) followed by one or more characters.
Example : print(‘ \\ ’)
String Types in Python : Python supports two types of strings :
1. Single Line Strings (Basic Strings): It must terminate in one line.
Example : a = ‘ hello
there ‘
Error : By default, python creates single line strings with both single or double quotes. So,
if at the end of a line, there is no closing quotation mark for an opened quotation mark,
python shows an error.

2. Multi Line Strings : It can be created in two ways :


 By adding a backslash at the end of normal single – quote / double – quote strings.
 By typing the text in triple quotation mark (both triple apostrophe or triple quotation
marks will work)
Size of Strings : It is defined as number of characters in a string.
Example :
‘abc’ : 3, ‘hello’ : 5 , “\ab” : 2 , ‘Sajal’s pen’ : Syntax error, ‘Seema\’s pen’ : 11, “Amy’s” : 4,
‘Amy”s’ : 4
For multi – line strings created with triple quotes, the EOL (end of line) is also counted while
calculating the size of the string.
For multi – line strings created with single / double quotes and backslash character at the EOL,
the backslashes are not counted in the size of the string.

 Numeric Literal : Types of Numerical literals :-


 int (signed integers) : Positive or negative whole numbers with no decimal point.
2
 float : It represent real numbers and are written with a decimal point.
 Complex : Complex numbers of form “a + bJ”, where J2 = -1. (a = real part, b = imaginary
part of the complex number)
INTEGER LITERALS : Python allows three types of literals
 Decimal Integer Literal (0 to 9): It does not start with 0 (zero). Example : 1234, 41 , -98
 Octal Integer Literal (0 to 7): Starts with 0o. Example : 0o28, 0o14.
 Hexadecimal Integer Literal (0 to 9 AND A to F) : Starts with 0X. Ex : 0XACD (Valid),
0XPQR(Invalid).
FLOATING POINT LITERALS / REAL LITERALS : They have fractional part. They can be written in
two forms :
 Fractional form : 2.0 , 17.5, -13.0, .5 (will represent 0.5)
 Exponent form : It consist of two parts
Example : 5.8 can also be written as 0.58 x 101
 Mantissa : 0.58
 Exponent : 101
 Boolean Literal : TRUE(non – zero value) and FALSE(0).
 Special Literal None: It is used to indicate the absence of value.
d) Operators : Operators are tokens that trigger some computations when applied to
variables and other objects in an expression.
Types of operator:
1. Unary operator : +, - , ~(bitwise complement), not (logical negation)
2. Binary operator : +,-,*,/ (floating point divison) ,%,** (exponent),// (integer division)
3. Relational operator : <,>,<=,>=,==,!=
4. Logical operator : AND,OR
5. Assignment operator : =,/=,+=,-=,*=,%=,**=,//=
6. Membership operator : in, not in
7. Bitwise operator : & (bitwise AND) , ^ (XOR) , | (bitwise OR)
8. Shift operator : << (shift left), >>(shift right)
9. Identity operator : is , is not

e) Punctuators : Punctuators are special symbols that are used in programming languages to
organize programming – sentence structure, and indicate the rhythm and emphasis of
expressions, statements and program structure.
Example : ‘ , “ , # , ( ), = etc.

EXPRESSIONS : Any legal combination of symbols that represent a value is called an expression.
Example : 15, 56, a+5, (3+5)/ 4

3
STATEMENT : It is a programming instruction that does something i.e some action takes place.
Example : print(5+3) , where ‘print(5+3)’ is a statement and ‘(5+3)’ is an expression
b = a - 5 , where ‘b = a - 5’ is a statement and ‘a-5’ is an expression.
COMMENT : Comments are the additional readable information to clarify the source code. It
begins with symbol #.
Types of comment are as follows :
 Full line comment :
# Main program code follows now
 Inline comment :
if b < 5 : # checking if b is less than 5
 Multi-line comment / Block comment : We can enter multi – line comment in python code
in two ways :
 Add a # symbol in the beginning of every physical line.
Example :
#Multiline comments are useful for detailed additional information.
#Related to the program in question.
 Type a comment as a triple – quoted multi-line string
Example:
‘’’ Multiline comments are useful for detailed additional information.Related to the
program in question. ‘’’
Docstring : Comments enclosed in triple quotes (“””) or triple apostrophe (‘’’) are
called docstrings. They are very useful for documentation.

FUNCTIONS : It is a block of code that has a name and it can be reused by specifying its name in
the program, where needed.

BLOCK / CODE BLOCK / SUITE AND INDENTATION : A group of statements which are part of a
function are called block.
VARIABLES : It is a named location that refers to a value and whose values can be used and
processed during the program run.
>>> a = 5 >>> student = ‘Jacob’ >>> age = 15
LVALUE AND RVALUE
lvalue : Expressions that can come on the LHS of an assignment or lvalues are the objects to
which we can assign a value or expressions
4
rvalue : Expressions that can come on the RHS of an assignment or rvalues are the literals and
expressions that are assigned to lvalues.
Example : a = 10 , where a is lvalue and 10 is rvalue.
MULTIPLE ASSIGNMENTS
 Assigning same value to multiple variables >>> a = b = c = 10
 Assigning multiple values to multiple variables >>> x, y, z = 10, 20,30
 Swapping two variables >>> x, y = y, x

Note : Expressions separated with commas are evaluated from left to right and assigned in same order.
Example 1: Example 2: Example 3:
a , b, c = 5, 10, 7 x = 10 x , x = 20,30
b, c, a = a+1, b+2, c-1 y, y = x+2, x+5 y, y = x+10, x+20
print(a,b,c) 6,6,12 print(y) 15 print(x,y) 30 50

In Python, a variable is created when a value is assigned to it.


Example :
print(x) NameError : ‘x’ is not defined
x = 20
print(x) 20

DYNAMIC TYPING : In dynamic typing, a variable pointing to a value of a certain type, can be made to
point to a value/object of different type.
Example :
x=10
print(x) 10
x= “Hello world”
print(x) Hello world
CAUTION WITH DYNAMIC TYPING
X=10
Y=0
Y=X/2
X=’day’
Y=X/2 Error!! A string cannot be divided
TYPE (variable_name)
>>> a =10 >>>a =10.5 >>>a =”hello”
>>> type(a) >>> type(a) >>> type(a)
<class ‘int’> <class ‘float’> <class ‘str’>

SIMPLE INPUT : Built-in function input( ) is used to take input from the user.
Example :
name = input (‘What is your name ?’)
5
Note : input () always returns a value of string type.
Age = input(“Enter your age : ”)
Age = Age + 1 Error : string cannot be added with an integer

Solution:
Age =(int(input(“Enter your age : ”)))
Age = Age + 1

SIMPLE OUTPUT THROUGH PRINT STATEMENT : Built – in print( ) function.

You might also like