A note where a Python beginner got stuck

Studying Python 2.7.

Processing

No increment, decrement operator

Increment is ʻi = i + 1`

for is the content of the list that is iteratively processed

There is no loop like for (i = 0; i <10; i ++) like in other languages

Like using while


cnt = 0
while cnt<10:
    print cnt
    cnt += 1

Make a list of the specified range with the range function and turn it with for


for i in range(10):
        print i

String relation

Concatenation of string + number will result in error

Variables are defined by the type defined first.

Numerical values are converted to strings and then concatenated


print 'abc'+str(123)

Or use format


print 'abc{0}'.format(123)

The character string is basically a byte character string. When dealing with unicode strings, you have to explicitly declare'it's a unicode string'

print len('AIUEO')
# 15
print len(u'AIUEO')
print len('AIUEO'.decode('utf-8'))
print len(unicode('AIUEO', 'utf-8'))
#All the above results are 5

Format output

Something like sprintf


'%s %d %f' % ('AIUEO', 10, 1.2345)

format function


print '{0} {1} {2}'.format('AIUEO', 10, 1.2345)

Sprintf-like guys using% can use the same flags as sprintf

print '%s %04d %.2f' % ('AIUEO', 10, 1.2345)
#Aiueo 0010 1.23

Format output of unicode string

The following code will result in an error

print '{0} {1} {2}'.format(u'String', 10, 1.5)
#UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

The correct answer is that the format string is also a unicode string

print u'{0} {1} {2}'.format(u'String', 10, 1.5)
#String 10 1.5

Recommended Posts

A note where a Python beginner got stuck
A note about [python] __debug__
A story about a python beginner stuck with No module named'http.server'
Python: A Note About Classes 1 "Abstract"
A note about mock (Python mock library)
Note: Python
Python note
Where VSCode + venv environment construction got stuck
A reminder of what I got stuck when starting Atcoder with python
Beginner ABC154 (Python)
Python study note_002
Note: Python Decorator
Beginner ABC156 (Python)
Python programming note
[Python] Learning Note 1
python beginner memo (9.2-10)
[Python3] A story stuck with time zone conversion
python beginner memo (9.1)
Python beginner notes
Python study note_004
[Beginner] Python array
[Note] Create a one-line timezone class with python
A note on optimizing blackbox functions in Python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
A story stuck with handling Python binary data
A note about the python version of python virtualenv
Beginner ABC155 (Python)
Data analysis in Python: A note about line_profiler
A simple Pub / Sub program note in Python
Python Note: When assigning a value to a string
Python study note_003
I got stuck installing Anaconda 4.3.0 which became Python 3.6
[Beginner] Python functions
Beginner ABC157 (Python)
[Note] openCV + python
PyQ ~ Python Beginner ~
Just a note
A memo for creating a python environment by a beginner
Python beginner's note
Python beginner memo (2)
Python beginner Zundokokiyoshi
A story that got stuck when trying to upgrade the Python version on GCE
I got stuck when trying to specify a relative path with relative_to () in python
When I tried to introduce python3 to atom, I got stuck
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 2-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 0-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 5-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 6-
Things to note when initializing a list in Python
A note on handling variables in Python recursive functions
A note on speeding up Python code with Numba
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 4-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 3-
Python Note: The mystery of assigning a variable to a variable
A * algorithm (Python edition)
AtCoder Beginner Contest 181 Note
[Python] Take a screenshot
[Note] future sentence ~ Python ~
AtCoder Beginner Contest 187 Note