Unit 2 - Data Types and String - Lec (1) - 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

10/13/2020

COMMENTS
• Helps in getting description about the code for future reference.
• Python comment starts with # symbol
• E.g. #compute the percentage of the hour that has elapsed percentage.
• Percentage = (minute * 100)/60
UNIT II: DATA TYPES AND STRINGS
BY : CJCENTENO

BY : CJCENTENO
Image: https://morioh.com/p/b51aa797cb94

DOCSTRINGS PYTHON VARIABLES


• A variable provides a named storage that the
program can manipulate.

 Python docstrings are the string literals that appear right •The declaration happens automatically when you
assign a value to a variable.
after the definition of a function, method, class, or
• Variables can have any name, but Python reserved
module. words cannot be used.

• Variables are named memory location used to store


data in program which keeps on changing during
execution.

• Fixed values used in programs such as numbers, letters


and strings are called “Constants”. https://www.networkworld.com/article/3387154/w
orking-with-v ariables-on-linux.html

BY : CJCENTENO BY : CJCENTENO

VARIABLE NAMING CONVENTIONS: MNEMONIC VARIABLE NAMES:


• Must start with a letter or an underscore “_”. • Use simple rules of variable naming and avoid reserved words.
• Must consist of a letters, numbers and underscores. • While using simple rules, we have a lot of choice for variable naming.
• It is a case sensitive. • Initially this choice can be confusing either in reading or writing the program.
• Cannot be used: 1_hello, @hello, h123#2, -abc. • The following two programs are identical in terms of what they accomplish, but
*Eg: First_Name, Age, Num1. very different when you read and try to understand them:

*Note: You cannot use reserved words for variable names and identifiers.

BY : CJCENTENO BY : CJCENTENO

1
10/13/2020

MULTIPLE
ASSIGNING VALUES TO VARIABLES
ASSIGNMENT
• Python allows you to assign a
single value to several variables
simultaneously.

a=b=c=1
a, b, c = 1, 2, "cris“

https://w w w .netclipart.com/isee/ioxoxo_person-thinking-clipart-emoticon-smiley-face-emoji-clipart/

BY : CJCENTENO BY : CJCENTENO

DATA TYPES

It is a classification that
specifies which type of
value a variable has and
what type of
mathematical, relational https://threatpost.com/zoom-removes-data-mining-linkedin-feature/154404/

or logical operations can


be applied to it without STANDARD DATA TYPES
causing an error.

BY : CJCENTENO BY : CJCENTENO

• Number objects are created when 1. PYTHON


you assign a value to them. NUMBERS 2. PYTHON STRINGS
var1 = 1
var2 = 10 • String created with either pairs of single or
double quotes.
• word = 'word' sentence = "This is a sentence."
• You can delete a single object or
multiple objects by using the del • paragraph = """This is a paragraph. It is made
statement. up of multiple lines and sentences."""

del var1
del var_a, var_b

BY : CJCENTENO BY : CJCENTENO

2
10/13/2020

STRING IS A SEQUENCE OF
CHARACTERS, LIKE "PYTHON IS COOL"
• Each character has an index

• Accessing a character: string[index]

x = "Python is cool“
print(x[10])

BY : CJCENTENO BY : CJCENTENO

STRING
• It is a sequence of characters.
• It may contain alphabets, numbers and special characters.
• Usually strings are enclosed within a single quotes and double quotes.

BUILT IN STRING FUNCTIONS


• le() – Find out the length of characters in string
• Max() – Smalles value in a string based on ASCII values
• Min() – Largest value in a string based on ASCII values

BY : CJCENTENO BY : CJCENTENO

4. TUPLE
3. LIST • Tuple is an ordered sequence of items same as a list. The only difference is that
tuples are immutable. Tuples once created cannot be modified.
• List is an ordered sequence of items. It is one of the most used datatype in Python
and is very flexible. All the items in a list do not need to be of the same type.

• Accessing tuple using Loops

BY : CJCENTENO BY : CJCENTENO

3
10/13/2020

THE + OPERATOR USED FOR STRING


CONCATENATION STRING SLICE
Example:
a= "hi"
b= "hello"
c= a+b
print(c)
s=hello
THE * OPERATOR CAN CREATE MULTIPLE
CONCATENATED COPIES
print("PYTHON"*10)
Output:
PYTHONPYTHONPYTHONPYTHONPYTHONPYTHONPYTHONPYTHONPYTHONPYTHON

BY : CJCENTENO BY : CJCENTENO

BOOLEAN VALUES
Booleans represent one of two values: True or False.

END OF UNIT ii

BY : CJCENTENO BY : CJCENTENO

You might also like