Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python Fundamentals

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Fundamentals

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Fundamentals

Python character set: Character set is a set of valid Characters that a language can
recognize.

 Letters: A-Z, a-z


 Digits: 0-9
 Special symbols: Space, + - * / \** () [] {} // + != == < , ? > .’ “ ‘’’ ; : % # _ etc.
 Whitespaces: blank space, tab, carriage return, new line
 Other characters: python can process all ASCII and Unicode characters

TOKENS IN PYTHON
The smallest individual unit in a program is known as Token.

Tokens in Python are Keywords, identifiers, Literals, Operators, Punctuators

Keywords:

A keyword is a word having special meaning reserved by programming language.

Examples: True, False, None, for, while, if, else, or, elif, break, is, in, with, import
etc.

Identifiers (Names):

Identifiers are the names given to different parts of the program.


Examples: variables, objects, classes, functions, list etc.

Naming rules:

 The first character must be a letter or underscore ( _ ).


 The digits 0 through 9 can be part of the identifier.
 Python is case sensitive as it teats upper and lower case character differently.
 An identifier must not be a keyword of Python.
 An identifier cannot contain any special character except underscore ( _ ).
Valid identifiers invalid identifiers
Name=”Rahul” 6Name=”Rahul”

_name=”anuj” s.name=”anuj”

Class_sec=”XI F” Class sec=”XI F”

Num1std=”sunil kumar” for=”sunil kumar”

Literals:

Literals are data items that have a fixed/constant value.


Types of Literals:

 String Literals
 Numeric Literals
 Boolean Literals
 Special Literal None

String Literals:
A string literal is a sequence of characters surrounded by quotes.

Example: ‘ops’, “oxford”

Nongraphic character:

Nongraphic characters are those characters that cannot be typed directly from
keyboard.

Example: newline, tab, carriage return etc.

Nongraphic characters can be represented by using escape sequences. An escape


sequence is represented by backlash (\) followed by characters.

Escape sequences in Python:

\\ - Backslash (\)
\’ -Single quote (‘)

\” -Double quote (“)


\n - New line

\t - Horizontal tab
String type in Python

 Single line strings


 Multiline strings

>>> s='''ops >>> s=''ops\


ranchi''' Ranchi"
>>> s >>> s
'ops\nranchi' 'opsranchi'
>>> len(s) >>> len(s)
10 9

Numeric Literals

 Integer
 Floating point
 Complex numbers

Integer: Integer Literals are whole number without any fractional part. It may
contain either + or – sign. A number with no sign is assumed to be positive.

Python allows three types of integer literals:

i) Decimal Integer : 25, 200,+30,-157


ii) Octal Integer : A sequence of digits starts with 0o is taken to be an octal
integer. Example:- Decimal integer 23 will be written as 0o27 or 0O27.
iii) Hexadecimal Integer: A sequence of digits starts with 0x is taken to be an
hexadecimal Integer. Example: Decimal int 23 will be written as 0x17 or
0X17
Floating point: Floating literals are also called real literals. Floating literals are
numbers having fractional parts.
Fractional form : 30.5, -50.5, 0.25

Exponent form:
A real literal in Exponent form consists of two parts: mantissa and exponent

Example: 7.5=0.75x101=0.75E01,

Complex numbers: python represents complex numbers in the form A+Bj, python
uses j or J in place of traditional i.

X=0+5.5j Y=2.3+5j

print(x)=5.5j print(y)= (2.3+5j)


x.real=0.0 y.real=2.3

x.imag=5.5 y.imag=5.0

Boolean Literals: True or False

Special Literal: None


Operators:
Operators are tokens that trigger some computation/ action when applied to variables and
other objects in an expression.
Unary operators: the operators that act on one operand are referred to as unary
operators.
Unary+, and Unary –
Example: 4, -4, -3.5
Binary operators: operators that act upon two operands are referred to as Binary
operators.
 Arithmetic operators:+ - * / % // **
Example: 3+3= 6, 3*3= 9, 5-2= 3, 5/2= 2.5, 2**2=4, 2**2**3=256, (2**2)**3=64
5//2= 2, -5//2= -3, 14%3= 2, -14%-3= -2, 14%-3= -1, -14%3= 1

 Relational operators: < <= > >= != ==


Examples: 5<6-True, 5==8- False , 4!=3- True, 5>=3- True
 Assignment operators:=, /=, +=, **=
Example: a=5, (a=a+5 or a+=5)
 Identity operators: is is not
Examples:
>>>A=5 >>>A=5 >>>A=5 >>>A=”ops”

>>>B=6 >>>B=5 >>>B=5 >>>B=”ops”

>>>A is B >>>A is B >>>A is not B >>>A is B

False True False True

 Logical Operators: and or not

Examples:
>>> 5 and 6 >>> 5 or 6 >>> 5 or 6 and 0 >>> not 5
6 5 5 False
>>> 0 and 5 >>> 0 and 5 >>> 0 and 5 or 2 >>> not (0 and 2 or 0)
0 5 2 True
Truth value (False): 0, 0.0, 0j, False, None, ‘’, *+, ,-, ()

 Bitwise operators: & | ^ (xor) ~ complemets


Examples:
>>> 10 & 12 >>> 10 | 12 >>> 10 ^12 >>> ~12
8 14 6 -13
>>> 20 & 10 >>> 20 | 10 >>> 20 ^ 10 >>> ~20
0 30 30 -21

 Membership operators: in not in


Examples:
>>> "s" in "school" >>> "s" not in "school" >>> "S" not in "school"
True False True
>>> "p" in "school" >>> "p" not in "school" >>> "P" not in "school"
False True False

Punctuators:
Punctuators are symbols that used in programming languages to organize sentence
structure, statement, program structure etc.
Examples:
# \( )*+ ,-: = ‘“

You might also like