Chapter 2: Variables and Data Types
Reference Book
These notes are based on Python Crash Course, 2nd Edition. I learned Python and took these notes while studying this book.
2.1 Hello Python World
Your first Python program:
# 2.1 Hello Python world
print("Hello Python world!")
Output:
Hello Python world!
2.2 Variables
Using variables to store data:
# 2.2 variable
m = "Hello Python world!" # variable name
print(m)
Output:
Hello Python world!
Exercise 2-1
# E-2-1
a = "YANG GUANGZE"
print(a)
Output:
YANG GUANGZE
Exercise 2-2
# E-2-2
a = "YANG GUANGZE"
print(a)
a = "you koutaku"
print(a)
Output:
YANG GUANGZE
you koutaku
2.3 Strings
Basic String Usage
# 2.3 Strings
a = "YANG"
b = 'yang'
print(a)
print(b)
Output:
YANG
yang
Avoiding Quote Conflicts
# Avoid code duplications
a = "Y'A'NG"
b = 'y"a"ng'
print(a)
print(b)
Output:
Y'A'NG
y"a"ng
2.3.1 String Methods
title() Method
The title()
method changes the first letter of each word to uppercase:
# 2.3.1 title() used to change the first letter of a word to upper case
name = 'yang guangze'
print(name.title())
# value.title()
Output:
Yang Guangze
upper() and lower() Methods
# upper() used to convert the string to upper case
name = 'Yang Guangze'
print(name.upper())
# value.upper()
# lower() used to convert the string to lower case
print(name.lower())
# value.lower()
Output:
YANG GUANGZE
yang guangze
2.3.2 Using Variables in Strings
F-strings for string formatting:
# 2.3.2 using values in strings
first_name = 'yang'
last_name = 'guangze'
print(f"My first name is {first_name}. and my last name is {last_name}.")
# f means string.format()
Output:
My first name is yang. and my last name is guangze.
# title() with f-strings
first_name = 'yang'
last_name = 'guangze'
full_name = f"{first_name} {last_name}"
message = f"Hello, {full_name.title()}!"
print(message)
Output:
Hello, Yang Guangze!
2.3.3 Whitespace Characters
# 2.3.3 whitespace characters
print("Languages:\npython\nC\nJavaScript")
print("Languages:\n\tpython\n\tC\n\tJavaScript")
# \t = [TAB]
# \n = [ENTER/NEWLINE]
Output:
Languages:
python
C
JavaScript
Languages:
python
C
JavaScript
2.3.4 Stripping Whitespace
# 2.3.4 removing whitespace
f_l = ' python '
print(f_l)
print(f_l.rstrip())
# rstrip(): removes spaces at the end of the string
print(f_l.lstrip())
# lstrip(): removes spaces at the beginning of the string
print(f_l.strip())
# strip(): removes spaces at both the beginning and the end of the string
f_l = f_l.strip()
print(f_l)
Output:
python
python
python
python
python
String Methods
rstrip()
: Remove whitespace from the right endlstrip()
: Remove whitespace from the left endstrip()
: Remove whitespace from both ends
Exercise 2-3
# E-2-3
name = 'yang guangze'
print(f'Hello {name}, would you like to learn some Python today?')
Output:
Hello yang guangze, would you like to learn some Python today?
# Using different case methods
print(f'Hello {name.upper()}, would you like to learn some Python today?')
print(f'Hello {name.lower()}, would you like to learn some Python today?')
print(f'Hello {name.title()}, would you like to learn some Python today?')
Output:
Hello YANG GUANGZE, would you like to learn some Python today?
Hello yang guangze, would you like to learn some Python today?
Hello Yang Guangze, would you like to learn some Python today?
Exercise 2-5
# E-2-5
print('Albert Einstein once said, "A person who never made a mistake never tried anything new."')
Exercise 2-6
# E-2-6
famous_person = 'Albert Einstein'
message = f'{famous_person} once said, "A person who never made a mistake never tried anything new."'
print(message)
Exercise 2-7
# E-2-7
famous_person = '\tAlbert Einstein\n'
message = f'{famous_person} once said, "A person who never made a mistake never tried anything new."'
print(message)
message = f'{famous_person.strip()} once said, "A person who never made a mistake never tried anything new."'
print(message)
message = f'{famous_person.rstrip()} once said, "A person who never made a mistake never tried anything new."'
print(message)
message = f'{famous_person.lstrip()} once said, "A person who never made a mistake never tried anything new."'
print(message)
2.4 Numbers
Number Representation
# 2.4 numbers
num = 14_000_000_000
print(num)
Output:
14000000000
2.4.5 Multiple Assignment
# 2.4.5 Assignment to variables at the same time (assignment in order)
x, y, z = 0, 0, 0
print(x, y, z)
Output:
0 0 0
2.4.6 Constants
# 2.4.6 constants (Constants are defined as all-capital variable names)
MAX_CONNECTIONS = 5000
Exercise 2-8
# E-2-8 - Four expressions that equal 8
print(5 + 3)
print(2 * 4)
print(int(16 / 2))
print(2 ** 3)
Output:
8
8
8
8
Exercise 2-9
# E-2-9
F_NUM = 9
print(f'{F_NUM} is my favorite number.')
Output:
9 is my favorite number.
2.5 Comments
# 2.5 comment (#)
# Use comments to explain your code
Summary
In this chapter, you learned about:
- Variables: Containers for storing data values
- Strings: Text data and string manipulation methods
- String Methods:
title()
,upper()
,lower()
,strip()
, etc. - F-strings: Modern way to format strings with variables
- Numbers: Integer and floating-point numbers
- Constants: Values that shouldn't change (use ALL_CAPS)
- Comments: Adding explanations to your code
These fundamentals form the foundation for all Python programming. In the next chapter, you'll learn about lists, one of Python's most important data structures.