Post

Python Crash Course 2 - Variable & Datatype

I’m YouKoutaku. These codes are from the book python_Crash_Course, 2nd edition. I learned python and took these notes for this book.

1. Introduction

2.variable and datatype

1
2
#2.1 Hello Python world
print("Hello Python world!")
1
Hello Python world!
1
2
3
#2.2 variable
m = "Hello Python world!" #variable name
print(m)
1
Hello Python world!
1
2
3
#E-2-1
a = "YANG GUANGZE"
print(a)
1
YANG GUANGZE
1
2
3
4
5
#E-2-2
a = "YANG GUANGZE"
print(a)
a = "you koutaku"
print(a)
1
2
YANG GUANGZE
you koutaku
1
2
3
4
5
#2.3 Strings
a="YANG"
b='yang'
print(a)
print(b)
1
2
YANG
yang
1
2
3
4
5
#Avoid code duplications
a="Y'A'NG"
b='y"a"ng'
print(a)
print(b)
1
2
Y'A'NG
y"a"ng
1
2
3
4
#2.3.1 title() used to change the first letter of a word to upper case.
name = 'yang guangze'
print(name.title())
#value.title()
1
Yang Guangze
1
2
3
4
5
6
7
8
#upper() used to convert the string to upper case
name = 'Yang Guangze'
print(name.upper())
#value.upper()

#lower() used to the string is converted to lower case.
print(name.lower())
#value.lower()
1
2
YANG GUANGZE
yang guangze
1
2
3
4
5
6
#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 is means string.format()
1
My first name is yang. and my last name is guangze.
1
2
3
4
5
6
#title()
first_name = 'yang'
last_name = 'guangze'
full_name = f"{first_name} {last_name}"
message = f"Hello, {full_name.title()}!"
print(message)
1
Hello, Yang Guangze!
1
2
3
4
5
6
#2.3.3 blank print
print("Languages:\npython\nC\nJavaScript")
print("Languages:\n\tpython\n\tC\n\tJavaScript")

#\t=[TAB]
#\n=[ENTER]
1
2
3
4
5
6
7
8
Languages:
python
C
JavaScript
Languages:
	python
	C
	JavaScript
1
2
3
4
5
6
7
8
9
10
11
#2.3.4 delete blank
f_l = '  python  '
print(f_l)
print(f_l.rstrip())
#rstrip(): means remove spaces at the end of the string.
print(f_l.lstrip())
#lstrip():means remove spaces at the beginning of the string.
print(f_l.strip())
#strip():means remove spaces at both the beginning and the end of the string.
f_l=f_l.strip()
print(f_l)
1
2
3
4
5
  python  
  python
python  
python
python
1
2
3
4
#E-2-3
name = 'yang guangze'
print(f'Hello {name}, would you like to learn some Python today?')

1
Hello yang guangze, would you like to learn some Python today?
1
2
3
4
#E-2-3
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?')
1
2
3
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?
1
2
#E-2-5
print('Albert Einstein once said, "A person who never made a mistake never tried anything new."')
1
Albert Einstein once said, "A person who never made a mistake never tried anything new."
1
2
3
4
#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)
1
Alebert Einstein once said, "A person who never made a mistake never tried anything new."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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)

1
2
3
4
5
6
	Alebert Einstein
 once said, "A person who never made a mistake never tried anything new."
Alebert Einstein once said, "A person who never made a mistake never tried anything new."
	Alebert Einstein once said, "A person who never made a mistake never tried anything new."
Alebert Einstein
 once said, "A person who never made a mistake never tried anything new."
1
2
3
4
5
#2.4 number
num = 14_000_000_000
print(num)


1
14000000000
1
2
3
4
5
6
7
#2.4.5 Assignment to variables at the same time (assignment in order)
x, y, z = 0, 0, 0
print(x,y,z)

#2.4.6 constants(Constants are defined as all-capital variable names)
MAX_CONNECTIONS = 5000

1
0 0 0
1
2
3
4
5
6
#E-2-8
print(5+3)
print(2*4)
print(int(16/2))
print(2**3)

1
2
3
4
8
8
8
8
1
2
3
#E-2-9
F_NUM = 9
print(f'{F_NUM}is my favorite number.')
1
9is my favorit numeber.
1
#2.5 comment (#)
This post is licensed under CC BY 4.0 by the author.