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

4b - Programming in Python - Constants and Variables

This document provides an introduction to programming in Python, focusing on literal constants, variables, and operators. It explains the concepts of strings, escape sequences, and the rules for naming identifiers, along with practical activities to reinforce learning. Additionally, it covers the creation of strings using variables and the declaration of multiple variables in a single statement.
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)
2 views

4b - Programming in Python - Constants and Variables

This document provides an introduction to programming in Python, focusing on literal constants, variables, and operators. It explains the concepts of strings, escape sequences, and the rules for naming identifiers, along with practical activities to reinforce learning. Additionally, it covers the creation of strings using variables and the declaration of multiple variables in a single statement.
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
You are on page 1/ 37

PROGRAMMING IN

PYTHON
Literal Constants and Variables

© H. Chiname 2023. Version 05


INTRODUCTION
 The previous lecture introduced you to the steps that
are required to create, save and run a Python program.
 This lecture introduces further aspects of Python, on:
 Literal constants;
 Variables; and
 An introduction to operators;
 By the end of the lecture you should understand most of
the basic Python rules, excluding of course, rules that
apply to the building blocks (sequence, selection and
the loop).
© H. Chiname 2023. Version 05
LITERAL CONSTANTS
 Literal constants are parts of a programme that do not
change in their value.
 Literal constants can be strings or numerals.
 They are the opposite of variables.

© H. Chiname 2023. Version 05


STRINGS
 Strings can be text enclosed in quotes.
 Numerals can be converted into strings by enclosing
them within quotes.
 There three types of quotes that are recognised in
Python:
 single quotes(‘);
 double quotes (“); or
 triple quotes (’’’) or (”””).
 Triple quote are used for multiline strings.

© H. Chiname 2023. Version 05


ACTIVITY 1: STRINGS
 Print the following string in Python using a single print
instruction:

 This is a multi-line string.


 This is the second line.
 "Don’t complain" he said on WhatsApp. "If you do, I will
resend these strings."

© H. Chiname 2023. Version 05


ACTIVITY SOLUTION 1: STRINGS

Print the following string in Python using a single print


instruction:

 Save this activity, we will use it in the next activity.


© H. Chiname 2023. Version 05
ESCAPE SEQUENCES

 The backslash (\) before a quote makes Python ignore


quotes as a mark for the beginning or end of a
sentence.
 \n - denotes a new line of the same string
 \ - denotes the same line of the same string
continued in a new line
 \t - denotes a tab
 \\ - denotes a backslash in a string

© H. Chiname 2023. Version 05


ACTIVITY 2: USING ESCAPE SEQUENCES
 Demonstrate the knowledge that you have just acquired
on use of escape sequences by using the escape
sequences that you have learnt up to now to
manipulate the program that you created in the
previous activity.

© H. Chiname 2023. Version 05


ACTIVITY SOLUTION 2: STRINGS

Well done if you came up with something like this:

© H. Chiname 2023. Version 05


ACTIVITY SOLUTION 2: STRINGS
 Type and run the following strings in Python:

 Take note of the effect of the escape sequences on the strings.

© H. Chiname 2023. Version 05


RAW STRINGS
 To make python ignore the escape sequence effect of
the backslash, create a raw string by prefixing “r” to a
string, for example:

 Take note of the output once you have run the code.

© H. Chiname 2023. Version 05


OPERATORS
 These are the known mathematical operators such as:
 + - ÷ × =
 [÷] is represented by [/]
 [×] is represented by [*]

© H. Chiname 2023. Version 05


ACTIVITY 3 :OPERATORS AND
STRINGS
 Type each of the following print statements (one
statement at a time) and take note of the output:
 print(“match” + “box”)
 print(“match” “box”)
 print(“Hello” * 5)
 print(“cat” * “dog”)

 What programming lessons on strings do you derive


from the above?
© H. Chiname 2023. Version 05
ACTIVITY 3 :OPERATORS AND
STRINGS
 For example:

© H. Chiname 2023. Version 05


SUMMARY ON STRINGS
 Strings can be text.
 Numerals can be converted into strings.
 Strings are represented by the use of quotes.
 It can be single quotes, double quotes or triple quotes.
 Strings can be added.
 Two or more strings can be joined together to form one
string (using the + operator or the space character).
This is technically referred to as CONCATENATION.
 Strings cannot be multiplied or divided by each other,
and they cannot be subtracted from each other.
© H. Chiname 2023. Version 05
VARIABLES

 Variables are the opposite of literal constants.


 Variables can change in value.
 They are constructed using identifiers and data types.
 The identifier is the name of the variable.
 The data type is the value of the variable, basically
numbers or strings.
 There are special rules for naming identifiers:

© H. Chiname 2023. Version 05


VARIABLES

1. The identifier should be one continuous word (or grouping


of one or more characters), without spaces in between,
and should not be in quotes.
2. The first character of the identifier can only be a Unicode
alphabetic character (uppercase or lowercase), or an
underscore (_).
3. The rest of the characters can be a Unicode alphabetic
character, an underscore, or a digit.
4. The identifier is case sensitive, so once defined, the same
case should be maintained whenever referring to the
identifier.
 Although not a requirement or a rule, it makes life easier to
use an identifier that is descriptive of the variable.
© H. Chiname 2023. Version 05
ACTIVITY 4: IDENTIFIERS
 Pick the valid Python identifiers from the following list:
“FinMaths” JSE_Fin Y2K
_index ZSE-IND 2boy
€uro βeta Dow Jones
corpfin2 ∑ Hakuna_Matata
©2016 Jensenα -ALSI

© H. Chiname 2023. Version 05


ACTIVITY 4 SOLUTION: IDENTIFIERS
 If you are not sure which of the potential identifiers in
the above activity is valid, then try to use each of the
words as a variable and see if it gives an error or not.

© H. Chiname 2023. Version 05


ACTIVITY 5: DECLARING VARIABLES
 Create the following program:

 After running, change your program as follows:

 What do you learn from the above?


© H. Chiname 2023. Version 05
COMPOSITION OF A VARIABLE

 A valid variable is composed of the following


components:
 An identifier – which is the name of the variable;
 An assignment operator – which is the equals sign
that assigns a value to a variable (the one that
appears between the identifier and the data type);
A data type – which is the value assigned to a
variable. The value can be a integer, a float, a string,
or some other valid type in Python.

© H. Chiname 2023. Version 05


ACTIVITY 5 SOLUTION: DECLARING VARIABLES

 Variables should first be declared before they are used.

© H. Chiname 2023. Version 05


ACTIVITY 6: PROPERTIES OF VARIABLES

© H. Chiname 2023. Version 05


ACTIVITY 6 SOLUTION: PROPERTIES OF VARIABLES

 Study the program output from this activity.


 Which programming rules (lessons) do you derive from
the Activity on the use of the variables in Python?

© H. Chiname 2023. Version 05


A GENERAL COMMENT ON PYTHON

 Python is strongly object-oriented.


 This means it refers to anything and everything used in
a program as an object, including:
 numbers;
 strings; and
 functions
 Variables can be used directly in a string or in
conjunction with the format method to create a string.

© H. Chiname 2023. Version 05


DECLARING MULTIPLE VARIABLES IN A
SINGLE STATEMENT
 Python is versatile enough to allow for the declaration
of more than a single variable in a single statement.
 The following example illustrates this capability.

© H. Chiname 2023. Version 05


EXAMPLE: DECLARING MULTIPLE VARIABLES
IN A SINGLE STATEMENT

 You should get the following output:

© H. Chiname 2023. Version 05


COMMENT ON DECLARING MULTIPLE
VARIABLES IN A SINGLE STATEMENT
 Did you notice that the three identifiers are separated
by commas?
 Did you also notice that in addition to the commas, the
values are also in parenthesis?
 The order of the identifiers and the values attached to
them matters in the declaration line.

© H. Chiname 2023. Version 05


EXAMPLE: CREATING STRINGS USING VARIABLES
THE DIRECT METHOD

 COMMENT
 This is referred to as the direct method.
 Take note of the use of the commas and quotes

© H. Chiname 2023. Version 05


EXAMPLE: CREATING STRINGS USING VARIABLES
THE FORMAT METHOD

 COMMENT
• This is referred to as the format method because it makes use of
the format function.
• Take note of the use of curly braces.

© H. Chiname 2023. Version 05


ACTIVITY 7:
CREATING A STRING USING VARIABLES
Create variables out of the following two data
types:
117 and ALSI.
Once you have created the variables, use them
to create a string that will print as:
The ALSI is 117 today on the ZSE
i. Using the direct method
ii. Using the format method

© H. Chiname 2023. Version 05


ACTIVITY 7 SOLUTION: CREATING A STRING
USING VARIABLES

The Output
The ALSI is 117 today on the ZSE

© H. Chiname 2023. Version 05


ACTIVITY 8: CREATING AN ADDING PROGRAM

Create program that adds two values and prints


out the sum.

© H. Chiname 2023. Version 05


ACTIVITY 8 SOLUTION:
CREATING AN ADDING PROGRAM

© H. Chiname 2023. Version 05


EXERCISES
• To get some practice on the material
covered do far, try out the following
exercises.

© H. Chiname 2023. Version 05


EXERCISE A
• You intend to create one of the following
two programs:
– The first program calculates the present values
and future value of various cash flows.
– The second program calculates duty on various
imported goods.
• For one of the two programs, determine
some names of variables that might be
required in your program.
• The program that you choose will be
determined by your study specialty.

© H. Chiname 2023. Version 05


EXERCISE B

• Create a program that calculates the


required return of a counter listed on the
ZSE using the capital asset pricing model
(CAPM).
• K = Kr + (Km * B) + e
• Where:
• K = required return of the asset
• Kr = risk-free rate of return
• Km = market rate of return
• B – the beta of the asset
• e = error term
© H. Chiname 2023. Version 05

You might also like