You are on page 1of 21

Variables and Simple

Data Types

LECTURE 03
CSC011 - Programming for Scientific Computing
Jennifer Joyce M. Montemayor


Department of Computer Science
School of Computer Studies

MSU - Iligan Institute of Technology
Values and Data Types
‣ a value is one of the fundamental things that a program
manipulates

‣ values are classified into different classes or data types

Basic data types:

data type class description

integer int whole numbers

float float real numbers

sequence of characters enclosed in quotation


string str marks

2 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Variable
‣ a name that refers to a value

‣ every variable holds a value which is the information


associated with that variable

‣ a powerful feature of a programming language is the


ability to manipulate variables

‣ are references to Python objects (all are objects in


Python)

3 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Variable
‣ created using the assignment statement which uses the assignment
token, =

‣ read it as “n gets the value 17” or “n is assigned 17”

‣ you should not read it as “n equals 17”

‣ used in programs to “remember” things

‣ its values can change as the program executes


4 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01
Variable names
‣ Variable names can contain only letters, numbers, and underscores.
They can start with a letter or an underscore, but not with a number. For
instance, you can call a variable message_1 but not 1_message.

‣ Spaces are not allowed in variable names, but underscores can be


used to separate words in variable names. For example,
greeting_message works, but greeting message will cause errors.

‣ Avoid using Python keywords and function names as variable names;


that is, do not use words that Python has reserved for a particular
programmatic purpose

‣ Variable names should be short but descriptive. For example, name is


better than n, student_name is better than s_n, and name_length is
better than length_of_persons_name.

‣ Be careful when using the lowercase letter l and the uppercase letter O
because they could be confused with the numbers 1 and 0.
5 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01
keywords
‣ cannot be used as variable names

‣ has special meaning in the language syntax rules and structure

6 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Which of the following are valid variable names?

1Letter
max_entries
TWO*FOUR
_variable2
joe’s
float
xYZ123
part#2
XyZ123
this_is_a_long_one
print

7 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Which of the following are valid variable names?

1Letter invalid, cannot begin with a digit

max_entries valid
TWO*FOUR invalid, char * is not allowed
_variable2 valid
joe’s invalid, char ‘ is not allowed
float invalid, keyword in Python
xYZ123 valid
part#2 invalid, char # is not allowed
XyZ123 valid
this_is_a_long_one valid
print invalid, function in Python

8 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Which of the following are valid variable names?

4score
color12
CONST_TIME

helloworld
two!s

for
elif
@twitter

Gender

_x2y1z0

9 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Which of the following are valid variable names?

4score invalid, cannot begin with a digit

color12 valid
CONST_TIME valid

helloworld valid
two!s invalid, char ! is not allowed

for invalid, keyword in Python


elif invalid, keyword in Python
@twitter invalid, char @ is not allowed

Gender valid

_x2y1z0 valid

10 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Activity:

Write a separate program to accomplish each of these


exercises. Save each program with a filename that follows
standard Python conventions, using lowercase letters and
underscores.

1. Store a message in a variable, and then print that


message. 

(Save it as: simple_message.py)

2. Store a message in a variable, and print that


message.Then change the value of your variable to a
new message, and print the new message. 

(Save it as: simple_messages.py)
11 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01
statement
‣ an instruction that the Python interpreter can execute

‣ does not produce any result

e.g assignment statement, if statement, while statement

expression
‣ a combination of values, variables, operators, and calls to
functions

‣ evaluation of an expression produces a value

‣ a value all by itself is a simple expression, so is a variable


12 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01
Operators and operands
‣ operators are special tokens that represent computations

‣ operands are the values the operator uses

Sample operators:

operator operation
+ addition
- subtraction
* multiplication
** exponentiation
/ division
// floor division
% modulo

13 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Type converter functions
int function

‣ can take a floating point number, a string, an integer and turn it into
an int

‣ discards the decimal portion of the number - truncation towards zero

14 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Type converter functions
float function

‣ can take an integer, a float, or a syntactically legal string into a float

‣ discards the decimal portion of the number - truncation towards zero

15 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Type converter functions
str function

‣ turns its argument into a string

16 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Order of operations
Rules for Evaluating Expressions

1. Parentheses rule

‣ all expressions in parentheses must be evaluated separately.

‣ nested parenthesized expressions must be evaluated from inside out, with the innermost
expression evaluated first

2. Operator precedence rule

‣ operators in the same expression are evaluated in the following order:

unary +,-, ** first


* , /, % next
binary +, - last

3. Associativity rule

‣ Unary operators in the same subexpression and at the same precedence level are
evaluated right to left (right associativity)

‣ Binary operators in the same subexpression and at the same precedence level are
evaluated left to right (left associativity)
17 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01
Operations on strings
In general, you are not allowed to perform mathematical operations on
strings even if the strings look like numbers

+ token works with strings as the concatenation operator, that joins


two operands by appending the second operand to the first operand

* token works with strings as the repetition operator, given that one
operand is a string and the other operand is an integer

18 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Function input
A built-in function in Python for getting user input:

The user of the program can provide the name and hit “enter”
key to signal the interpreter that the value provided by the user
can now be assigned to variable name

19 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Comment
‣ provide supplementary information to better understand the
program

‣ serve as inline documentation

‣ no effect on the execution of the program

hello = “Hello!” #this is inline comment



print(hello)

‘’’

this is a block / multiple line

comment

‘’’


20 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01


Composition
Now that we know how to do the following,
✓ get user to provider some input

✓ convert string into another type

✓ print values

✓ compose complex expressions

Let us put these together to write a program that will as the


user to input a value for the radius of a circle, and then
computes the area of the circle given the formula below:

21 Jennifer Joyce M. Montemayor / CCC011 / Lecture 01

You might also like