0% found this document useful (0 votes)
2 views4 pages

CH 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Computer

Chapter – 1
Introduction to Python

It is widely used general-purpose, high-level programming language. Developed


by Guido van Rossum in 1991.

Features of Python
1. Easy to use
2. Interpreted language
3. Cross-platform language

Modes in Python:
i) Interactive Mode: It executes one by one command.
ii) Script Mode: It used to execute the multiple instruction (complete
program) at once.

Tokens: A token is the smallest individual unit in a python program. It is also


known as Lexical Unit.
Keywords: Python keywords are unique words reserved with defined
meanings. Eg :print
Identifiers: Identifiers are names used to identify a variable or function. Eg:
variable name
Literals: Literals are the fixed values or data items used in a source code.

Operators:
An operator is used to perform specific mathematical or logical operation on
values.
i) Arithmetic operators : =,+-,*,//,%
ii) Assignment operators : =,+=,-=
iii) Comparison operators: <,>,==,<=,>=
iv) Logical operators: and, or,NOT
v) Membership operators: in, not in

Comments in python: Comments are non-executable statements of


python. Types of comment:
i) i. Single line comment (#) – comments only single line.
ii) ii. Multi-line comment (‘‘‘………..’’’) – Comments multiple line.

Input Statement:
Input()-This function first takes the input from the user and converts it into a
string.
name = input('What is your name?\n')
Output using print() function:
print() : function prints the message on the screen or any other standard output
device.

Numbers: Number data types store numeric values.


Python supports four different numerical types
• int (signed integers)
• long (long integers, they can also be represented in octal and hexadecimal)
• float (floating point real values)
• complex (complex numbers)

Strings: String in Python are identified as a contiguous set of characters


represented in the quotation marks. Python allows for either pairs of single or
double quotes.
print str # Prints complete string Hello World!
print str[0] # Prints first character of the string H
print str[2:5] # Prints characters starting from 3rd to 5th llo
print str[2:] # Prints string starting from 3rd character llo World!
print str * 2 # Prints string two times Hello World!Hello World!
print str + "TEST" # Prints concatenated string Hello World!TEST

Lists:
A list contains items separated by commas and enclosed within square brackets
([]).
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list ['abcd', 786, 2.23, 'john', 70.2]
print list[0] # Prints first element of the list- abcd
print list[1:3] # Prints elements starting from 2nd till 3rd
- [786, 2.23]
print list[2:] # Prints elements starting from 3rd element -[2.23, 'john', 70.2] print
tinylist * 2 # Prints list two times-[123, 'john', 123, 'john'] print list + tinylist #
Prints concatenated lists-['abcd', 786, 2.23, 'john', 70.2, 123, 'john']

Tuples:
A tuple is another sequence data type that is similar to the
list. tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints the complete tuple--('abcd', 786, 2.23, 'john', 70.2)
print tuple[0] # Prints first element of the tuple-- abcd
print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
--(786, 2.23)
print tuple[2:] # Prints elements of the tuple starting from 3rd element--(2.23,
'john', 70.2)
print tinytuple * 2 # Prints the contents of the tuple twice--(123, 'john', 123,
'john')
print tuple + tinytuple # Prints concatenated tuples-- ('abcd', 786, 2.23, 'john',
70.2, 123, 'john')

Dictionary:
Python's dictionaries are kind of hash table type. They consist of key-value pairs
dict = {}
31
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key This is one
print dict[2] # Prints value for 2 key This is two
print tinydict # Prints complete dictionary {'dept': 'sales', 'code': 6734, 'name':
'john'}
print tinydict.keys() # Prints all the keys ['dept', 'code', 'name']
print tinydict.values() # Prints all the values ['sales', 6734, 'john']

Boolean:
Boolean represent one of two values: True or False.
Type conversion:
i) Explicit conversion: int(value) – convert value to an integer. ii)
Implicit conversion: In implicit conversion, data conversion is done
automatically. Var=10.5(float)
Control statements:
There are three types of control statements.
i. Decision Making Statements
ii. Iteration Statements (Loops)
iii. Jump Statements (break, continue, pass)

Decision Making Statement: Decision making statement used to control the


flow of execution of program depending upon condition.
There are three types of decision making statement.
i. if statements
ii. if-else statements
iii. Nested if-else statement

Iteration Statements (Loops):


Iteration statements(loop) are used to execute a block of statements as long as
the condition is true.
Python Iteration (Loops) statements are of three type :-
1. While Loop:
while (condition):
statement
[statements]
2. For Loop:
for val in sequence:
statements
for val in range(start,stop,step):
statements
3. Jump Statements
There are three types of jump statements used in python.
1.break
2.continue
3.pass
i) break it is used to terminate the loop.
ii) Continue: It is used to skip all the remaining statements in the loop and
move controls back to the top of the loop.
iii) pass Statement: This statement does nothing. It can be used when a
statement is required syntactically but the program requires no
action.

You might also like