Python numbers, strings, list types (Python learning memo ①)

Python type

Numerical value

point


#addition
>>> 2+2              
4
      
#Subtraction and multiplication
>>> 50 - 5 * 6        
20                   
>>> (50 - 5) *6      
270

#division
>>> 17 / 3       
5.666666666666667
>>> 17 // 3      
5                
>>> 17 % 3       
2

#Get both quotient and remainder
>>> divmod(17, 3)
(5, 2)

#radix
# 0b:Binary
# 0o:Octal
# 0x:Hexadecimal
>>> 0b10
2
>>> 0o10
8
>>> 0x10
16

#Exponentiation
>>> 5 ** 2
25
>>> 5 ** 3
125

#Use interactive mode as a calculator
>>> _ + 1
126

String type

point

#In the path name/Can be used, so the following is unnecessary
# raw name (Prefix the string with r)
>>> print('C:\some\name')
C:\some
ame
>>> print(r'C:\some\name')
C:\some\name

#Multiple lines
print("""\
    Usage: thingy [OPTIONS]
        -h Display this usage message
        -H hostname   
""")
#output
Usage: thingy [OPTIONS]
        -h Display this usage message
        -H hostname

#Repeat and concatenate strings
>>> 3 * "un" + "ium" 
'unununium'

#Automatic concatenation of string literals(Cannot be used with variables)
>>> 'Py' 'thon' 
'Python'

# index
>>> word[0]
'P'
>>> word[-1] 
'n'

# slice
>>> word[:3] 
'Pyt'
>>> word[3:] 
'hon'

# len
>>> len(word) 
6

list

point

#list
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f']
# index
>>> letters[1] 
'b'

# slicing
>>> letters[3:] 
['d', 'e', 'f']

# append
>>> letters.append('g') 
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']

#Substitution to slicing
>>> letters[3:5] = ['D', 'E'] 
>>> letters
['a', 'b', 'c', 'D', 'E', 'f', 'g']

# len
>>> len(letters)
7

#Nested list
>>> array = [['a', 'b', 'c'], [1, 2, 3]] 

Find the Fibonacci sequence


#Fibonacci series
#Multiple assignment is possible in this way
a, b = 0, 1

while b < 10:
    print(b)
    a, b = b, a+b

About the print function

name = 'Kawausomando'
#Specify multiple values(Space is given between)
print('My name is', name)
#output: My name is Kawausomando

#end keyword
print(name, end=' is my name')
#output: Kawausomando is my name

#sep keyword
print(1, 2, 3, sep=' and ')
#output: 1 and 2 and 3

Recommended Posts

Python numbers, strings, list types (Python learning memo ①)
Python class (Python learning memo ⑦)
Python module (Python learning memo ④)
Python3 List / dictionary memo
[Memo] Python3 list sort
Python exception handling (Python learning memo ⑥)
Python control syntax, functions (Python learning memo ②)
Input / output with Python (Python learning memo ⑤)
Interval scheduling learning memo ~ by python ~
"Scraping & machine learning with Python" Learning memo
Python memo
python memo
Python memo
python memo
python learning
Python memo
[Python] list
Python memo
Python memo
Python & Machine Learning Study Memo: Environment Preparation
[Learning memo] Basics of class by python
Python data structure and operation (Python learning memo ③)
Python standard library: second half (Python learning memo ⑨)
Python & Machine Learning Study Memo ③: Neural Network
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Python & Machine Learning Study Memo ⑥: Number Recognition
Python standard library: First half (Python learning memo ⑧)
LPIC201 learning memo
Python basics: list
[Python] Learning Note 1
python beginner memo (9.2-10)
Django Learning Memo
Python & Machine Learning Study Memo ⑤: Classification of irises
[python] Create a list of various character types
python beginner memo (9.1)
python learning output
Python & Machine Learning Study Memo ②: Introduction of Library
Python learning site
★ Memo ★ Python Iroha
Python learning day 4
[Python] EDA memo
Python Deep Learning
Python 3 operator memo
Python learning (supplement)
Python> Comprehension / Comprehension> List comprehension
Deep learning × Python
[Memo] Machine learning
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
progate Python learning memo (updated from time to time)
Python & Machine Learning Study Memo ⑦: Stock Price Forecast
Python beginner memo (2)
python learning notes
Python list manipulation
About list processing (Python beginners after learning Ruby)
[Python] Numpy memo
Convert strings to character-by-character list format with python
Python learning memo for machine learning by Chainer from Chapter 2
Python learning memo for machine learning by Chainer Chapters 1 and 2
[Python] Extract only numbers from lists and character strings