0% found this document useful (0 votes)
24 views13 pages

Python Fundamentals

The document discusses the fundamentals of Python including its character set, tokens, keywords, identifiers, literals, operators, punctuators, comments, statements, expressions, blocks, functions, variables and dynamic typing. It explains the different types of tokens, literals, operators and variables in Python as well as concepts like identifiers, comments, statements, expressions, blocks and functions.

Uploaded by

Tanmay sinha
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)
24 views13 pages

Python Fundamentals

The document discusses the fundamentals of Python including its character set, tokens, keywords, identifiers, literals, operators, punctuators, comments, statements, expressions, blocks, functions, variables and dynamic typing. It explains the different types of tokens, literals, operators and variables in Python as well as concepts like identifiers, comments, statements, expressions, blocks and functions.

Uploaded by

Tanmay sinha
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/ 13

03-08-2022

Python
Fundamentals

Python character set


Character set is a set of valid characters that a language
can recognise.

Letters : A – Z, a – z
Digits :0–9
Special Symbols : + - * / ^ () : etc.
White spaces : space, tab, newline etc
Other characters : any of the 256 ASCII and Unicode
characters

1
03-08-2022

Tokens
The smallest individual unit in a program is known as a
Token or a lexical unit.
Python has following types of tokens:
• Keywords
• Identifiers (Names)
• Literals
• Operators
• Punctuators

Keywords
Keywords are the words having a special meaning
reserved by the programming language.

Keywords have special meaning and hence should


not be used as normal identifier names.

Ex. if, elif, else, while, class, not etc.

2
03-08-2022

Identifiers
Identifiers are fundamental building blocks of a program and are used as the
names given to different parts of a program e.g. variable, object, classes,
functions etc.
Rules for forming identifiers:

• It’s a arbitrary long sequence of letters, digits (0-9) or underscore(_)


without any space in between, no other special characters are allowed
• First character must be a letter or an underscore( _ ) (digit)
• Upper and lower case letters are different (i.e. it is case sensitive)
• Keywords can’t be used as name of an identifier
• Avoid using names of built in function (e.g. int, input, print etc)

Ex. VAR1, _var1, var1 are all valid


1var, -var, print, Print ------- ?

Literals
Literals/constants are data items that never changes their values
during execution of program or have a fixed value.

Different kinds of literals in Python are:


1. String literals
2. Numeric literals
3. Boolean literals
4. Special literal None

3
03-08-2022

String Literals
A string literal is a sequence of characters surrounded by quotes
(single/ double/triple).
e.g. 'Welcome', ''Computer Science''

Nongraphic characters are also allowed in Python strings.


Characters which can’t be typed from keyboard are non-graphic
characters. These can be printed/typed using escape sequence
character. It starts with backslash (\). Ex:
\n – newline
\t – tab

How to print in Python ''Hari's Book''

String Types
Two types in Python:
i) Single line String ii) Multiline String
>>>Text = '''abc
a) Can be created as:
def
>>>Text = 'Python program'
pqr'''
>>>Text = ''Python program''
>>>abc
>>>Text
def
Python program pqr
len(Text)=11
b) Can be created as: ''' can be used for writing
>>>Text = 'abc\ multiline string it
def\ treats/counts newline as a
pqr' single character
>>>Text
abcdefpqr
len(Text) = 9
\continuation of string

4
03-08-2022

Numeric Literals
Can be of three types:
• Integer
Ex. 235, -45,
0o457, 0O306 (Octal , 0-zero, o-letter)
0x69A, 0X5B8 (Hexadecimal, 0-zero, x-letter)
0b1011, 0B1011 (Binary, 0-zero, b-letter)
• float
Ex. 17.5, .8, 179E05, 3.E3, 25E–4, 4.E5
• complex
of the form a+bj where j (or J) =

Boolean and None Literals


• A Boolean literal is a literal having two possible values True or
False

• None is a literal in Python which means absence of any value


or no value. Python doesn’t print anything if asked to print
value of a variable whose value is None
>>>v1=10
>>>v2=None
>>>v1
10
>>>v2
With print command v2
prints None else it prints
>>>print(v2) nothing
None
>>>

5
03-08-2022

Operators
Operators are tokens that perform some computation when
applied to variables and other objects in an expression.

Operators can be –
• Unary(requires one operand)
e.g. – (unary minus), +(unary plus)
~(bitwise complement)
not (logical negation)

• Binary(needs two operand)


e.g. arithmetic, relational, bitwise, shift, identity, logical,
assignment and membership

Punctuators
Punctuators are the symbols used in programming languages to
organize statements

[] () {} , : " ' : \ # etc

Each of them have their own meaning and function.

6
03-08-2022

Sample Program
# Sample program Comment in the program,
# Definition of function Print begins with #
def Print():
User defined function
print ("Function print")
# Main program code
A = 15
Expressions
B = A-10
print (A+3)
if B>10 : : specifies beginning of a block ,
block begins with an indentation
print("Value more than 10") and can contain more than one line
else: #block
print("Value less than 10")
print()

Components of a Program
It can be:
• Statements
• Comments
• Functions
• Expressions
• Blocks

7
03-08-2022

Statements
Smallest instruction in a program that does something.

e.g.
A = 15 #value of variable A is 15
B=A–3 #value assigned to B
print ("Hello") #prints a message

Expression
Any legal combination of symbols that represent a value.

e.g.
15 #can be value only
3.9
A+5
(b+3)/7

8
03-08-2022

Comments
Comments are pieces of codes that the compiler discards or ignores and doesn’t
execute. It is to increase the readability/understandability of the program.

Comments can be –
1. Single line comments - #aaaaaa
2. Multiline comments -

''' abc OR '''abc\


xyz pqr\
pqr ''' xyz'''

(Comments enclosed in '''/""" (triple quotes) are called docstrings, generally


written in the beginning of a program)

Block and Function


Block/Code Block/Suite
A group of statements which are part of another statement or
a function are called block.

Function
A function is a code/block that has a name, does a specific
task and can be reused in the program by its name.

9
03-08-2022

Variables
Named labels, whose values can be used and processed
during program run, are called variables.

Sub = 'Physics' #string variable


A = 10 #numeric variable

lvalue rvalue
left hand side value right hand side value
x=10 #integer class
A variable pointing to a value of
print (x) a certain type, can be made to
x=10.7 #float class point to a value/ object of
print (x) different type. This is called
Dynamic Typing in Python.
x = ‘Computer’ #string class
print (x)

Variables
Multiple assignments:
a=b=c=10
x, y, z = 10, 20, 30 #assigns x=10, y=20, z=30
Swapping
x, y = 25, 40
x, y = y, x
Execution
a, b, c = 5, 10, 9
b, c, a = a+1, b+2, c-1 #will assign a=8, b=6, c=12

(comma separated expression evaluation is from left to right and


assigned in same order)

10
03-08-2022

Variable creation and Dynamic Typing


(Everything is an object in Python)
Dynamically-typed languages perform type checking at runtime, while statically typed languages perform type checking at compile time.

Python variables are created by assigning a value of required type to it.


>>> x = 10
>>> y = 10 #y=x
Creates an integer object x with value 10 in it.
For y no new object creation, python allocates y the same reference as
that of x (dynamic typing). If,
>>> x = x + 1
For x Python will create another object.
>>> z = 10
z will refer to the same memory location as of y. If,
>>> z = "CAR"
Python will allocate new space for z.

Input/Output in Python
Input/reading Can be done using input function which reads a string,
printing can be done using print function

>>>name = input("Enter your name")


Enter your name:
>>>print(name)

input reads everything as a string, numbers even, hence can not be


used in an arithmetic expression.
int() and float() functions can be used for it.

>>>num = int(input("Enter an integer: "))


OR
>>>num = float(input("Enter a decimal number: "))

11
03-08-2022

Input/Output in Python
Variations of print
• converts item to string before printing
• two parameter to work with is sep(default is single space) and end (default is newline

>>>print ('Python')
>>>print ('Sum of 2 and 3 is' , 2+3)
>>>a=25
>>>print(''double of'', a, ''is'', a*2)
>>>print("Computer", "Science")
Computer Science #auto insertion of space in between,
#because of default sep value
>>>a, b, c = 10, 20 ,30
>>>print ("Numbers are: ", a, b, c)

Input/Output in Python
Variations of print:
print("Computer")
print("Science")
Output: Computer
Science
(print terminates with newline)

print("Computer", end='$')
print("Science")
Output: Computer$Science

print("Computer", end=' ')


print("Science")
Output: Computer Science

12
03-08-2022

Input/Output in Python
Print variation with sep:

Assignment
1.+, -, *, / of two numbers
2.Area and perimeter of square
3.Area and perimeter of rectangle
4.Area and circumference of circle
5.Average of 5 numbers
6.Area of triangle on base and height
7.C to F and F to C
8.CM to feet and inches

13

You might also like