Bài viết này dành cho những người đã có kinh nghiệm lập trình và muốn học nhanh Python
Tôi đã tạo tài nguyên này vì thất vọng khi không thể tìm thấy khóa học trực tuyến hoặc bất kỳ tài nguyên nào khác dạy Python cho các lập trình viên đã biết các ngôn ngữ lập trình khác
Trong khóa học này, tôi sẽ trình bày tất cả những kiến thức cơ bản về Python [các bài học chuyên sâu sẽ được bổ sung sau] để bạn có thể bắt đầu làm quen với ngôn ngữ này một cách nhanh chóng
Mục lục
- Thiết lập môi trường
- Chào thế giới
- Dây
- Số
- Trôi nổi
- bool
- Danh sách
- Tuple
- bộ
- Từ điển
- nếu như. khác
- vòng lặp
- Chức năng
- Các lớp học
- mô-đun
- Giá trị thật và giả
- xử lý ngoại lệ
Thiết lập môi trường
Để bắt đầu, hãy cài đặt Python 3. Tôi khuyên bạn nên sử dụng VSCode làm trình chỉnh sửa của mình. Nó có rất nhiều tiện ích mở rộng để định cấu hình để bạn có thể thiết lập môi trường của mình chỉ trong vài phút
Chào thế giới
print["Hello world"]
Nếu bạn đã biết cơ bản về lập trình, có thể bạn sẽ biết cách chạy chương trình này. P. Lưu chương trình với phần mở rộng
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
8. Sau đó chạy # This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
9 hoặc 2
2 * 3
2 ** 7
[2 + 3] * 4
0Trong toàn bộ hướng dẫn này, chúng ta sẽ làm theo
2
2 * 3
2 ** 7
[2 + 3] * 4
1. 2
2 * 3
2 ** 7
[2 + 3] * 4
2 sẽ ngừng hoạt động vào năm 2020. Vì vậy, tôi nghĩ thật tốt khi sử dụng phiên bản mới nhấtBiến và kiểu dữ liệu
Các biến có thể chứa các chữ cái, số và dấu gạch dưới
Dây
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
Số
2
2 * 3
2 ** 7
[2 + 3] * 4
phao nổi
________số 8_______Hãy cẩn thận trong nối
count = 5
print["I need" + count + "chocolates"] # This line will throw error. As count is a integer, you have to type cast it.
print["I need" + str[count] + "chocolates"] # This will work
bool
True # First letter is caps
False
bool["some value"] # Returns True
bool[""] # Returns False
bool[1] # Returns True
Danh sách
Danh sách về cơ bản giống như mảng. Trong thế giới Python, họ gọi chúng là
2
2 * 3
2 ** 7
[2 + 3] * 4
3. Và họ được lệnhnumbers = ["one", "two", "three"]
numbers[0] # one
numbers[-1] # three. This is awesome. If we pass negative value Python will start from the end.
numbers[-2] # two
len[numbers] # 3. It just returns the length
numbers.append["four"] # Append will add the element to the last. ["one", "two", "three", "four"]
numbers.insert[1, "wrong_one"] # Insert will insert the particular value to the appropiate position. ["one", "wrong_one", "two", "three", "four"]
# Deleting a value is somewhat weird
del numbers[1] # Will delete the value in the appropiate position. "one", "two", "three", "four"]
# Pop will help you to remove the last element of an array
popped_element = numbers.pop[]
print[popped_element] # four
print[numbers] # ["one", "two", "three"]
# Remove elements by value
numbers.remove["two"] # ["one", "three"]. This will remove only the first occurence of an array.
# Sorting
alpha = ["z", "c", "a"]
alpha.sort[]
print[alpha] # ["a", "c", "z"] Sorting is permanent. now `alpha` is sorted permanently
alpha.sort[reverse=True]
print[alpha] #["z", "c" , "a"] Reverse sorting.
alpha = ["z", "c", "a"]
print[sorted[alpha]] # ["a", "c", "z"] This will just return the sorted array. It wont save the sorted array to the variable itself.
print[alpha] # ["z", "c", "a"] As you can see, it's not sorted
# Reversing an array
nums = [10, 1, 5]
nums.reverse[]
print[nums] # [5, 1, 10] It just reverses an array. It means it reads from last. It's not sorting it. It's just changing the chronological order.
# Slicing elements
alpha = ['a', 'b', 'c', 'd', 'e']
alpha[1:3] # ['b', 'c']. The first element is the starting index. And Python stops in the item before the second index.
alpha[2:5] # ['c', 'd', 'e']
alpha[:4] # [ 'a', 'b', 'c', 'd'] In this case, the first index is not present, so Python startes from the beginning.
alpha[:3] # ['a', 'b', 'c']
alpha[3:] # ['d', 'e'] In this case, last index is not present. So it travels till the end of the list.
alpha[:] # ['a', 'b', 'c', 'd', 'e'] There is no starting or ending index. So you know what happens. And this helps you in copying the entire array. I think I don't have to explain that if you copy the array, then any changes in the original array won't affect the copied array.
another_alpha = alpha # This is not copying the array. Any changes in alpha will affect another_alpha too.
bộ dữ liệu
Bộ dữ liệu giống như danh sách nhưng chúng không thay đổi. Điều này có nghĩa là bạn không thể thêm hoặc cập nhật chúng. Bạn chỉ có thể đọc các yếu tố. Và hãy nhớ rằng, giống như danh sách, Tuples cũng tuần tự
nums = [1, 2, 3]
print[nums] # [1, 2, 3]
print[nums[0]] # 1
print[len[nums]] # 3
empty_tuple = [] # empty tuple. Length is zero.
num = [1, ] # Note the trailing comma. When defining a single element in the tuple, consider adding a trailing comma.
num = [1]
print[type[num]] # It won't return a tuple. Because there is no trailing comma.
# Creating a new tuple from the existing tuple
nums = [1, 2, 3]
char = ['a', ]
new_tuple = nums + char
print[new_tuple] # [1, 2, 3, 'a']
bộ
Bộ là bộ sưu tập không có thứ tự không có phần tử trùng lặp
alpha = {'a', 'b', 'c', 'a'}
print[alpha] # set[['a', 'c', 'b']] As you can see, duplicates are removed in sets. And also the output is not ordered.
# Accessing items in set
# You can't access by index because Sets are unordered. You can access it only by loop. Don't worry about the for loop, we will get that in-depth in the following section.
for ele in alpha:
print[ele]
# To add element into the set
alpha.add['s']
# add can be used to insert only one element. If you want multiple elements, then update will be handy
alpha.update[['a', 'x', 'z']] # set[['a', 'c', 'b', 'x', 'z']] Remember duplicated are removed.
# Length of the alpha
len[alpha] # 5
# Remove the element from the set
alpha.remove['a']
alpha.discard['a'] # It's safer to use discard than remove. Discard will never throw an error even if the element is not present in the set but remove will do.
từ điển
Từ điển là bản đồ khóa-giá trị trong Python. Chúng không có thứ tự
user = {'id': 1, 'name': 'John wick', 'email': 'john@gmail.com'}
user['id'] # 1
user['name'] # John wick
# Length of the dict
len[user] # 3
# Add new key-value pair
user['age'] = 35
# To get all the keys
keys = user.keys[] # ['id', 'name', 'email', 'age']. This will return a list.
# To get all the values
values = user.values[] # [1, 'John wick', 'john@gmail.com']
# To delete a key
del user['age']
# Example of nested dict.
user = {
'id': 1,
'name': 'John wick',
'cars': ['audi', 'bmw', 'tesla'],
'projects': [
{
'id': 10,
'name': 'Project 1'
},
{
'id': 11,
'name': 'Project 2'
}
]
}
# We will see, how to loop through the dict in for loop section.
nếu như. khác
Bạn có thể đã biết cách hoạt động của câu lệnh
2
2 * 3
2 ** 7
[2 + 3] * 4
4. Nhưng hãy xem một ví dụ ở đây# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
0vòng lặp
Python có hai loại vòng lặp
- Vì
- Trong khi
vòng lặp while
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
1Đối với vòng lặp
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
2Chức năng
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
3Các lớp học
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
4mô-đun
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
5Giá trị thật và giả
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
6xử lý ngoại lệ
# This is comment in Python
msg_from_computer = "Hello" # String
another_msg ='Hello in single quote' # This is also a String.
print[msg_from_computer + " World..!"]
# Type will return the data type.
print[type[msg_from_computer]] # . We will see the explanation of this later.
7Cảm ơn bạn đã đọc. ]
Được đăng ban đầu trong repo Github này. Khóa học về Python
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Sản xuất tại Ấn Độ, Kỹ sư phần mềm, Trình tạo sê-ri, Rails ❤️ JavaScript
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu