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

Python Fundamentals 1

Uploaded by

jatin Pujari
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)
8 views6 pages

Python Fundamentals 1

Uploaded by

jatin Pujari
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

CH.

6: PYTHON FUNDAMENTALS

PYTHON CHARACTER SET

Character set is a set of valid characters that a language can recognize. Python encodes
characters using Unicode, which includes over 100,000 different characters from 100
languages —including natural and artificial languages like mathematics

 lower ⇐ a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z
 upper ⇐ A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
 digit ⇐ 0|1|2|3|4|5|6|7|8|9
 ordinary ⇐ |(|)| [ | ] | { | } |+|-|*|/|%|!|&| | |~|^||,|.|:|;|$|?|#
 ordinary special ⇐ ’ | " | \
 white space ⇐ | → | ←- (space, tab, or newline)
 other characters

TOKENS

The smallest individual unit in a program is known as a token or lexical unit or Building
Blocks. Python breaks each logical line into a sequence of elementary lexical components
known as tokens. Each token corresponds to a substring of the logical line (AS in English
language the words and punctuators can be considered as tokens). Python tokens include
identifiers, keywords, literals, operators and punctuators.

Keywords

Python has 30 keywords, which are the words having special meaning and are reserved for
programming. Keywords contain lowercase letters only. You cannot use keywords as
regular identifiers. Following is list of Python keywords:

and del for is raise

assert elif from lambda return

break else global not try

class except if or while

continue exec import pass with

def finally in print yield


Identifiers

An identifier is a name given by user to identify a variable, function, class, module, or other
object. An identifier

a=10

B=20

 Starts with a letter (A to Z or a to z) or an underscore (_) followed by zero or more


letters, underscores, and digits (0 to 9).
 Case is significant in Python: lowercase and uppercase letters are distinct.
 Python does not allow any other characters such as @, $, and % within identifiers.
 Must not be a keyword of python.

Following are some valid identifiers:

MyFile, data123, _2data, std_name

Following are some invalid identifiers:

My File (space not allowed), 123data (cannot start with number), 2%data (No special
character allowed), class (is a keyword )

Literals

A literal (also referred to as constant) is a number or string that appears directly in a


program. Python allows following kinds of literals:

(a) String (b) Numeric (c) Boolean (d) None (e) Collection

(a) String Literals

Text enclosed within quotes (either single ’...’ or double “…”) are string literals of python.
Even a single character within quotes is treated as string. ‘a’. “a”, “ABC”, “123abc”, “ hello +
how” , ‘1-2-@welcome’ are all string literals.

Everything written within quotes is printed as such (with some exceptions). And these
exceptions are the escape sequences. These are used to print non graphic characters, i.e.
the characters that cannot be printed directly from keyboard e.g., backspace, tab, carriage
return etc. An escape sequence starts with \ (back slash) followed by one or more
character, all enclosed in quotes. Although you can directly type single quote within double
quoted string or vice-versa.
Escape sequences in Python:

Code Result
\' Single Quote
\” Double Quote
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
\ooo Octal value
\xhh Hex value

>>> print("hello\n welcome") # \n will break line


hello
welcome
>>> print("hello\t welcome") #\t will print tab
hello welcome
>>> print('Alok\'s Pen') # to print quotes
Alok's Pen
>>print(“Alok’s Pen”) #single quote printed within
Alok's Pen double quote
>>> print("old\new") #\n of \new will break line
old
ew
print("old\\new") #\\ will print \new
old\new
Python allows two types of strings: (i) Single lined (ii) Multiline string.

(i) Single line Strings: Are created by enclosing text in single or double quotes, they
must terminate in one line. E.g. print(“Welcome\n to\n Python !!!”)
Str=”Hello How are you? This is my program”
(ii) Multiline Strings: Sometimes a single string needs to be spread along multiple lines,
this can be done by two ways in Python, (a) by adding backslash at end of each line
or (b) by typing multiple lines within triple quotes.
(a) Adding backslash at end of each line: At the end of string just add backslash
( \ ) before pressing enter key.

>>>print("Hello\

welcome to my\

first program")

OUTPUT: Hello welcome to myfirst program

(b) Typing text in triple quotes: You can create multiline strings by
writing them within triple quotes (either single or double quotes).
>>> text=”””Hello
welcome to
Python Programming”””
>>> print(text)
Hello
welcome to
Python Programming
>>>

Length of a string: Length (size) of a string is the count of number of characters in it. For
example length of “Alok” is 4 and of “ABC 123” is 7 (space is also counted as a character).
For getting length (size) of a string there is Python function len(<object name>).

>>> a="Alok"
>>> len(a)
4
>>> len(“ABC 123”)
7
But remember if there is an escape sequence character within a string it is counted as a
single character. Length of “Alok\’s” would be 6, “Old\new” will give length 6 (as \n of \new
would be treated as single character)

>>>text=''’a
B
c'''
>>> print(len(text))
5
text=''A \
B\
C''
>>> print(len(text))
4
The enter keys used in first example above are considered as End of Line (EOL) character
and are counted in the length of string. While the backslash ( \ ) used in second example
are not counted as characters of string.

(b) Numeric Literals:


Numeric literals store Numerical Values. These are of three different types:
i. Integer & Long
ii. Float/floating point
iii. Complex

Integer Literals:
Integers are the whole numbers consisting of + or – sign without any decimal digits like
100000, -99, 0, +17. While writing a large integer value, don’t use commas to separate
digits. Also integers should not have leading zeros.
Range of an integer in Python can be from -2147483648 to 2147483647, and long integer
has unlimited range subject to available memory. These are also called decimal integer
literals. Apart for this there may be Octal or Hexadecimal Integer literal.
A sequence of digits starting with 0o (zero followed by letter o) is taken to be octal integer.
For example:
>>> a=0o14
>>> a
12
14 an octal number is equivalent to 12 in decimal (1 * 81+4 * 80 = 8+4 = 12)
Likewise sequence of digits starting with 0x or 0X is considered as hexadecimal integer.
For example:
>>> a=0x14
>>> a
20
14 a hexadecimal number is equivalent to 20 in decimal (1 * 161+ 4 * 160 = 16+4 = 20)

Floating Point Literals:


Numbers with fractions or decimal point are called floating point or real literals. A floating
point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -
21.9, 0.98333328, 15.2963. These numbers can also be used to represent a number in
Exponent (engineering/ scientific) notation.
-2.0 X 105 will be represented as -2.0E5
2.0X10-5 will be 2.0E-5

Complex Literals would be discussed later.


(c) Boolean Literals
Python contain Boolean Type which is a unique data type, consisting of two constants,
True & False. A Boolean True value is Non-Zero, Non-Null and Non-empty.
(d) None
This is special data type with single value. It is used to signify the absence of value/false in
a situation. It is represented by None.

You might also like