0% found this document useful (0 votes)
26 views

Lecture02 Variables Datatypes

This document provides an overview of fundamental programming concepts like constants, variables, data types, operators, and expressions in Python. It defines constants as fixed values like numbers and strings. Variables are named locations that store and reference values. Values have types like integers, floats, and strings. Operators perform computations on operands. Expressions combine values and operators. Comments help explain code.

Uploaded by

Ramazan Karahan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lecture02 Variables Datatypes

This document provides an overview of fundamental programming concepts like constants, variables, data types, operators, and expressions in Python. It defines constants as fixed values like numbers and strings. Variables are named locations that store and reference values. Values have types like integers, floats, and strings. Operators perform computations on operands. Expressions combine values and operators. Comments help explain code.

Uploaded by

Ramazan Karahan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Fundamental data types, constants, variables,

operators
SE 113 – Introduction To Programming

Assoc. Prof. Dr. Tarık Kışla


Contents (Chapter 2)

 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

 Contants 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 tell us what is the type 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 values like “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

 In typing a large integer,


 we shouldn’t use commas or 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 first 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 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

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:
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 is 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 floating point result.

 To obtain the truncated answer in Python 3 use floored ( // 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 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 (%).

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

\n for new line.


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

 All input from the users are string.


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

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 accomplish, but very di fferent
when you read them and try to understand them.

(“mnemonic” = “memory aid”)

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…

You might also like