0% found this document useful (0 votes)
22 views23 pages

Variables, Expressions, Statements

The document provides an overview of fundamental programming concepts in Python, including variables, identifiers, keywords, expressions, statements, and scripts. It explains how to create and use variables, the rules for naming identifiers, and different types of expressions and statements. Additionally, it covers input functions, multi-line statements, and the importance of indentation in Python syntax.

Uploaded by

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

Variables, Expressions, Statements

The document provides an overview of fundamental programming concepts in Python, including variables, identifiers, keywords, expressions, statements, and scripts. It explains how to create and use variables, the rules for naming identifiers, and different types of expressions and statements. Additionally, it covers input functions, multi-line statements, and the importance of indentation in Python syntax.

Uploaded by

DEIVAM R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Variables,

Expressions,
Statements
Variables
Variable means its value can vary.

You can store any piece of information in a variable.

Variables are nothing but just parts of your computer’s memory

where information is stored.

To be identified easily, each variable is given an appropriate name.


Identifier
Identifiers are names given to identify something. This something can be a variable, function, class,

module or other object. For naming any identifier, there are some basic rules like:
 The first character of an identifier must be an underscore ('_') or a letter (upper or lowercase).
 The rest of the identifier name can be underscores ('_'), letters (upper or lowercase), or digits (0-9).
 Identifier names are case-sensitive. For example, myvar and myVar are not the same.
 Punctuation characters such as @, $, and % are not allowed within identifiers.

Examples of valid identifier names are sum, __my_var, num1, r, var_20, First, etc.

Examples of invalid identifier names are 1num, my-var, %check, Basic Sal, H#R&A, etc.
Keywords
The interpreter uses keywords to

recognize the structure of the


Python 3
program, and they cannot be used as has these
variable names
Keywords are displayed in a different color
keywords:
Expressions
An expression is a combination of values, variables, and operators. A value all by
itself is considered an expression, and so is a variable so the following are all legal
expressions:
>>> 42
42
>>> n
17
>>> n + 25
42
Expressions
An expression is any legal combination of symbols (like variables, constants and operators) that
represents a value.

In Python, an expression must have at least one operand (variable or constant) and can have
one or more operators.

On evaluating an expression, we get a value. Operand is the value on which operator is applied.
 Constant Expressions: One that involves only constants. Example: 8 + 9 – 2

 Integral Expressions: One that produces an integer result after evaluating the expression. Example:

a = 10

 Floating Point Expressions: One that produces floating point results. Example: a * b / 2

 Relational Expressions: One that returns either true or false value. Example: c = a>b

 Logical Expressions: One that combines two or more relational expressions and returns a value as
True or False. Example: a>b && y! = 0

 Bitwise Expressions: One that manipulates data at bit level. Example: x = y&z

 Assignment Expressions: One that assigns a value to a variable. Example: c = a + b


Statement: Instructions written in the
source code for execution are called
statements.
A statement is a unit of code that has an effect, like creating a variable or displaying a
value.
>>> n = 17
>>> print(n)
The first line is an assignment statement that gives a value to n.
The second line is a print statement that displays the value of n.
When you type a statement, the interpreter executes it, which means that it does
whatever the statement says.
There are different types of statements in the Python programming language like Assignment
statements, Conditional statements, Looping statements, etc.
Script
A script usually contains a sequence of The assignment statement
statements. If there is more than one produces no output.
statement, the results appear one at a time as 5
the statements execute. x=5
For example, the script x+1
print(1)
x=2
print(x)
produces the output
1
2
Assignment Statement
In Python, programmers need not explicitly declare variables to reserve memory
space.

The declaration is done automatically when a value is assigned to the variable using
the equal sign (=).

The operand on the left side of equal sign is the name of the variable and the
operand on its right side is the value to be stored in that variable.
Assignment statements
An assignment statement creates a new variable and gives it a value:
>>> message = 'And now for something completely different'
>>> n = 17
>>> pi = 3.141592653589793
Example
Input Statement

To take input from the users, Python makes use of the input() function.

The input() function prompts the user to provide some information on which
the program can work and give the result.

However, we must always remember that the input function takes user’s input
as a string.
Example
Conditional Statement Looping Statement
site = ‘gmail'
if site == ‘gmail': j=1
print('Logging on to mail ') while(j<= 5):
else: print(j)
print('retype the URL.')
j=j+1
print('All set !')
Multi-Line Statements:
Statements in Python can be extended to one or more lines using parentheses
(), braces {}, square brackets [], semi-colon (;), continuation character slash (\).

When the programmer needs to do long calculations and cannot fit his
statements into one line, one can make use of these characters.
Examples
Declared using Continuation Character (\):
s=1+2+3+\
4+5+6+\
7+8+9

Declared using parentheses () :


n = (1 * 2 * 3 +
7 + 8 + 9)
Declared using square brackets [] :
footballer = ['MESSI',
'NEYMAR',
'SUAREZ']
Contd.,
Declared using braces {} :
x = {1 + 2 + 3 + 4 +
5 + 6 + 7 + 8 + 9}

Declared using semicolons(;) :


flag = 2; ropes = 3; pole = 4
Indentation
Whitespace at the beginning of the line is called indentation. These whitespaces or the
indentationare very important in Python. In a Python program, the leading whitespace
including spaces and tabs at the beginning of the logical line determines the indentation level
of that logical line.
Question1
You can use a minus sign to make a negative number like -2.
What happens if you put a plus sign before a number?
What about 2++2?
Question2
In math notation, leading zeros are okay, as in 02. What happens if you try this in Python?
Question 3
What happens if you have two values with no operator between them?
Question
What happens if you put a semicolon at the end of a Python
statement?

You might also like