Professional Documents
Culture Documents
Python Programming
CT108-3-1-PYP
TOPIC LEARNING OUTCOMES
• Variables in python
• Rules for naming variables
• Expressions and statements
• Type conversion
Variable names can be a group of both the letters and digits but must begin
with a letter or an underscore.
Python – does not have to declare a variable before using it in the application.
When assigning any value to the variable, the variable is declared automatically.
01 02 03 04 05
The first All the character except
the first character may
Identifier name Identifier name Identifier name are case
sensitive for example :-
character of be an alphabet of lower
must not use any must not be similar
• NAME and name are
variable must be case like(a-z),upper case white space and any keyword the different name not
an alphabet or like(A-Z) and they use special character a same variable
the digits(0-9) (!,@,#,%)
underscore(_)
• Global
– Global variables can be used throughout the program, and its scope is in the entire program.
– Global variables can be used inside or outside the function.
– A variable declared outside the function is the global variable by default.
– Python provides the global keyword to use global variable inside the function. If the
global keyword were not used, the function treats it as a local variable
• Local
• Variables that declared inside the function and have scope within the function
def add():
a=20
b=10
c=a+b
print("The sum is ", c)
add()
a = 50
def add():
global a
b=10
c=a+b
print("The sum is ", c)
add()
print(a)
It is helpful to think of constants as containers that hold information which cannot be changed
later.
Refer to names associated with values that never change during a program’s execution.
Fixed values such as numbers, letters, and strings are called “constants” - because their value
does not change.
Constants are usually declared and assigned in a module
• Variable can hold value and every value has a data type
• No need to define the type of the variable while declaring
– Example:- a=5
– The variable hold the integer value five
• User can not define any Data Type
• Python interpreter will automatically give an integer Data Type to the variable
• Python provide the type () function to check the data type any variable
• The basic types are:
Python keywords are special reserved words that convey a special meaning to
the compiler/interpreter.
x = 3.9 * x * ( 1 - x )
x = 0.6
x = 3.9 * x * ( 1 - x )
Exponentiation:
2**3 is 8
2.5**2 is 6.25
Python can also manipulate real Examples: 6.022 -15.9997 42.0 2.143e17
numbers.
The operators + - * / % ** ( ) all work The / produces an exact answer: 15.0 / 2.0 is 7.5
The same rules of precedence also apply to real numbers:
for real numbers. Evaluate ( ) before * / % before + -
When we write
Which operator “takes
operators together - This is called
precedence” over the
Python must know “operator precedence”
others
which one to do first
x = 1 + 2 * 3 - 4 / 5 ** 6
vs
When writing code - keep mathematical expressions simple enough that they are easy to
understand
False
True True
– a = 10
– b=a*5
– c = "Your result is: "
– print(c, b)
a = 10
b=a
c = "Your result is: "
print(b)
a = 10
b=a
c = "Your result is: "
print(b)
It will print out 10. When you set one variable equal to another, they don’t become linked; b
is set to 10 and no longer has anything else to do with a.
• Without using any Python IDLE, write on a piece of paper or any text editor tools a
python program that asks the user for two numbers and prints out the average.
• Pretend that you’re writing a program to compute someone’s weighted grade. You have so
far:
– hwWeight = 0.4
– examWeight = 0.5
– discussionWeight = 0.1
• Write a program starting with these three lines that asks the user for their homework grade,
exam grades, and discussion grades and prints out their total grade in the class.
• Variables in python
• Rules for naming variables
• Expressions and statements
• Type conversion