How to use basic variables and types Variables have no type declaration and values have types
a = 10 #Integer type (int)
b = 10.123 #Floating type (float)
c = "Hello" #String type (str)
d = True #Logical type (bool)
e = [1, 2, 3] #List type (list)
print(a)
print(b)
print(c)
print(d)
print(e)
[Output] 10 10.123 Hello True [1, 2, 3]
List is the same as array
a = [10, 9, 8, 7, 6] #Creating a list
print(a[0]) #Show first element
a.append(5) #Add an element at the end
print(a)
a[0] = 0 #Swap elements
print(a)
[Output] 10 [10, 9, 8, 7, 6, 5] [0, 9, 8, 7, 6, 5]
Tuples are arrays like lists, but elements cannot be added, deleted, or replaced.
a = (5, 4, 3, 2, 1) #Creating tuples
print(a)
print(a[2]) #Get the third element
b = (1,) #Tuples with only one element at the end","Is necessary
print(b)
[Output]
(5, 4, 3, 2, 1) 3 (1,)
Arithmetic operator+addition
-subtraction
*Call
/Divide (decimal)
//Divide (integer)
%remainder
**Exponentiation
Comparison operator<small
>large
<=that's all
>=Less than
==equal
!=Not equal
Logical operator and satisfy both
or satisfy either one
not not satisfied
a = 5
if a < 10:
print(" a < 10")
elif a > 100:
print(" a > 100 ")
[Output] a < 10
Use the range of the loop with the range and in operators
for a in [1, 100, 200]: #Loop with list
print(a)
for a in range(3): #Loop using range
print(a)
[Output] 1 100 200 0 1 2
Recommended Posts