0% found this document useful (0 votes)
4 views20 pages

Python Variables and Program Structure

Chapter 7 covers Python fundamentals including variable definitions, dynamic typing, and the structure of a Python program. It explains how to create and assign values to variables, the concept of input and output statements, and the significance of comments and functions. The chapter also discusses the differences between dynamic and static typing, as well as the representation of variables in memory.

Uploaded by

manishkumarmeher
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)
4 views20 pages

Python Variables and Program Structure

Chapter 7 covers Python fundamentals including variable definitions, dynamic typing, and the structure of a Python program. It explains how to create and assign values to variables, the concept of input and output statements, and the significance of comments and functions. The chapter also discusses the differences between dynamic and static typing, as well as the representation of variables in memory.

Uploaded by

manishkumarmeher
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

CHAPTER -7 PYTHON FUNDAMENTAL

Presenter
Manish Kumar Meher
Topic
 Variable
 Definition of variable
 Creating variable
 Assigning values to variable(single and multiple)
 Representation of value using variable in memory
 Dynamic typing
 Barebones of a python program(structure)
 Expression
 Statements
 Comments
 Function
 Blocks and indentation
 Input statement
 String input
 Integer input
 Floating point input
 Possible Errors during input of numeric values
 Output statement
 Print()
Variable Definition
A variable in Python is a label that refers to a value and whose values can
be used and processed during program run. It is not a storage container
just like other programming language. It refers to the value store in
memory address.
For instance, to store name of a student and mark of a student during a
program run, we require some labels to refer to these student and mark.
so that these can be distinguished easily.
Example :
std_nm =“Rahul” mark=70
Creation of variable
In python no need to declare variable before using it. We can simple assign the
value to a
variable as follow std_nm=“ Rahul” mark=70
The variable std_nm has contained the string value . So it is now a string type
variable.
The variable mark has contained the numeric value . So it is now a numeric type
variable.
So we come to the conclusion that python created the variable of the type similar to
the type value assigned.

std_nm=“ Rahul” and mark=70 means the values “Rahul” , ‘70’ are created first in
memory and then a tag by the name ‘mark’ is created to show that value as
std_nm Rahul

mark 70
Assigning values to variable(single and multiple)
1. LVALUES AND RVALUES
Ivalue : expressions that can come on the lhs (left hand side) of an assignment.
Rvalue : expressions that can come on the lhs (right hand side) of an
assignment.

a = 20
b = 10
20 = a ERROR
2. Single Value assignment:

A=10
S=“Rahul”
B=25.5
Multiple Assignment
1. Assigning same value to multiple variables:
You can assign same value to multiple variables in a single statement, e.g., a = b =c = 10
It will assign value 10 to all three variables a, b, c That is, all three labels a, b, c will refer to same
location with value 10.
2. Assigning multiple values to multiple variables:
You can even assign multiple values to multiple variables in single statement, e.g.
X, Y, Z = 10, 20, 30
It will assign the values order wise, i.e., first variable is given first value, second variable the
second
value and so on. That means, above statement will assign value 10 to x, 20 to y and 30 to z.
This style of assigning values is very useful and compact. For example, consider the code given
below :
X, Y = 25, 50 print (x, y)
It will print result as 25 50
Because x is having value 25 and y is having 50. Now, if you want to swap values of x and y. you
just need to write x, y =y , x
print(x, y)
50 25
Representation of value using variable in memory
Most programming language create variables as storage container s e.g
age=15
age=20
Firstly value 15 is assigned to variable age and then value 20 is assigned to
it
Python variable in memory
Python does this differently as compare to traditional programming
language. Let see how python will do it.
Python preloads some commonly used values before the identifier(variable is
created) in an area of memory we can refer to this area as front-loaded
dataspace.
This dataspace memory has literal/values at defined memory location has a
memory address.
14 15 16 17 18 19 20 21 22 23
20200 20208 202016 20232 20248 20252 20260 202270 202090 20295

When you give statement age=15, variable age will be created as a label
pointing to memory location where vale 15 is stored i.e as
14 15 16 17 18 19 20 21 22 23
20200 20208 202016 20232 20248 20252 20260 202270 202090 20295

. Age now referring to location 20208 that


has value 15
When you give the statement age =20, the label age will not having the same
location as earlier. It will now refers to value 20, which is at different location. i.e,

14 15 16 17 18 19 20 21 22 23
20200 20208 202016 20232 20248 20252 20260 202270 202090 20295

Age now referring to location 20260 that has


value 20 .
So this time memory location of variable age ‘s value is changed. Thus variable in
python do not have fixed location unlike other programming languages. The
location they refer to changes every time their value change. (Note: this rule is
applicable for immutable type only)
Dynamic typing
A variable pointing to a value of certain type ,can be made to point to a value/object of different
type. This is called Dynamic typing.
In python a variable is defined by assigning to it some value of a particular type such as numeric,
string etc. for example :
X=10
we can say that variable x is referring to a value 0f integer type. Later in your program, if you
reassign a value of some other type to variable x, python will not complain.
Ex:
x=10 With dynamic typing,
print(x) Python makes a label
X=‘Hello world”
Print(x)
refer to new value
the above program will gives the output as follow: with new assignment.
10
Hello world int : 10
x
x=10 int : 10
:
String : Hello
x= “hello world” x
world
Dynamic typing in Python variables.
Static Typing

In static typing, a data type is attached with a variable when it is


defined first and it is fixed. That is ,data type of a variable can not be
changed in static typing, where as there is no such restriction in
dynamic typing, which is supported by python.
Barebones of a python program
A python program may contain various elements such as comments,
statements, expressions etc.
# this program shows a program’s components.
Comments #Definition of function see you () f0llows
begin with Def seeyou():
Print( “ hello”) Function
#

#Main program code follows now


a=15
statements b=a-10

Print(a+3) Expressio
n
If b>5: # Colon means it’s a
print(“Value of ‘a’ was more than 15 initially”)
block Block
Indentatio
n see Else:
indented print(“Value of ‘a’ was more than 15 or less initially”)
Function
line
Seeyou()
Expression:

it is Any legal combination of symbols like variable ,operators, constant value etc that
represents a value. An Expression represents something, which python evaluates and which
than produces a value. Example: 15,,a-10, a+3, b>5.
Statement:
A statement is a programming instruction that does something i.e some action take place. Example: a=15 , b=a-
10, print(a+3), if b>5:
A statement executes and may or may not produce a value.
Comments:
Comments are additional readable information to clarify the source code, which is read by the programmers but
ignore by the python interpreter. Comments in python start with symbol # and end with end of the physical line.
in the above code you can see four comments.
i. Full line comment: The physical lines begin with # are full line comments. There are three full line comments in the
above program.
# this program shows a program’s components.
#Definition of function see you () f0llows
#Main program code follows now
ii. Inline comment: the fourth comment is called inline comment as it starts in the middle of a
physical line.
If b>5: # Colon means it’s a block
iii. Multiline Comment:
Comment in more than one line is called multi line comment. It can be
applied in two ways in python
i. Add a # symbol in the beginning of each physical line part of the
multiline comments, e.g
# Multiline comments are useful for detailed additional information.
# Related to the program in question.
# it helps clarify certain important things.
ii. Type comment as a triple-quoted multiline string e.g
‘‘‘ Multiline comments are useful for detailed additional information.
Related to the program in question.
it helps clarify certain important things. ’’’
OR
“ “ “ Multiline comments are useful for detailed additional
information.
Related to the program in question.
it helps clarify certain important things. ” ” ”
Function:
A function is a code that has a name and it can be reused by specifying its
name in program , where needed.
In the above program , there is one function name seeyou (). This function
has been started with the keyword def .
Seeyou () is the calling statement. It calls th seeyou () function.
Block and indentation:
A group of statements which are part of another statement or a function are
called block in python.
If b>5:
temp=a
Four space mark a=b Block
the next indent
b=temp
level

Python use indentation to create blocks of code. Statements at the same


indentation level are part of same block.
Input statement
How to Input a string:
Syntax:
Var=input(“ message”)
Example : str=input(“Enter a string”)
input() is used to input string value. Whatever type vale ,you will input using
input() function will be string type. The input function always return string type
value.
How to input Number(Integer and Float value) :
To input int and float type python offers two function int() and float() which are
used with input() function to convert string value into int and float types
respectively.
While entering numeric value
Syntax: through input along with
Var=int(input(‘enter a number’)) int()/float() make sure that you
Var= float(input(‘enter a number’)) enter values that are convertible
Example: to the target type otherwise
Python will raise an error.
a=int(input(‘enter a number’))
b= float(input(‘enter a number’))
Output: print()
The print() is used to produce output in python on a standard output device,
which is normally a monitor.
Syntax:
print(*object,[sep=‘ ’ or <separator-string>end=‘\n’ or <end string>]
1. *object means it can be one or multiple comma separated
objects(values/variables) to be
printed.
example: print(“sum of two number”, sum)
print(a, b, c)
sep:
[Link] you do not give value for sep, it insert space between items
automatically because the default value of sep argument
Output is space(‘
line has automatically ‘). inserted
spaces
in between them because default sep
Example: print(“ my”, “name”, “is” , “Amit.”)
character is a space.
my name is Amit.

In syntax elements in square bracket means that they are


2. The sep argument specifies the separator character. You can change the
value of separator character with sep argument of print().
Example: print(“my”, “name”, “is”, “Amit.” , sep=‘….’)
output: my….name….is….Amit.
This time print() separated the
items with given sep character,
which is ‘….’ .

3. It appends a new line character at the end of the lines if you do not give
your own end of line.
Example: print( “ My name is Amit.”)
print(“I am 16 years old”)A print () function without any
output: My name is Amit. value or name or expression
prints a blank line.
I am 16 years old.

Python automatically added a newline(‘\n’)


character in the end of a line printed so that
the next print() prints from the next line.
4. If you explicitly give an end argument with a print() function then the
print() will print the line and end it with the string specified with the end
argument.
Example:
print(“My name is Amit.” , end=‘$’)
print(“I am 16yers old .”)
output: My name is Amit. $ I am 16yers old.

So the end argument determines the end character that will be printed at
the end of print line.
a , b,c=20, 30,40 In print() function, the default value
print(“a=”, a, end=‘ ’) of end argument is newline
character(‘\n’) and for sep
print(“b=”, b)
argument , it is space(‘ ‘)
print(“c=”, c)
output: a=20 b=30
c=40 This space is because of end=‘ ‘
in print()

This new line is because by default


print() function takes (‘\n) newline

You might also like