Ở phương pháp này, bạn sẽ gán trực tiếp giá trị trong python nhưng với các ngôn ngữ lập trình khác như C, C++, trước tiên bạn phải khởi tạo kiểu dữ liệu của biến. Vì vậy, trong Python không cần khai báo rõ ràng các biến so với việc sử dụng một số ngôn ngữ lập trình khác. Bạn có thể bắt đầu sử dụng biến ngay lập tức
C++
// C++ code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
#include <bits/stdc++.h>
using namespace std;
geeksforgeeks0 geeksforgeeks1
geeksforgeeks2
geeksforgeeks3geeksforgeeks4
geeksforgeeks3geeksforgeeks0 geeksforgeeks7
geeksforgeeks3geeksforgeeks9
geeksforgeeks3The value of a is: 11The value of a is: 12 The value of a is: 13
The value of a is: 14
C
The value of a is: 15
// upon condition using Direct Initialisation Method
The value of a is: 17
geeksforgeeks0 geeksforgeeks1
geeksforgeeks2
geeksforgeeks3geeksforgeeks4
geeksforgeeks3geeksforgeeks0 geeksforgeeks7
geeksforgeeks3geeksforgeeks9
geeksforgeeks3The value of a is: 19// C++ code to demonstrate variable assignment0// C++ code to demonstrate variable assignment1// C++ code to demonstrate variable assignment2
The value of a is: 14
Java
// C++ code to demonstrate variable assignment4
// upon condition using Direct Initialisation Method
// C++ code to demonstrate variable assignment6 // C++ code to demonstrate variable assignment7
// C++ code to demonstrate variable assignment8 // C++ code to demonstrate variable assignment9
geeksforgeeks3// upon condition using Direct Initialisation Method1 // upon condition using Direct Initialisation Method2 // upon condition using Direct Initialisation Method3 // upon condition using Direct Initialisation Method4
geeksforgeeks3geeksforgeeks2
// upon condition using Direct Initialisation Method7geeksforgeeks4
// upon condition using Direct Initialisation Method7geeksforgeeks0 #include <bits/stdc++.h>1#include <bits/stdc++.h>2#include <bits/stdc++.h>3
// upon condition using Direct Initialisation Method7geeksforgeeks9
// upon condition using Direct Initialisation Method7#include <bits/stdc++.h>7The value of a is: 12 #include <bits/stdc++.h>9
geeksforgeeks3The value of a is: 14
The value of a is: 14
Python3
using3
using4
using5
using6using7 #include <bits/stdc++.h>2
using9
namespace0 ________165 _______0The value of a is: 12 ________ 169_______3 ________ 169_______4namespace5
C#
namespace6
// upon condition using Direct Initialisation Method
using namespace9
std;0
// C++ code to demonstrate variable assignment8 std;2
geeksforgeeks3
// upon condition using Direct Initialisation Method1 // upon condition using Direct Initialisation Method2 // upon condition using Direct Initialisation Method3 std;7
geeksforgeeks2
geeksforgeeks3
geeksforgeeks3geeksforgeeks01
geeksforgeeks3geeksforgeeks0 geeksforgeeks7
geeksforgeeks3geeksforgeeks06
geeksforgeeks3geeksforgeeks08The value of a is: 12 #include <bits/stdc++.h>9
The value of a is: 14
The value of a is: 14
std;0
geeksforgeeks14
Javascript
geeksforgeeks15
geeksforgeeks16
// upon condition using Direct Initialisation Method
// upon condition using Direct Initialisation Method7geeksforgeeks4
// upon condition using Direct Initialisation Method7geeksforgeeks21 geeksforgeeks7
// upon condition using Direct Initialisation Method7geeksforgeeks9
// upon condition using Direct Initialisation Method7geeksforgeeks26The value of a is: 12 #include <bits/stdc++.h>9
geeksforgeeks29
geeksforgeeks30
Đầu raThe value of a is: 5
Gán nhiều giá trị cho các biến khác nhau
Không giống như các ngôn ngữ khác, chúng ta có thể dễ dàng gán giá trị cho nhiều biến trong python một cách dễ dàng
Java
geeksforgeeks31
geeksforgeeks32
// C++ code to demonstrate variable assignment6 // C++ code to demonstrate variable assignment7
// C++ code to demonstrate variable assignment8 // C++ code to demonstrate variable assignment9
geeksforgeeks3// upon condition using Direct Initialisation Method1 // upon condition using Direct Initialisation Method2 // upon condition using Direct Initialisation Method3 geeksforgeeks41
// upon condition using Direct Initialisation Method7
geeksforgeeks43geeksforgeeks44geeksforgeeks45geeksforgeeks46geeksforgeeks47geeksforgeeks48geeksforgeeks45#include <bits/stdc++.h>3
geeksforgeeks43geeksforgeeks52
geeksforgeeks43
geeksforgeeks3The value of a is: 14
The value of a is: 14
con trăn
geeksforgeeks57
geeksforgeeks58using7geeksforgeeks45geeksforgeeks61geeksforgeeks47geeksforgeeks61geeksforgeeks45
namespace0geeksforgeeks66namespace3geeksforgeeks68namespace3geeksforgeeks70
Javascript
geeksforgeeks57
geeksforgeeks72_______0_______45geeksforgeeks61geeksforgeeks47geeksforgeeks61geeksforgeeks45geeksforgeeks78
geeksforgeeks79
Đầu rageeksforgeeks
Phương pháp 2. Sử dụng toán tử có điều kiện (?. )
Phương pháp này còn được gọi là toán tử bậc ba. Vậy Cú pháp cơ bản của Toán tử có điều kiện là. -
điều kiện? . Sai_Value
Sử dụng Toán tử có điều kiện, bạn có thể viết mã một dòng trong python. Toán tử điều kiện hoạt động theo cách đầu tiên sẽ đánh giá điều kiện, nếu điều kiện là đúng thì biểu thức đầu tiên( True_value) sẽ in ra, ngược lại sẽ đánh giá biểu thức thứ hai(False_Value)
Dưới đây là cú pháp trong các ngôn ngữ phổ biến khác
C++
// C++ code to demonstrate variable assignment
geeksforgeeks81
#include <bits/stdc++.h>
using namespace std;
geeksforgeeks0 geeksforgeeks1
geeksforgeeks2
geeksforgeeks3geeksforgeeks90
geeksforgeeks3geeksforgeeks0 geeksforgeeks93
geeksforgeeks3geeksforgeeks9
geeksforgeeks3The value of a is: 11The value of a is: 12 The value of a is: 13
The value of a is: 14
C
The value of a is: 15
geeksforgeeks81
The value of a is: 17
geeksforgeeks0 geeksforgeeks1
geeksforgeeks2
geeksforgeeks3geeksforgeeks90
geeksforgeeks3geeksforgeeks0 geeksforgeeks93
geeksforgeeks3geeksforgeeks9
geeksforgeeks3The value of a is: 19// C++ code to demonstrate variable assignment0// C++ code to demonstrate variable assignment1// C++ code to demonstrate variable assignment2
The value of a is: 14
Java
// C++ code to demonstrate variable assignment4
geeksforgeeks81
// C++ code to demonstrate variable assignment6 // C++ code to demonstrate variable assignment7
// C++ code to demonstrate variable assignment8 // C++ code to demonstrate variable assignment9
geeksforgeeks3// upon condition using Direct Initialisation Method1 // upon condition using Direct Initialisation Method2 // upon condition using Direct Initialisation Method3 // upon condition using Direct Initialisation Method4
geeksforgeeks3geeksforgeeks2
// upon condition using Direct Initialisation Method7geeksforgeeks90
// upon condition using Direct Initialisation Method7geeksforgeeks0 #include <bits/stdc++.h>1The value of a is: 138 The value of a is: 139The value of a is: 140 The value of a is: 141The value of a is: 142 The value of a is: 143The value of a is: 144#include <bits/stdc++.h>3
// upon condition using Direct Initialisation Method7geeksforgeeks9
// upon condition using Direct Initialisation Method7#include <bits/stdc++.h>7The value of a is: 12 #include <bits/stdc++.h>9
geeksforgeeks3The value of a is: 14
The value of a is: 14
Python3
The value of a is: 155
The value of a is: 156
The value of a is: 157
using6using7 The value of a is: 142 The value of a is: 161 The value of a is: 138 The value of a is: 139The value of a is: 140 The value of a is: 165 The value of a is: 144