Python basics (variables, types, operators, if statements, for statements)

How to use basic variables and types Variables have no type declaration and values have types

1. Variables and 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]

2. List, tuple


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,)

3. Operator

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

4. if statement

a = 5
if a < 10:
    print(" a < 10")
elif a > 100:
    print(" a > 100 ")

[Output] a < 10

5. for statement

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

Python basics (variables, types, operators, if statements, for statements)
Python Exercise for Beginners # 1 [Basic Data Types / If Statements]
Python basics ② for statement
[Python] Output battles and combinations (nesting for statements and if statements)
[TouchDesigner] Tips for for statements using python
Python basics ⑤
Python basics
Learn the basics while touching python Variables
Python basics ④
Summary of various for statements in Python
[Python] Variables
Python basics ③
[Python] Organizing how to use for statements
Python basics
Getting Started with Python for PHPer-Super Basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
[Python] I searched for various types! (Typing)
Python variables and data types learned in chemoinformatics
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
[Python for Hikari] Chapter 09-01 Classes (Basics of Objects)
2016-10-30 else for Python3> for:
Python if statement
python [for myself]
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
# 3 [python3] Various operators
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
[Python] if statement
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Getting Started with python3 # 2 Learn about types and variables
Python netCDF4 read speed and nesting of for statements
A Java programmer studied Python. (for, if, while statement)
[Introduction to Python] How to write repetitive statements using for statements
Python3> slice copy / slice notation> used in for statements, etc.