Variables, Expression and Statements
Variables, Expression and Statements
y.
ud
st
ty
si
er
Variables, Keywords, Expressions,
iv
un
Statements
Program
in
•Basic instructions in almost every program:
y.
ud
–Input: Get data from keyboard, or file or some other device.
st
–Output: Display data on the screen or send data to a file or other device.
ty
si
–Math: Perform basic mathematical operations like addition and multiplication.
er
–Conditional Execution: Check for certain conditions and execute the appropriate
iv
sequence of statements. un
–Repetition: Perform some action repeatedly , usually with some variation
Debugging
in
• Process of tracking bugs and correct them is called debugging.
y.
ud
Types of Errors
st
ty
• Syntax Errors
si
er
• Run Time Errors (Exceptions)
iv
un
• Semantic Errors
Formal and Natural Languages
in
English, Spanish, French. They were not designed by people; they
y.
ud
evolved naturally.
st
• Formal Languages are languages that are designed by people for
ty
specific applications.
si
er
• Programming languages are formal languages that have been
iv
un
designed to express computations.
• Parsing is a process to figure out what the structure of the sentence
is.
Difference between Natural and Formal Languages
Ambiguity: Natural languages are full of ambiguity.
in
y.
Formal languages are designed to be nearly or completely
ud
unambiguous.
st
ty
Redundancy: Natural languages are more redundant as compared to
si
formal languages.
er
iv
Literalness: Natural languages are full of idiom and metaphor. If I say,
un
“The other shoe fell," there is probably no shoe and nothing
falling. Formal languages mean exactly what they say.
Variable Expressions and Statements
• Value is a one of the fundamental thing, that a program manipulates.
• Variable is a name that refers to a value.
in
y.
• The assignment statement creates new variables and gives them
ud
values:
st
ty
• >>> message = "What's up, Doc?"
si
er
• >>> n = 17
iv
un
• >>> pi = 3.14159
• A common way to represent variables on paper is to write the name
with an arrow pointing to the variable's value. This kind of
representation is called a state diagram.
Variable
• Variable names must be meaningful.
• It contains both numbers and letters, but they have to begin with a
in
y.
letter.
ud
• Case sensitive.
st
ty
• The underscore (_) character can appear in a name.
si
er
• Eg.
iv
un
– >>>76trombones = "big parade“ >>> more$ = 1000000
– SyntaxError: invalid syntax SyntaxError: invalid syntax
– >>> class = "Computer Science 101“
– SyntaxError: invalid syntax
Keywords
• Keywords define the language’s rules and structure and they can’t be
in
used as variable names.
y.
• Python has twenty-nine keywords:
ud
st
and def exec if not return
ty
si
assert del finally import or try
er
iv
break elif un for in pass while
class else from is print yield
continue except global lambda raise
Statements
in
• When you type a statement on the command line, Python executes it
y.
ud
and displays the result, if there is one.
st
• A script usually contains a sequence of statements. If there is more
ty
si
than one statement, the results appear one at a time as the
er
statements execute.
iv
un
Evaluating Expressions
in
• If you type an expression on the command line, the interpreter
y.
ud
evaluates it and displays the result:
st
– >>> 1 + 1
ty
si
– 2
er
iv
• A value all by itself is considered an expression, and so is a variable.
un
>>> 17 >>> x
17 2
Operators and Operands
in
addition and multiplication.
y.
ud
• The values the operator uses are called operands
st
• When both of the operands are integers, the result must also be an
ty
si
integer, and by convention, integer division always rounds down.
er
iv
• +, -, *, /, %, **, // un
Order of Operations
• When more than one operator appears in an expression, the order of
evaluation depends on the rules of precedence.
in
• The acronym PEMDAS is a useful way to remember the order of
y.
ud
operations:
st
– Parentheses have the highest precedence and can be used to force
ty
an expression to evaluate in the order you want.
si
er
– Exponentiation has the next highest precedence.
iv
un
– Multiplication and Division have the same precedence, which is
higher than Addition and Subtraction, which also have the same
precedence.
– Operators with the same precedence are evaluated from left to right.
Operations on Strings
• Mathematical operations can’t be performed on strings, even if the
strings look like numbers.
in
• the + operator represents concatenation.
y.
ud
• For Example:
st
ty
– fruit = "banana"
si
– bakedGood = " nut bread“
er
iv
– print fruit + bakedGood
un
• The * operator also works on strings; it performs repetition.
• For eg:
– "Fun"*3 is "FunFunFun"
Composition
• One of the most useful features of programming languages is their
ability to take small building blocks and compose them
in
Comments
y.
ud
• It is a good idea to add notes to your programs to explain in natural
st
ty
language what the program is doing. These notes are called comments.
si
• they are marked with the # symbol:
er
•For eg:
iv
un
# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60
un
iv
er
si
ty
st
ud
y.
in
Questions ??