0% found this document useful (0 votes)
6 views3 pages

Python Programming Basics Explained

Uploaded by

psoyam02
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Python Programming Basics Explained

Uploaded by

psoyam02
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic of Python Programming

Python Character set- A character set is the set of all valid characters that the Python language recognizes.
The following characters are included in the python character set-
 Letters: A-Z, a-z
 Digits: 0-9
 Special symbols: + - * / \ ( ) [ ] { } = ! > < . , ' " ; : % & # @ _
 Whitespaces: space key, tab, carriage return, newline, formfeed
 Other characters - All other ASCII and Unicode characters are recognized a part of data only.

Tokens - Tokens are the smallest individual unit in a program. Python has the following tokens

a) Keywords
b) Identifiers
c) Literals
d) Punctuators/Delimiters
e) Operators

a) Keywords- Keywords are words having special meaning in a programming language. eg. True, False, if, elif
etc.

b) Identifiers - Identifiers are names given to different parts of a program such as variables, objects,
functions etc.

Rules for naming identifiers-

1) Keywords cannot be used


2) They can contain only letters, numbers and underscore.
3) They cannot begin with a number
Examples of valid identifiers - abc, abc123, abc_123, _abc
Examples of invalid identifiers - 9abc, abc$123, elif

c) Literals - Literals are data items having a fixed or constant value. Literals can be -
i. String Literals- a sequence of characters enclosed in single quotes, double quotes, triple single
quotes or triple double quotes. Eg. 'abc', "hello", '''good morning'''
ii. Numeric Literals - Literals containing numeric data. They can be -
a. Integers - Integers not containing decimal point. They can be written in the following ways-
i. Decimal form- containing decimal symbols 0-9 and can be preceded by +/-
ii. Octal form - containing octal symbols 0-7 and preceded by 0o/0O i.e. digit zero
followed by either capital 'O' or small 'o'.
iii. Hexadecimal form - containing hexadecimal symbols 0-9, a-f, A-F and preceded by
0x/0X i.e. digit zero followed by small 'x' or capital
b. Floating point literals - They are numbers containing decimal point. They can be written in -
i. Fractional form - eg -3.2, 4.5567
ii. Exponent form - eg 0.5E2, 3.67e5. The E/e represents power of 10 and the number
following E/e must be an integer.
iii. Boolean Literals - They have value either True or False.

Page 1 of 3
iv. None - It is a special literal which is used to indicate absence of value

d) Punctuators/Delimiters - Punctuators are symbols that are used to organize sentences/lines in python.
Eg. ', ", (, [ etc.

e) Operators - Operators are symbols that trigger some computation when applied to variables or other
objects. The following are some kinds of operators-
a) Arithmetic operators: +, -, *, /, %, **, //
b) Assignment operator: =
c) Relational operators: >, <, >=, <=, ==, !=
d) Logical operators: not, and , or
e) identity operators, is , is not
f) membership operators- in , not in
g) arithmetic-assignment operators - +=, -=, *=, %=, **=, //=

Precedence of Operators
When an expression consisting of different operators is encountered, the expression is evaluated based on
precedence of operator. Higher precedence operator is evaluated first before lower precedence operator.

Operators Description

() Parentheses Highest Precedence


x ** y Power (exponentiation)
−x, +x Negation, identity
x / y, x // y, x % y, x * y True Division; floor division; (Remainder/format); Multiplication; repetition;
x – y, x + y (Subtraction/set difference), (Addition, concatenation)
x == y, x != y, x < y, x <= y, x > y, x >= y Relational Operators
not x Logical negation
x and y Logical AND
x or y Logical OR
= Assignment operator Lowest Precedence

Operator associativity
Operator associativity determines the order of evaluation when they are of same precedence, and are not
grouped by parenthesis. An operator may be Left-associative or Right–associative. In left associative, the
operator falling on left side will be evaluated first, while in right associative operator falling on right will be
evaluated first.

Usually all operators are left associative, except '**' which is right associative.

Variables - Variables are named storage location whose values can be manipulated during program run.

Dynamic Typing - In python the same variable name can be used to refer to one data type such as int and in
the next statement it can be used to refer to some other data type such as a string. This facility of changing
the data type of a variable during program execution is known as Dynamic Typing.

Assignment Operator - The = is the assignment operator in python. It is used to assign a variable on the left
side of the =, a value or an expression that evaluates to a value that is present on the right side of the =. The

Page 2 of 3
left side of the = symbol must contain either a single variable or if more than one variable is present then
they must be separated by comma. The right side of the = symbol contains values or expressions that
evaluates to a value. The number of values on the right side must match the number of variables on the left
side.

Examples of valid assignment statements:


a=5
a, b = 5, 'hello'
Examples of invalid assignment statements:
5=a
a, b, c = 5, 6

print statement-
The print statement is used to display output. The print can be used in the following ways:
a) print('hello') # displays hello
b) x=5
print (x) #used to display value contained in variable x
c) print("abc", "def", "ghi") # displays abc def ghi there is a space in between the words
d) print("jk", "lm", "no", sep=', ') # displays jk,lm,no i.e. separator ',' is added in between the words
e) print("pq" , "rs", sep="!", end="#") #end changes the default end line character from '\n' to '#'
print ("tu") # prints pq!rs#tu in a single line

input statement-
The input statement is used to accept user input. The user input is always accepted as a string which can be
converted to any other data type by using the appropriate type cast operator.
eg. name = input('Enter your name:')
age = int(input('Enter your age:'))

Page 3 of 3

You might also like