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

Python is Google's three most advanced programming languages. Guido van Rossum, a programmer, made Python, one of the script languages. The name Python is from Guido's favorite comedy program. It's from Monty Python's Flying Circus.

 

To compare Python to C, Python codes are way shorter and more straightforward. In Python, code blocks are fundamental since the loops work with the indentations.

 

To download Python, you can download it directly from the official website.

Download Python | Python.org

You can also download Anaconda, which includes Python, Jupyter notebook, pandas, scipy, and other libraries. 

Anaconda | Anaconda Distribution

 

After downloading it, check on the command prompt by inserting Python to check if it is downloaded well. If you see this >>>, it means it is there.

You will see the libraries in Anaconda if you insert the pip list.

Check if jupyter notebook is also set up; if so, let's open jupyter. 

You will see these new pop-ups.

Click New- Python 3(ipykernel) to create a new notebook.

In this notebook, you can run the codes directly and efficiently like the one below. 

You can click the run button to run the codes or use the hotkey Ctrl + Enter or Shift + Enter.

 

Now, we will download PyCharm

If you want to download the other versions, click here: https://www.jetbrains.com/ko-kr/pycharm/download/other.html.

After downloading, to create a project, click New Project.

This pythonProject will be the name of the project that you will make. 

Run main.py to see if there is a virtual environment.  

For the basic configurations, you can set them up in the Settings menu.

To install the keymap plugins that PyCharm doesn't have, click Get more keymaps in Settings / Plugins.

I will download the Eclipse keymap.

Here in Python Interpreter, you can see the libraries list. You can easily install, uninstall, or upgrade them. 

Now, let us create a file and run it.

Here are some eclipse hotkeys that I use a lot. 

#Eclipse Keymap
# To execute : Ctrl + F11
# To Copy and Paste : Ctrl + Alt + Pg Dn
# To comment out : Ctrl + /

 

Python is a functional language, so there are many built-in Functions. The link below is about the built-in functions in Python.

Built-in Functions — Python 3.7.14 documentation

 

The syntax differs from Java, so you may need to learn the difference. 

For example, in Java, you must put a semicolon on every line when the line finishes. In Python, you do not need semicolons except for writing more than two codes in one line. 

print('Java'); print('Python')

To see the reserved list, follow this: 

import keyword
print(keyword.kwlist)

Commenting is also different. Here, we use #, ''', and """

 

In the next post, we will discuss more syntaxes and basic rules in Python.

 

 

'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) Variables and Operators  (0) 2022.10.29

+ Recent posts