Basic knowledge of Python

Basic knowledge of Python

Sorting using slices

Example 00. Sort the character string "stressed" in reverse order.

q00.py


#! usr/bin/pyenv python
# _*_ cording utf-8 _*_

word = "stressed"
print(word[::-1])

q00.Output result of py


desserts

Point Use slices. A slice can extract a part or all of a character string.

word[(start point):(end point):(Number of steps)] 

If you want to select everything from the beginning to the end of a word, you can omit the start and end points.

Extraction display using slices

Example 01. Output the odd-numbered character of the character string "Patatokukashi". It also outputs even-numbered characters.

q01.py


#! usr/bin/pyenv python
# _*_ cording utf-8 _*_

word = "Patatoku Kashii"

#Extract odd-numbered characters
extract1 = word[::2]
print(extract1)

#Extract even-numbered characters
extract2 = word[1::2]
print(extract2)

q01.Output result of py


Police car
taxi

Point When using slices, the start point and end point may be omitted unless the character string is definitely determined.

Process multiple objects in a loop at the same time using the zip function

Example 02. "Police car" and "Taxi" are connected alternately from the beginning, and "Patatokukashi" is displayed.

q02.py


#! usr/bin/pyenv python
# _*_ cording utf-8 _*_

word1 = "Police car"
word2 = "taxi"
linking = ""

for (str1, str2) in zip(word1, word2):
    linking += (str1 + str2)
print(linking)

q02.Output result of py


Patatoku Kashii

Point In python, multiple objects can be processed in a loop at the same time by using the zip function. However, in the case of multiple objects, the number of executions is adjusted to the one with the smaller number of elements.

Recommended Posts

Basic knowledge of Python
Basic usage of Python f-string
Basic knowledge of Linux and basic commands
Summary of basic knowledge of PyPy Part 1
[Python] Basic knowledge used in AtCoder
Basic grammar of Python3 system (dictionary)
Basic study of OpenCV with Python
Introduction of Python
RF Python Basic_01
Basics of Python ①
Basics of python ①
Copy of python
Python3 basic grammar
RF Python Basic_02
Introduction of Python
Getting Started with python3 # 1 Learn Basic Knowledge
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
Python Basic Course (at the end of 15)
Basic grammar of Python3 system (included notation)
[Python] Operation of enumerate
List of python modules
Python basic course (12 functions)
Python I'm also basic
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Basic sorting in Python
Copy of python preferences
Python basic course (9 iterations)
Basics of Python scraping basics
Basic operation of pandas
Basic usage of flask-classy
[python] class basic methods
[python] behavior of argmax
Basic usage of Jinja2
Python Basic Course (11 exceptions)
Basic operation list of Python3 list, tuple, dictionary, set
Usage of Python locals ()
the zen of Python
Python basic course (6 sets)
Basic operation of Pandas
Python3 cheat sheet (basic)
Basic usage of SQLAlchemy
Python basic grammar (miscellaneous)
Installation of Python 3.3 rc1
Python Basic Course (Introduction)
Python basic memorandum part 2
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
python basic on windows ②
Basic knowledge about Linux
Python basic memo --Part 2
Python basic course (13 classes)
Basic Python command memo
# 4 [python] Basics of functions
Python basic grammar note (4)
Python basic grammar note (3)
Sober trivia of python3
Summary of Python arguments
Python basic grammar memo
This is the only basic review of Python ~ 3 ~