Basics of Python ①

I've been in the Ai classroom lately I will write a summary.

Basics

output

The unspoken output. The quotes used can be single or double

print('Hello World')

Enter a comment

#comment

Calculation

It can be calculated as follows. You can use it like this

・ Addition: "+" ·subtraction:"-" ·multiplication:"*" ·division:"/" ・ Calculation of remainder: "%" ・ Exponentiation: "**"

print(3+6)
#Output result
9

String

If you enclose it in quotation marks It will be a character string.

print('3+6')
#Output result
3+6

variable

Next, the basic variables

Variable basics

n = 3
#n is the variable name, 3 is the value to enter
print(n)
#3 and output
print(n+5)
#It can be calculated in the same way, and 8 is output.

The following 3 points can be used

The ones that cannot be used are as follows

important point

It's not unusable, but Japanese is OK But not very popular as a definition

If it turns out after it works

Delete it in the following form.

del variable name

Linking

You can connect as follows.

n = "Tanaka"
print("Name is" + n + "is")

##output:The name is Tanaka.

Mold

There are types such as letters and numbers. The types are as follows

str type: string int type: integer float type: floating point

When you want to know the type

print(type(Variable name))
age = 17
print(type(age))  #When you want to know the type of age

#Output result
<class 'int'>

Type conversion

If you try to connect with different types An error will occur, so convert as follows.

str (): Convert to string int (): Convert to integer value float (): Convert to numeric type including decimal point

height = 150
print("How tall are you" + str(height) + "cm.")

if statement

Comparison operator

Write as below and return true or falsa.

a == b # a and b are equal a! = b # a is not equal to b (inverted) a> b # a is greater than b a> = b # a is greater than or equal to b a <b # a is less than b a <= b # a is less than or equal to b

use example of if

if: Executes the following processing that is 4 indented when the conditions on the left are met. else: Execute if if and elif do not hold elif: Write if you want to process and condition separately between if and else.

Can be executed with only basic if Added else and elif in some cases

n = 2
if n == 1: #If n is equal to 1, perform the following processing
    print("This is the first process.") 
elif n == 2: #If n is equal to 2, perform the following processing
    print("This is the second process.") 
elif n == 3: #If n is equal to 3, perform the following processing
    print("This is the third process.") 
else: #If all the above three conditional expressions are not satisfied, the following processing is performed.
    print("This is the fourth and subsequent processes.")

Boolean operator (and ・ or ・ not)

Conditional expression A and conditional expression B # If both conditional expressions of A and B are True, processing is performed. Conditional expression A or conditional expression B # If either conditional expression is True, processing is performed. not Conditional expression # Returns False when the conditional expression is True, True when the conditional expression is False.

Example of use


n_1 = 15
n_2 = 29

print(n_1 > 8 and n_1 < 14)
# true
 
print(not n_1 ** 2 < n_2 * 5)
# true

Recommended Posts

Basics of Python ①
Basics of python ①
Basics of Python scraping basics
# 4 [python] Basics of functions
Basics of python: Output
Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
python: Basics of using scikit-learn ①
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
Python basics: list
Introduction of Python
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Getting Started with Python Basics of Python
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
Copy of python
#Python basics (#Numpy 2/2)
Review of the basics of Python (FizzBuzz)
#Python basics (functions)
Basics of Python x GIS (Part 2)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
About the basics list of Python basics
#Python basics (class)
Python basics summary
Introduction of Python
Learn the basics of Python ① Beginners
Basics of binarized image processing with Python
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
I didn't know the basics of Python
The basics of running NoxPlayer in Python
[Basics of python basics] Why do __name__ == "__main__"
[Python] Chapter 02-04 Basics of Python Program (About Comments)
[Python] Chapter 02-03 Basics of Python programs (input / output)
[Introduction to Data Scientists] Basics of Python ♬
[Python3] Understand the basics of file operations
[Python] Operation of enumerate
List of python modules
Python basics ② for statement
Python: Unsupervised Learning: Basics
Unification of Python environment
Copy of python preferences
Python basics 8 numpy test
[python] behavior of argmax
Usage of Python locals ()