C2 DataTypes Variables PCC
C2 DataTypes Variables PCC
C2 DataTypes Variables PCC
1. String
What is a string?
String is a series of characters. String is always in
quotes
Ex: “Hello world” or ‘Hello world’
String Methods:
String method is an action that Python can perform on a
piece of data. When using method remember to use the dot
(), it will tell python to do the method
Ex: print(“hello world”.title())
Output: Hello World
Some methods:
title() – title form a string
upper() – make a string become uppercase
lower() – make a string become lowercase
Combining and Concatenation:
To combine two or many variables together, use symbol (+)
to concatenate them.
Ex: Concatenate a name
last_name = “nguyen”
first_name = “tuan minh”
full_name = last_name + “” + first_name
print(full_name)
Output: nguyen tuan minh
* use method title() to complete the string
Adding whitespaces, Tabs and Newlines:
What is whitespace in programming? Whitespace is refer to a
nonprinting character
Why we need to organize whitespace? To make the output is
easier to read
Someway to adding whitespaces:
\n – adding tab
\t – new line
\n\t – newline then adding tab
Ex: A nice output
print(“Venus\nMecury”) print(“\tVenus\n\tMecury”)
Output: Output:
Venus Venus
Mecury Mecury
Stripping Whitespaces:
Why we need to strip whitespaces? Sometime, whitespaces
cause bug, sometime it make the output look not good
3 strip methods:
rstrip() – strip the whitespace in the right of the string
lstrip() – strip the whitespace in the left of the string
strip() – strip all the whitespaces in both side
Ex: Strip the text
text = “ whitespace cause problems ”
text_Strip = text.strip()
text_RStrip = text.rstrip()
text_LStrip = text.lstrip()
print(text_Strip\ntext_RStrip\ntext_LStrip)
Output:
“Whitespaces cause problems”
“ Whitespaces cause problems”
“Whitespaces cause problems ”
2. Number
Interger:
What is integer? It is the simplest thing in Python. Number
like: 2, 3, 6, 842948,... are integers
What can we do with number in python:
Plus +
Minus -
Divine /
Multiply *
Exponet **
Mod %
Float:
What is a float number? A float number is a number with a
decimal point. 0.1 or 73.4 is float number
Do the same math with integer. Sometime the output will be
like this: 0.300000004 with many zeros, just skip those
zeros
Debug – Avoiding Type Error with the str() Function:
What is a Type Error? Python can’t recognize the kind of
information you are using
A string can only concatenate with a string, it can’t
concatenate with a integer
To change a integer into a string, use str()
Ex: A gift for Linda
age = 23
message = “Welcome the age of ”+ age “, Linda”
print(message)
Output:
TypeError: can only concatenate str (not "int") to str
TypeError: Can’t convert ‘int’ object to str implicitly
3. Comments
The important of comments in coding: Comment makes the
program easier to understand, it will make many beneficial
when you work as a team. Always remember to make comments.
What is comment in python? Comment in python simply a note
Comment in Python always start with (#)
Tips when making comment: make it meaningful, as short and
clear as possible, use English to write comment
Always remember this when coding, make code beautiful, make your code
simple, make your code friendly, make your code readable