0% found this document useful (0 votes)
19 views

Chapter

This document provides an introduction to Python, including its interactive and script modes, basic data types like strings and integers, variables, operators, conditional statements, and formatting output. It discusses how to create and save Python files, use print and input statements, handle indentation, and describes various Python tokens like identifiers, keywords, literals and escape sequences.

Uploaded by

18naveenkannaa
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)
19 views

Chapter

This document provides an introduction to Python, including its interactive and script modes, basic data types like strings and integers, variables, operators, conditional statements, and formatting output. It discusses how to create and save Python files, use print and input statements, handle indentation, and describes various Python tokens like identifiers, keywords, literals and escape sequences.

Uploaded by

18naveenkannaa
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
You are on page 1/ 11

Chapter -5 (CS-1)

Intro to python, variables and datatypes

Index

1|Page
Python is a programing language developed by
Guido Van Rossum is very a very powerful language
which can be executed in two different modes
Intractive mode and Script mode .

Intractive mode:
In this mode python is executed line by line
and output of singe line will be displayed
Each line of code will be saved temporarily until
there is no error in the each lines of code, program
can only be entered if “>>>” is indicated in the
python prompt .(second line cannot be written
without executing the first line)
Eg1:
>>>print (“Python Programming Language”)
Python Programming Language
>>>x=10
>>>y=20
>>>z=x + y
>>>print (“The Sum”, z) The Sum = 30
Eg2:
>>> 5 + 10
15

2|Page
>>> 5 + 50 *10
505
>>> 5 ** 2
25

To create new python program /file


Go to open section in left corner and press new file
option or simply press Ctrl+”N” keys to open a new
file

Script mode:
In this mode python is executed line by line
But the output will be showed only when Run
option is pressed or “F5” is pressed

Save the program :


To save any program choose File and press Save or
Ctrl+”S” which display a confirmation of choice and
ask for file name to be entered

Print Statement:
3|Page
It is a basic statement to display any output on the
screen
To display more than one value comma “,” can be
used to separate the values
Syntax:print(“Something”,”Something”)

Input Statement:
It is a basic statement to display get any input from
user at the run time

Syntax:Input(“Something”)

Here “Something ” is a question or text shown to


user for entering the input

Eg:
>>h=input(“what is your name”)
what is you name
>>h=input()

4|Page
Indentation :
Python uses whitespace such as spaces and tabs to
define program blocks whereas other languages like
C, C++, java use curly braces { } to indicate
all statements within the block must be indented
with same amount spaces

Tokens Python :
breaks each logical line into a sequence of
elementary lexical components known as Tokens.
The normal token types are
1) Identifiers,
2) Keywords,
3) Operators,
4) Delimiters and
5) Literals.
Whitespace separation is necessary between
tokens, identifiers or keywords

Identifiers/Variables:

5|Page
An Identifier is a name used to identify a variable,
function, class, module or object.

Rules to write a variable name:


• An identifier must start with an alphabet (A..Z or
a..z) or underscore ( _ ).
• Identifiers may contain digits (0 .. 9)
• Python identifiers are case sensitive i.e.
uppercase and lowercase letters are distinct.
• Identifiers must not be a python keyword.
• Python does not allow punctuation character
such as %,$, @ etc., within identifiers.

Eg:Name,_jjjo,ww_2

Invalid eg: 12ji,2_ee,while,if

Keywords :
Keywords are special words used by Python
interpreter to recognize the structure of program.
As these words have specific meaning for
interpreter, they cannot be used for any other
purpose.

6|Page
Operators:
In computer programming languages operators are
special symbols which represent computations,
conditional matching etc. The value of an operator
used is called operands. Operators are categorized
as Arithmetic, Relational, Logical, Assignment etc.
Value and variables when used with operator are
known as operands

Arithmetic operators:
An arithmetic operator is a mathematical operator
that takes two operands and performs a calculation
on them

Eg:
+ (Addition) >>> a + b 110
- (Subtraction) >>>a – b 90
* (Multiplication) >>> a*b 1000
/ (Divisioin) >>> a / b 10.0
% (Modulus) >>> a % 30
10 ** (Exponent) >>> a ** 2
10000 // (Floor Division) >>>
a//30 (Integer Division) 3

7|Page
Relational or Comparative operators :
A Relational operator is also called as Comparative
operator which checks the relationship between
two operands. If the relation is true, it returns True;
otherwise it returns False

Eg:

== (is Equal)
>>> a==b False
> (Greater than)
>>> a > b True
< (Less than)
>>> a < b False
>= (Greater than or Equal to)
>>> a
>= b True <= (Less than or Equal to)
>>> a
<= b False != (Not equal to)
>>> a != b True

Logical operators:
In python, Logical operators are used to perform
logical operations on the given relational
8|Page
expressions. There are three logical operators they
are and, or and not

Eg:
or >>> a>b or a==b True
and >>> a>b and a==b False
not >>> not a>b False i.e. Not True

Assignment operators:
In Python, = is a simple assignment operator to
assign values to variable. Let a = 5 and b = 10
assigns the value 5 to a and 10 to b these two
assignment statement can also be given as a,b=5,10
that assigns the value 5 and 10 on the right to the
variables a and b respectively. There are various
compound operators in Python like +=, -=, *=, /=,
%=, **= and //=

Conditional operator :
Ternary operator is also known as conditional
operator that evaluate something based on a
condition being true or false. It simply allows testing
a condition in a single line replacing the multiline if-
else making the code compact
9|Page
Delimiters:
Python uses the symbols and symbol combinations
as delimiters in expressions, lists, dictionaries and
strings. Following are the delimiters

Literals Literal is a raw data given to a variable or


constant. In Python, there are various types of
literals.
1) Numeric
2) String
3) Boolean

Escape Sequences:
In Python strings, the backslash "\" is a special
character, also called the "escape" character. It is
used in representing certain whitespace characters:
"\t" is a tab, "\n" is a newline, and "\r" is a carriage
return. For example to print the message "It's
raining", the Python command is
>>> print ("It\'s rainning")
It's raining

Python Data types :


10 | P a g e
All data values in Python are objects and each
object or value has type. Python has Builtin or
Fundamental data types such as Numbe

11 | P a g e

You might also like