In this post, we will talk about variables in Python.

Depending on the language, variables, data types, and syntaxes differ.

# Variables : Name of the memory space for a certain type of data
# Format : variable name = value(data)

#Integer
i = 10
print('i=', i)
print(type(i))      #<class 'int'>

#Float
r = 3.14
print('r=', r)
print(type(r))      #<class 'float'>
# In Java, there are double and float, however, in Python, there is only float variable.

# Complex numbers
c = 3 + 5j
print('c=', c)
print(type(c))

# Boolean
b1 = True
b2 = False
#The first letter has to be upper case.
print('b1=', b1)
print('b2=', b2)
print(type(b1))
print(type(b2))     # <class 'bool'>

#String
s1 = "Meadow"
s2 = "Dodo"
print('s1=', s1)
print('s2=', s2)
print(type(s1))
print(type(s2))

#List
list = ['South Korea', 'China', 'Canada', 'Kenya', 'Germany']
print(list[0])          #Indexing : red
list[1] = 'Mexico'
print('list=', list)
print(type(list))       #<class 'list'>

#Tuple
t =('Argentina', 'America', 'Singapore', 'Australia', 'Laos')
print(t[0])         #Indexing
#t[0] ='Chile'       #The values in Tuple can't be edited.
print('t=', t)
print(type(t))

#Dictionary : { 'key' : 'value'}
#Map in Java
d = {'Google' : 'http://www.google.com',
     'Apple' : 'http://www.apple.com',
     'Naver' : 'http://www.naver.com'}
print('d=', d)
print(type(d))

Python has very different operators from other languages, which make human more convenient to program. 

For this reason, we will pour over these operators. 

 

Assignment Operators

The function of assignment operators is different from other languages in that there are no such functions that you can exchange values without assigning a temporal variable in other languages. 

# Assignment Operators
a = 10
print('a=', a)
x = y = z = 0
print('x=', x)
print('y=', y)
print('z=', z)

c, d = 10, 20
print('c=', c)
print('d=', d)

# Exchanging values
c, d = d, c
print('c=', c)
print('d=', d)

Arithmetic Operators

# Arithmetic Operators
# +,-,* /Division (float), //Division (floor), %(Modulus), **(Exponent)

a = 10; b = 3

result1 = a + b
result2 = a - b
result3 = a * b
result4 = a / b
result5 = a // b
result6 = a % b
result7 = a ** b

Comparison Operators 

In Python, you can compare strings, too. The comparison of strings works with decimal ASCII Code. 

The ASCII for ‘A’ is 65, and for ‘a’, it is 97 so 'a' will be greater than 'A' if you compare them.

If the strings are more than one, it will compare to the first letters. 

# Comparison (Relational) Operators
# Boolean
x = 10; y = 20
str1 = 'abc'
str2 = 'python'

print( x == y)
print( x != y)
print( x > y)
print( x >= y)
print( x < y)
print( x <= y)

print(str1 == str2)
print(str2 == 'python')
print(str1 < str2)

Logical Operators

Unlike Java, in Python, we don't use operator symbols but AND OR NOT.

Logical Operator Description
A and B If both operands are true, then the condition becomes true.
A or B  The condition becomes true if any of the two operands are non-zero.
not A Used to reverse the logical state of its operand.
# Logical Operators
# AND OR NOT
b1 = True; b2 = False; b3=True; b4=False

print(b1 and b2) #False
print(b1 and b3) #True
print(b2 or b3)  #True
print(b2 or b4)  #False
print(not b1)    #False
print(not b2)    #True

 

Now, let us create a program that prints the pass or fail of 5 subjects(minimum 40, avg 60) entered on the keyboard.

When it comes to if ~ else statement in Python, indentation is necessary.

n1 = int(input('Insert score1'))
n2 = int(input('Insert score2'))
n3 = int(input('Insert score3'))
n4 = int(input('Insert score4'))
n5 = int(input('Insert score5'))

avg = (n1+n2+n3+n4+n5) / 5      #float
print('avg=', avg)

if n1 >= 40 and n2 >= 40 and n3 >= 40 and n4 >= 40 and n5 >= 40 and avg >=60:
    print('Pass')
else:
    print('Fail')

 Augmented Assignment Operators

#Augmented Assignment Operators

a = 0    #Initial value

a += 1
print(a)

a -=5 
print(a)

a *= 2
print(a)

a /= 4
print(a)

Membership Operators

These operators are only in Python. It is a beneficial function, like the search function, that you can use when you put Ctrl + F.

Membership Operator Description
in Evaluates to true if it finds a variable in the specified sequence and false otherwise.
not in Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

Example 1

# Membership Operators : in , not in

list = [10, 20, 30, 40, 50]

result1 = 30 in list
result2 = 60 in list
print('result1=', result1)  #True
print('result2=', result2)  #False

str = 'abcde'
result3 = 'c' in str
resuslt4 = 'k' in str
print('result3=', result3)  #True
print('result4=', result4)  #False

Example 2

# Membership Operators : in , not in
msg = input('How are you?')

if 'a' in msg:
    print('There is ''a'' in your reply.')
else:
    print('There is no ''a'' in your reply.')

Example 3

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

for i in months:
    if 'r' in i:
         print(i)

'Python' 카테고리의 다른 글

Python) Control statements  (0) 2022.11.06
Python) Dictionary  (0) 2022.11.04
Python) Tuple / Set  (0) 2022.11.03
Python) Datatypes - Str  (0) 2022.11.03
Python) Configuration and Basics  (0) 2022.10.27

+ Recent posts