高校物理160203 views
高校国語788336 views
高校化学2924392 views
雑学1473612 views
高校日本史190571 views
小学社会310485 views
英語613639 views
小学算数1200427 views
りんご209490 views
高校生物551766 views
Help
Tools
NewsSpreadsheetCalendarSlidesTier ListPen ToolIllustrationCrayonPixel ArtASCII ArtPerspectiveEndless StairsGraphMind MapER DiagramFamily TreeMemeCurved TextImage EditorMosaicRetro FilterPencil SketchOCR/HighlighterMakeup EditorFaviconVideo TrimmerScrolling VideoVideo TitleColor PickerColor ExtractorBonfireFireworksWater RippleWater SplashBreaking GlassWood GrainMarble TextureCSS ButtonIcon MakerBar ChartGrouped Bar ChartStacked Bar ChartPie ChartLine ChartArea ChartStacked Area ChartScatter Plot3D Bar Chart3D Pie ChartBar Chart RaceBubble ChartPopulation PyramidPictogramEarningsCandlestick ChartInvestment RiskMortgage SimulatorCalculatorMatrix CalculatorFunction GraphPolynomial ExpansionVenn DiagramField VisualizerRubik's Cube Group TheoryTraveling SalesmanVoronoi and DelaunayFractalColumn ArithmeticDraw Math FiguresArithmetic AnimationArithmetic Word ProblemsCounting with Tree DiagramsCube NetsCross SectionsMotion PathMechanicsWavesLight and LensesThermodynamicsHow Semiconductors WorkMolecular StructuresAtomic OrbitalsElectrochemical CellsChemical EquilibriumCrystal LatticesBuffer pHComplex IonsDNA Double HelixCell DivisionMembrane ChannelsNerve ImpulseRock ClassificationWeatherConstellationsSolar and Lunar Eclipses3D ModelingFloor PlanSeismic StructuresIntersection TurnMaglevCooking AnimationOrigamiLive Viewer CountGeoJSON MapRailway MapPopulation MapCrime MapLand Price MapSchool MapShrine and Castle MapHouse of Representatives MapWord MapSolitaireReversiHakoiri MusumeChessHamburgerRippleSlide Puzzle MakerNeon PinballNovel MakerJapanese Typing PracticePiano Score EditorMusic Theory

English

Pythonのリスト(配列):数値や文字列を複数並べる

これまでは変数に一つの数値や文字列を入れていました。例えばxという変数に1を入れたり、aという変数に「りんご」を入れたりするプログラムです。

x = 1
a = ‘りんご’

では変数に複数の数値や文字列を入れたいときはどうすればいいでしょうか?結論からいうとカッコを使って次のように表します。

a = [1, 2, 3, 4, 5]
b = [‘りんご’, ‘みかん’, ‘メロン’]
c = [3, ‘りんご’, 7, ‘ケーキ’]

aには1,2,3,4,5という数値をこの順番に入れています。同じようにbには「りんご」「みかん」「りんご」という文字列を三つ入れています。こうした複数の値をもつ変数をリストといいます。aやbやcはすべてリストです。

リストには数値だけを入れることも、文字列だけを入れることも、数値と文字列を混ぜて入れることもできます。

問題

3,6,9,12をこの通りの順番に持つリストをつくりなさい。

解答

x = [3, 6, 9, 12]

リストをprintしたらどうなるか?

a = [1, 2, 3, 4, 5]
b = [‘りんご’, ‘みかん’, ‘メロン’]
c = [3, ‘りんご’, 7, ‘ケーキ’]

print(a)
print(b)
print©

上のようにリストそのものをprintしたらどうなるでしょうか?

[1, 2, 3, 4, 5]
[‘りんご’, ‘みかん’, ‘メロン’]
[3, ‘りんご’, 7, ‘ケーキ’]

そのままになりました。リストはカッコつきで定義し、カッコつきで表示されます。

Pythonで複数の数値や文字列を、一つの変数に入れたいときはリストというものを使います。リストはカッコを使って表します。