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

Lecture02 Variables DataTypes

Chapter 2 of SE 113 covers fundamental programming concepts including constants, variables, operators, and expressions. It explains the importance of variable naming, the use of comments for code clarity, and the significance of data types such as integers, floats, and strings. Additionally, it introduces user input handling and provides exercises for practical application of the concepts discussed.

Uploaded by

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

Lecture02 Variables DataTypes

Chapter 2 of SE 113 covers fundamental programming concepts including constants, variables, operators, and expressions. It explains the importance of variable naming, the use of comments for code clarity, and the significance of data types such as integers, floats, and strings. Additionally, it introduces user input handling and provides exercises for practical application of the concepts discussed.

Uploaded by

omeburak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Fundamental data types, constants,

variables, operators
SE 113 – Introduction To Programming
Contents (Chapter 2)

❑ Constants
❑ Values and types
❑ Variables
❑ Variable names and keywords
❑ Statements
❑ Operators and operands
❑ Expressions
❑ Order of operations
❑ Modulus operator
❑ String operations
❑ Asking the user for input
❑ Comments
❑ mnemonic variable names
Constants

❑ Constants are fixed values such as numbers, letters, and strings,


❑ Their value does not change
❑ String constants use single quotes (') or double quotes (")

>>> 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.

❑ The print statement also works for integers.


❑ print(4)
Values and types

❑ Type function tells us what is the type of a value


>>> type(‘Hello, World!’)
<class ‘str’>
>>> type(17)
<class ‘int’>
❑ strings belong to the type str and integers belong to the type int.
❑ Numbers with a decimal point belong to a type called float
>>> type(3.2)
<class ‘float’>
Values and types

❑ What about “17” and “3.2”?


❑ In this values we use quotation marks
>>> type(’17’)
<class ‘str’>
>>> type(‘3.2’)
<class ‘str’>
They are strings
Values and types

❑ For typing the large integers,


❑ we shouldn’t use commas or a point between groups of three digits, as in 1,000,000.
❑ This is not a legal integer in Python.
Variables

❑ 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:

This example makes three assignments.


the rst assigns a string to a new variable named message;
the second assigns the integer 17 to n;
the third assigns the (approximate) value of π to pi.
Variable names and keywords

❑ Programmers generally choose names for their variables that are meaningful, hence they can
track 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 / good practice 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

x = 12.2 x 12.2 100


y = 14
x = 100
y 14
Variable names and keywords

❑ If you give a variable an illegal name, you get a syntax error:

76trombones is illegal because it begins with a number.


more@ is illegal because it contains an illegal character, @.

What about variable name class?


Variable names and keywords

❑ It turns out that class is one of Python’s keywords.


❑ The interpreter uses keywords to recognize the structure of the program, and they cannot be used
as variable names.
❑ Python reserves 35 keywords.
❑ Let’s remember them from last week:
Statements

❑ A statement is a unit of code that the Python interpreter can execute.


❑ Until now, we have seen two kinds of statements:
❑ ‘print’ expression statement
❑ Assignment statements
Statements

❑ 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 are more than one statement, the results appear one at a time as the statements
execute.
❑ For example, the script Produces the output

The assignment statement produces no output


Operators and operands

❑ 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

❑ In Python 3, the result of this division is a oating point result.

❑ To obtain the truncated answer in Python 3, use oored ( // integer) division.


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:

❑ 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 rst.

❑ 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 operator

❑ The modulus operator works on integers and yields the remainder when the rst operand is
divided by the second.
❑ In Python, the modulus operator is a percent sign % .

With using modulus,


we can find the right-most digit of x: (x % 10) we can
find the last two digits: (x % 100)
String operations

❑ 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 reads an 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:

\n for new line.


‘\n’ is a special character that causes a line break.
Asking the user for input

❑ The input reads everything as string type.


❑ If you expect the user to type an integer, you can try to convert the return value to int using the
int() function

if the user types something other than


a string of digits, you get an error
Comments

❑ As programs get bigger and more complicated, they become more difficult to read.
❑ Formal languages are dense, and it is often difficult to look at a piece of code and gure
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 starting from the # symbol to the end of the line is ignored; it has no effect 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 gure out what the code does; it is much
more useful to explain why.

Good variable names can reduce the need for comments,


but long names can make complex expressions hard to read.
mnemonic variable names

❑ 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 do, but they
are very different in terms of readability and understandability.

(“mnemonic” = “memory aid”)


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 third program because the
programmer has chosen variable names that reect their intent regarding what data will
be stored in each variable.
mnemonic variable names

❑ To sum up, we should choose mnemonic variable names to help us remember why we
created that variable in the rst 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 to enter hours and a rate per hour to compute a
worker's wage.

❑ Write a program which prompts the user to enter a Celsius temperature value and convert
the temperature to Fahrenheit. Print out the converted temperature.
❑ Please read chapter 2 from the book ☺
Thank you for listening…

You might also like