Lecture02 Variables Datatypes
Lecture02 Variables Datatypes
operators
SE 113 – Introduction To Programming
Constants
Values and types
Variables
Variable names and keywords
Statements
Operators and operands
Expressions
Order of operations
Modulus operatör
String operations
Asking the user for input
Comments
mnemonic variable names
Constants
>>> print(123)
123
>>> print(98.6)
98.6
>>> print('Hello world')
Hello world
Values and types
A value is one of the basic things a program works with, like a letter or a number.
These values belong to different types:
2 is a number (integer), and
“Hello, World!” is a string, so called because it contains a “string” of letters.
The interpreter can identify strings because they are enclosed in quotation marks.
One of the most powerful features of a programming language is the ability to manipulate variables.
A variable is a named place in the memory where a programmer can store data and later retrieve the data using the
variable “name”
A variable is a name that refers to a value.
An assignment statement creates new variables and gives them values:
Programmers generally choose names for their variables that are meaningful and document what the variable
is used for.
You can change the contents of a variable in a later statement
Variable names can be arbitrarily long.
Case Sensitive
They can contain both letters and numbers, but they cannot start with a number.
It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter.
The underscore character ( _ ) can appear in a name.
It is often used in names with multiple words, such as my_name or airspeed_of_unladen_swallow.
Variable names can start with an underscore character, but we generally avoid doing this
unless we are writing library code for others to use.
Variable names and keywords
When you type a statement in interactive mode, the interpreter executes it and displays the result, if there is
one.
A script usually contains a sequence of statements.
If there is more than one statement, the results appear one at a time as the statements execute.
For example, the script Produces the output
Operators are special symbols that represent computations like addition and multiplication.
The values the operator is applied to are called operands.
The operators +, -, *, /, and ** perform addition, subtraction, multiplication, division, and exponentiation,
as in the following examples:
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
% Remainder
Operators and operands
If you type an expression in interactive mode, the interpreter evaluates it and displays the result:
Type the following statements in the Python interpreter to see what they do:
Order of operations
When more than one operator appears in an expression, the order of evaluation depends on the rules of
precedence.
For mathematical operators, Python follows mathematical convention.
The acronym PEMDAS is a useful way to remember the rules:
Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want.
Since expressions in parentheses are evaluated first.
Exponentiation has the next highest precedence
Multiplication and Division have the same precedence, which is higher than Addition and Subtraction
Operators with the same precedence are evaluated from left to right.
When in doubt, always put parentheses in your expressions to make sure the computations
are performed in the order you intend
Order of operations
x = 1 + 2 * 3 - 4 / 5 ** 6
Modulus operatör
The modulus operator works on integers and yields the remainder when the first operand is divided by the
second.
In Python, the modulus operator is a percent sign (%).
The + operator works with strings, but it is not addition in the mathematical sense.
Instead it performs concatenation, which means joining the strings by linking them end to end.
For example:
String operations
The * operator also works with strings by multiplying the content of a string by an integer.
For example:
Asking the user for input
Sometimes we would like to take the value for a variable from the user via their keyboard.
Python provides a built-in function called input that gets input from the keyboard
Before getting input from the user, it is a good idea to print a prompt telling the user what to input.
You can pass a string to input to be displayed to the user before pausing for input:
As programs get bigger and more complicated, they get more di fficult to read.
Formal languages are dense, and it is often di fficult to look at a piece of code and figure out what it is doing,
or why.
For this reason, it is a good idea to add notes to your programs to explain in natural language what the
program is doing.
Everything from the # to the end of the line is ignored; it has no e ffect on the program.
These notes are called comments, and in Python they start with the # symbol:
Comments
Comments are most useful when they document non-obvious features of the code.
It is reasonable to assume that the reader can figure out what the code does; it is much more useful to explain
why.
As long as you follow the simple rules of variable naming, and avoid reserved words, you have a lot of
choice when you name your variables.
In the beginning, this choice can be confusing both when you read a program and when you write your own
programs.
For example, the following three programs are identical in terms of what they accomplish, but very di fferent
when you read them and try to understand them.
The Python interpreter sees all three of these programs as exactly the same but humans see
and understand these programs quite differently.
mnemonic variable names
The Python interpreter sees all three of these programs as exactly the same but humans see and understand
these programs quite differently.
Humans will most quickly understand the intent of the second program because the programmer has chosen
variable names that reflect their intent regarding what data will be stored in each variable.
mnemonic variable names
Sum up, We should choose mnemonic variable names to help us remember why we created the variable in
the first place.
Exercises
Write a program that uses input to prompt a user for their name and then welcomes them.
Write a program to prompt the user for hours and rate per hour to compute gross pay.
Write a program which prompts the user for a Celsius tem-perature, convert the temperature to Fahrenheit,
and print out the converted temperature.
Please read chapter 2 from the book
Thank you for listening…