Review of Python Basics

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

CH:1 review of python basics

 Python
o Python is a powerful and high-level language and it is an interpreted
language.
o Python gives us two modes of working
1. Interactive mode
2. Script mode
 It is possible to develop various apps with Python like
o GUI Apps
o Web Apps
o Games
o DBMS Apps
o Scripting
 Python Limitations
o There are few limitations in Python which can be neglected because of its
vast usage.
o It is not a Fast Language.
o Libraries are very less.
o It is week in Type binding.
o It is not easy to convert in some other language.
 Tokens
o Token- is the smallest unit of any programming language. It is also known
as Lexical Unit. Types of token are
1. Keywords
2. Identifiers (Names)
3. . Literals
4. . Operators
5. . Punctuators
 Keywords:
o Keywords are those words which provides a special meaning to
interpreter. These are reserved for specific functioning. These cannot be
used as identifiers, variable name or any other purpose. Available
keywords in Python are-
1
 Identifiers
o These are building blocks of a program and are used to give names to
different parts/blocks of a program like - variable, objects, classes,
functions.
o An identifier may be a combination of letters and numbers.
o An identifier must begin with an alphabet or an underscore(_).
Subsequent letters may be numbers (0-9).
o Python is case sensitive. Uppercase characters are distinct from lowercase
characters (P and p are different for interpreter).
o Length of an Identifier is unlimited.
o Keywords can not be used as an identifier.
o Space and special symbols are not permitted in an identifier name except
an underscore( _ ) sign.
o Some invald identifiers are –
 DATA-REC, 29COLOR, break, My.File
o Some valid identifiers are
 Myfile, Date9_7_17, Z2T0Z9, _DS, _CHK FILE13
2
 Literals / Values
o Literals are often called Constant Values.
o Python permits following types of literals –
 String literals - “Pankaj”
 Numeric literals – 10, 13.5, 3+5j
 Boolean literals – True or False
 Special Literal None
 Literal collections
 String Literals
o String Literal is a sequence of characters that can be a combination of
letters, numbers and special symbols, enclosed in quotation marks, single,
double or triple(“ “ or ‘ ‘ or “’ ‘”).
 In python, string is of 2 types
 Single line string
o Text = “Hello World” or Text = ‘Hello World’
 Multi line string
o Text = ‘hello\ or Text = ‘’’hello
world’ word ‘’’
 Numeric Literals
o Numeric values can be of three types
 int (signed integers)
 Decimal Integer Literals – 10, 17, 210 etc.
 Octal Integer Literals - 0o17, 0o217 etc.
 Hexadecimal Integer Literals – 0x14, 0x2A4, 0xABD etc
o float (floating point real value)
 Fractional Form – 2.0, 17.5 -13.5, -.00015 etc.
 Exponent Form - -1.7E+8, .25E-4 etc.
o complex (complex numbers)
 3+5i etc.
 Boolean Literals
o It can contain either of only two values – True or False
 A= True
 B=False
3
 Special Literals
o None, which means nothing (no value).
 X = None
 Operators
o An Operator is a symbol that trigger some action when applied to
identifier (s)/ operand (s)
o Therefore, an operator requires operand (s) to compute upon.
 example : c = a + b
o Here, a, b, c are operands and operators are = and + which are performing
differently.
 Punctuators
o In Python, punctuators are used to construct the program and to make
balance between instructions and statements. Punctuators have their own
syntactic and semantic significance.
o Python has following Punctuators –
 ‘ , ” , #, \, (, ), [, ], {, }, @. ,, :, .. `, =
 DATA TYPES
o Data can be of any type like- character, integer, real, string.
o Anything enclosed in “ “ is considered as string in Python.
o Any whole value is an integer value.
o Any value with fraction part is a real value.
o True or False value specifies boolean value.
o Python supports following core data types
 Numbers (int like10, 5) (float like 3.5, 302.24) (complex like 3+5j)
 String (like “pankaj”, ‘pankaj’, ‘a’, “a” )
 List like [3,4,5,”pankaj”] its elements are Mutable.
 Tuple like(3,4,5,”pankaj”) its elements are immutable.
 Dictionary like {‘a’:1, ‘e’:2, ‘I’:3, ‘o’:4, ‘u’:5} where a,e,i,o,u are
keys and 1,2,3,4,5 are their values.
 Variables and Values
o In Python, values are actually objects.
o And their variable names are actually their reference names. Suppose we
assign 10 to a variable A.
4
A = 10
o Here, value 10 is an object and A is its reference name.

A 10
Reference Object
Variable
 Mutable and Immutable Types
o Following data types comes under mutable and immutable types
 Mutable (Changeable)
 lists, dictionaries and sets.
 Immutable (Non-Changeable)
 integers, floats, Booleans, strings and tuples.
 Operators
o The symbols that shows a special behavior or action when applied to
operands are called operators. For ex- + , - , > , < etc.
o Python supports following operators
 Arithmetic/ Mathematical Operator
 Relation / Comparison Operator
 Identity Operators
 Logical Operators
 Assignment Operators
 Membership Operators
 Operator Associativity
o In Python, if an expression or statement consists of multiple or more than
one operator then operator associativity will be followed from left-to
right.
>>> 7*8/5//2
>>> 5.0
o In above given expression, first 7*8 will be calculated as 56, then 56 will
be divided by 5 and will result into 11.2, then 11.2 again divided by 2 and
will result into 5.0.
Only in case of ** , associativity will be followed from right-to-left.

5
>>> 3**3**2
19683
Above given example will be calculated as 3**(3**2).
 Type Casting
o As we know, in Python, an expression may be consists of mixed
datatypes. In such cases, python changes data types of operands
internally. This process of internal data type conversion is called
implicit type conversion.
o One other option is explicit type conversion which is like- (identifier)
 For ex a=“4” b=int(a)
 Another ex
 If a=5 and b=10.5 then we can convert a to float. Like
d=float(a)
o In python, following are the data conversion functions
(1) int ( ) (2) float( ) (3) complex( ) (4) str( ) (5) bool( )

You might also like