Ch.
3: Python
Fundamentals
You’ll be learning about Python’s Basic
like
• Character set,
• Tokens,
• Expressions,
• Statements,
• simple input and output
Character Set
• Character set is a set of valid character that a language can
recognize.
• Represent
• any letters A-Z, a-z
• digit or symbols. 0=9
• Special symbols +_*/(){}[]=!% _
Tokens
• In a passage of text, individual words and punctuation marks
are called Tokens.
• Another words we can say Lexical units.
Python has following Tokens:
• Keywords
• Identifiers
• Literals
• Operators
• punctuations
1. keywords
• Keywords are the words that convey a special meaning to the
language interpreter.
• Contain following keywords:
• False, in, is, or, pass, while, for, not, try etc…
2. Identifiers
• Identifiers are fundamentals building blocks of a program and
are used as the general terminology for the names given to
different parts of a program
• Variables, object, classes, functions, lists, dictionaries etc…
Following are some valid identifiers
• Myfile,
• date123,
• _as,
• aSDF234
Following are some invalid identifiers
• DATE-IS
• 25th
• Break
• My.myname
3. Literals / values
• Literals are data items that have a fixed value.
• we can say as a constant value.
Several kinds of Literals
• String literals
• Numeric literals
• Boolean literals
• Special literal None
String Literals
• A string literals is a sequence of characters surrounded by
quotes.
• There are two types of string literals
• Single-line
• multiline
• Ex:
• a=“Alchemy
• Surat”
• Print(a)
• o\p: Error
• A=“Alchemy\
• Surat”
• Print(a)
• o\p: Alchemy Surat
• a= “”” Welcome
To
Alchemy
School”””
• Print(a)
o\p:
Welcome
To
Alchemy
school
• a= “”” Welcome \
• to\
• Alchemy \
• School \
• Surat”””
• Print(a)
o\p:
Welcome to Alchemy School Surat
Numeric Literals
• Int
• Float
• complex
(1)Integer Literals
• Integer constant must have at least one digit and must not
contain any decimal point.it may contain + or – sign.
• Decimal integer Ex: 123, +87, -56
• Octal Integer only digit 0-7 Ex: (12)8
• Hexadecimal Integer contain 1-9 and A to F Ex: (B) 16
(2) Floating point literals
• Floating point is also called real literals.
• Fraction part Ex: 2.0, -13.5, .3
• Exponent part Ex: 152E5, -0.17E4
Boolean Literals
• True
• false
Special literal None
• Python has one special literals, which is None.
• It is used to indicate absence of value.
Operators:
• Operators are tokens hat trigger some computation when
applied to variable and the objects in an expression.
• Variables and object to which the computation is applied, are
called operands.
• So an operator requires some operands to work upon.
Unary Operator
• + unary plus
• - unary minus
• Ex: a=4 Ex: a=4
• print(a) print(-a)
• o\p: 4 o\p: -4
Binary Operator
• Arithmetic +,-,*,/
• Bitwise & , ^ , |
• Shift >>,<<
• Identity is, is not
• Relational >,<,<=,>=,!=
• Assignment =,+=, *=,-=
• Logical And, Or
• Membership in, not in
Punctuator
• Punctuator are symbols that are used in programming
languages to organize sentence structures.
• Ex: ‘ “ \ () [] {} @ , : . ; ` =
Barebones of a Python Program
Various components like:
• Expressions
• Statements
• Comments
• Function
• Block and indentation
Expressions:
• Any expressions is any legal combination of symbols that
represents a value.
• Ex: 25, x + 10, c>6 etc..
Statements:
• A statement is a programming instruction that does something.
• While an expressions is evaluated, a statements is executes.
• Ex:
• a=10
• b=15
• if a>b:
• print(“a is higher no.")
• else:
• print(“b is higher no.")
Comments:
• Comments are the additional readable information to clarify the
source code.
• Ex:
• # this is my first program.
Multi-line comments:
• ‘’’ this is my first page
Its include some statements
And some functions’’’
Function:
• A function is a code that has a name and it can be reused by
specifying its name in the program.
• Ex:
• Def functionname()
• def seeyou():
print("jignesh")
Seeyou()
Blocks and Indentation
• A group of statements which are part of another statement or a function are
called block or code of block or suite in python.
• a=10
• b=a-3
• print(a+b)
• if b>5:
• print("option 1")
• print("b is higher no.")
• else:
• print("Option 2")
• print("a is higher no.")
Multiple assignments
• Assigning same value to multiple variables
• a=b=c=25
• Assigning multiple value to multiple variables
• x,y = 5,10 correct
• x,y,z=5,10 incorrect
Variables and assignments
• A variable in Python represents named location that refers to a
value and whose values can be used and processed during
program run.
• Ex:
• a=5
• abc=“Jignesh”
• a=5 correct
• 5=a incorrect
Variable Definition
• A variable is created when you first assign a value to it.
• X=10
• Print(X)
Dynamic typing:
• A variable is defined by assigning to it some value.
• A=10
• Print(A)
• A=15
• Print(A)
Type()
• A=10
• Type(A)
• <class int>
Simple input and output:
• To get input from user interactively, you can use built in function
input().
• Ex:
• a=input(“what is your name?”)
• print(a)
• o\p:
What is your name? Jignesh
jignesh
• The input() function always returns a value of string type.
• Type(a)
• o\p: str
• Ex:
• b=input(“enter any number:”)
• print(b)
• o\p:
• Enter any number: 5
• type(b)
• o\p: str
Reading numbers:
• String values cannot be used for arithmetic or other numeric
operations.
• You need to have values of numeric types int or float.
• Python offers two functions: int() and float() to be used with input() to
convert the values received through input() into int and float types.
• a=int(input(“enter your age:”))
• print(a)
• o\p: enter your age: 15
• type(a)
• o\p: int
Possible errors when reading numeric
values:
• a= int(input(“Enter any number:”))
• Print(a)
• o\p: enter any number: 15.5
• o\p: error
• a= float(input(“Enter any number:”))
• Print(a)
• o\p: enter any number: 15.5 abc
• o\p: error
Features of print statements
• It auto-converts the items to strings.
• It inserts spaces between items automatically because the
default value of sep argument is space(‘’).
Ex: print(“my”, “name”, “is”)
o\p: my name is
Ex: print("hello","hi how are you",sep=“surat")
o\p: hellosurathihow are you
• print(“Alchemy”, end=” ”)
• print(“Surat”)
• o\p: Alchemy Surat
• a=5
• b=10
• c=a+b
• print("total value of",a,"and",b ,"is",c)
Assignment Questions
• How are keywords different from Identifiers?
• Keywords is a word having special meaning reserved by
programming language.
• Ex: input, False, True etc..
• Identifiers are fundamentals building blocks of a program and
are used as the general terminology for the names given to
different parts of a program
• Ex: value1, a, b, etc..
• What are the literals in Python?
• Literals means constants.
• Data items never change their value during a program run.
• How many types of strings are supported in python?
• Single line string
• Multi line string
• Identify the types of literals.
• 45.34
• 67
• ‘true’
• ‘abc4’
• What is the error in following Python Program.
• print(“enter value i:”,a)
• Value of a is not define.
• Write down Python program to find out area of the square.
• r=5
a=r*r
print(“the area of the square is:”, a)