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

Module 7 Python-Identifiers

This document provides an introduction to fundamental Python concepts like identifiers, data types, variables, constants, and operators. It explains that identifiers are names given to entities like variables and functions. There are specific rules for writing valid identifiers in Python. The document also outlines the different built-in data types in Python including strings, integers, floats, lists, tuples, dictionaries, booleans, and others. It describes how to create and assign variables of different data types. Finally, it covers the various types of operators in Python like arithmetic, comparison, logical, and bitwise operators. The overall objective is for students to understand these core building blocks of Python programming.

Uploaded by

ILEENVIRUS
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)
19 views

Module 7 Python-Identifiers

This document provides an introduction to fundamental Python concepts like identifiers, data types, variables, constants, and operators. It explains that identifiers are names given to entities like variables and functions. There are specific rules for writing valid identifiers in Python. The document also outlines the different built-in data types in Python including strings, integers, floats, lists, tuples, dictionaries, booleans, and others. It describes how to create and assign variables of different data types. Finally, it covers the various types of operators in Python like arithmetic, comparison, logical, and bitwise operators. The overall objective is for students to understand these core building blocks of Python programming.

Uploaded by

ILEENVIRUS
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/ 43

COMPUTER FUNDAMENTALS AND

PROGRAMMING
PYTHON: Identifiers, Data type, Variables, Constants,
Operators, Assignment statements and expressions

A.Y. 2023 - 2024


FIRST SEMESTER
University of Science and Technology of Southern Philippines
OBJECTIVES / INTENDED LEARNING OUTCOMES

At the end of the lesson, students will be able to:

• Students should be able to identify the arithmetic


operators in Python and how to use them.
• Students should be able to create working programs
applying all core data types, variables, constants,
operators and assignment statements.
• Students should be able to know what is Identifiers,
variables and constants are.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


INTRODUCTION

To create a Python program, we must first understand and identify

the basic variables, data types, operators, statement expressions,

and etc. Because, python is an all- purpose programming language

that deals with various kinds of fundamentals used to create Python

programs.
IDENTIFIERS

An identifier is a “name given to entities” like class, functions,


variables, etc. It helps to differentiate one entity from another.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


IDENTIFIERS

Rules for writing identifiers:


1. The Python identifier is made with a combination of lowercase or
uppercase letters, digits or an underscore.

These are the valid characters.


• Lowercase letters (a to z)
• Uppercase letters (A to Z)
• Digits (0 to 9)
• Underscore (_)

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


IDENTIFIERS

Examples of a valid identifier:


• num1
• FLAG
• get_user_name
• userDetails
• _1234
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is a
valid name.
3. Keywords cannot be used as identifiers.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


IDENTIFIERS

4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.


5. An identifier can be of any length.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


IDENTIFIERS

Identify if the following are valid characters:


• 1character
• num2
• age$
• $name
• _1234
• Age_

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


DATA TYPES

In programming, data type is an important concept.

Variables can store data of different types, and different types can do
different things.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


BUILT-IN DATA TYPES

Python has the following data types built-in by default, in these categories:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


BUILT-IN DATA TYPES

1. Strings are arrays of bytes representing Unicode characters. A string is a


collection of one or more characters put in a single quote, double-quote or
triple quote. In python there is no character data type, a character is a
string of length one. It is represented by str class.

2. Numeric value can be integer, floating number or even complex numbers.


These values are defined as int, float and complex class in Python.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


BUILT-IN DATA TYPES

• Integers – This value is represented by int class. It contains positive or


negative whole numbers (without fraction or decimal). In Python there is
no limit to how long an integer value can be.
• Float – This value is represented by float class. It is a real number with
floating point representation. It is specified by a decimal point. Optionally,
the character e or E followed by a positive or negative integer may be
appended to specify scientific notation.
• Complex Numbers – Complex number is represented by complex class. It is
specified as (real part) + (imaginary part)j. For example – 2+3j

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


BUILT-IN DATA TYPES

3. Sequence is the ordered collection of similar or different data types. Sequences


allows to store multiple values in an organized and efficient fashion.

• Lists are just like the arrays, declared in other languages which is ordered
collection of data. It is very flexible as the items in a list do not need to be of the
same type.
• Tuple is also an ordered collection of Python objects. The only difference
between tuple and list is that tuples are immutable (cannot be changed) i.e.
tuples cannot be modified after it is created. It is represented by tuple class.
• Range - range() function returns a sequence of numbers, starting from 0 by
default, and increments by 1 (by default), and stops before a specified number.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


BUILT-IN DATA TYPES

4. Mapping type - is a data type comprised of a collection of keys and associated


values. Python's only built-in mapping type is the dictionary.

5. Set Types:
• Set – used to store multiple items in a single variable.
• Frozen set - is just an immutable version of a Python set object. While elements of
a set can be modified at any time, elements of the frozen set remain the same
after creation.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


BUILT-IN DATA TYPES

6. Boolean Type - Data type with one of the two built-in values, True or False.
Boolean objects that are equal to True are truthy (true), and those equal to False
are falsy (false). But non-Boolean objects can be evaluated in Boolean context as
well and determined to be true or false. It is denoted by the class bool.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


BUILT-IN DATA TYPES

7. Binary Types:

• Bytes - byte() function converts an object to an immutable byte-represented


object of given size and data.
• Byte Array - bytearray() method returns a bytearray object which is an array of
given bytes. It gives a mutable sequence of integers in the range 0 <= x < 256.
• Memory View - memory view() function returns the memory views objects.
• memoryview objects allow Python code to access the internal data of an object
that supports the buffer protocol without copying

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


GETTING THE DATA TYPE

You can get the data type of any object by using the type() function:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


SETTING THE DATA TYPE

In Python, the data type is set when you assign a value to a variable:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


SETTING THE DATA TYPE

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


SETTING THE SPECIFIC DATA TYPE

If you want to specify the data type, you can use the following
constructor functions:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


VARIABLES

Variables are containers for storing data values.

CREATING VARIABLES
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


VARIABLES

Variables do not need to be declared with any particular type, and


can even change type after they have been set.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


CASTING

If you want to specify the data type of a variable, this can be done
with casting.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


GET THE TYPE

You can get the data type of a variable with the type() function.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


SINGLE OR DOUBLE QUOTES?

String variables can be declared either by using single or double


quotes:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


CASE SENSITIVE

Variable names are case-sensitive.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


CONSTANTS

A constant is a type of variable that holds values, which cannot be


changed. In reality, we rarely use constants in Python. Constants are
usually declared and assigned on a different module/file.

Then, they are imported to the main file.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

Operators are the constructs which can manipulate the value of

operands. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called

operands and + is called operator.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

Python language supports the following types of operators.


• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

ARITHMETIC OPERATORS
Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication, etc.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

COMPARISON OPERATORS
Comparison operators are used to compare values. It returns either True or
False according to the condition.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

LOGICAL OPERATORS
Logical operators are the and, or, not operators.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

BITWISE OPERATORS
Bitwise operators act on operands as if they were strings of binary digits. They
operate bit by bit, hence the name.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in
binary)

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

ASSIGNMENT OPERATORS
Assignment operators are used in Python to assign values to variables.

a = 5 is a simple assignment operator that assigns the value 5 on the right to


the variable a on the left.

There are various compound operators in Python like a += 5 that adds to the
variable and later assigns the same. It is equivalent to a = a + 5.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

MEMBERSHIP OPERATORS
Membership operators are used to test if a sequence is presented in an
object:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


OPERATORS

IDENTIFY OPERATORS
Identity operators are used to compare the objects, not if they are equal, but
if they are actually the same object, with the same memory location:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


END OF PRESENTATION
Thank you for listening!

Any questions?
Nothing is impossible. The word itself says ‘I’m possible!'

— Audrey Hepburn

You might also like