05-Python

Python(Part.4)| python [Literals and types]

python| まとめ | 現役エンジニア&プログラミングスクール講師「python」のまとめページです。pythonに関して抑えておきたい知識や文法やにについて記事をまとめています。まとめページの下部には「おすすめの学習書籍」「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中...

Objective

  • To understand “literal”.
  • To understand “types”.

”Literals” and ”types” are the foundation of python. ”Literals” and ”types,” like ”operators,” are useful for understanding small changes in programs by knowing their roles and how to use them. They are important to keep in mind before learning about ”variables,” ”control structures,” ”classes,” ”file operations,” and so on.

Python Basics (Literals)

In the previous issue, we dealt with ”operators”. In this installment, we will learn about ”literals”. ”Literals” are unchangeable values used in programs.

Specifically, they are integers such as ”0” and ”23”, floating-point numbers such as ”56.0” and ”0.034,” strings such as ”abcdefg,” and python’s built-in constants such as “True” and “False”.

Literals are the values used for the operand in the previous study.

「Types of literals」

「Types of literals」

A ”literal” was an unchangeable value used in a program. These literals can be classified into several types. These can be broadly classified into ”numeric literals,” ”string literals,” ”built-in constants,” and ”other literals (lists, tuples, dictionaries, sets)”.

Numeric ”literals” can be further divided into ”integer literals” and ”floating-point literals”, and ”integer literals” into ”binary literals,” ”octal literals,” ”decimal literals,” and ”hexadecimal literals”.

Literal
Classification
Classification of classified literalsNotation
Numeric
literals
Integer literals (binary literals)
Binary numbers are prefixed with “0b”.
0b10011
Available numbers are “0” and “1”.
Integer literals (octal literals)
Octal numbers begin with the prefix “0o”.
0o12345670
Available numbers are “0” to “7”.
Integer literals (decimal literals)1234567890
Available numbers are “0” to “9”.
Integer literals (hexadecimal literals)
Hexadecimal numbers start with “0x”.
0x1234567890abcdef
Available numbers are “0” to “9” and a” to ”f”.
Floating point number literals123.45
Imaginary literal3.14j
String literalsString literal”abcdef”
Built-in
constants
Boolean literal (True)True
Boolean literal (False)False
NoneNone
Other
Literals
List[1,2,3,4,5]
Tuple(1,2,3,4,5)
Dictionary{key1:value1, key2:value2}
Set{1,2,3,4,5}

Unchangeable Value

The meaning of “unchangeable value” expressed in the “explanation of literal” is that for values marked “1”, the value does not change its appearance.

python basics (types)

Literals have a “type” that describes their kind. A “type” is also a classification of literals and defines how the literal is to be handled. For example, a literal that represents a number can perform arithmetic operations.

TypeMeaning
intInteger type (in python, the number of digits a PC can handle is available)
※In other programming languages, the number of digits is fixed.
floatFloating point number type
※In other languages, there are “double” and “float” floating-point types, but only “float” is available in python. “float” in python is equivalent to “double” in other languages.
complexcomplex type
A type of imaginary literals.
strString type
booleanboolean True/False
listlist type
tupletuple type
dictdictionary type
setset type

”Literal” and ”type” are inseparable.

Using Literals and Checking Types

From here, I would like to use the “Interactive Shell” to manipulate “literals” and confirm “types”. Start the “Interactive Shell” and enter the following

We use “binary literals,” “octal literals,” “decimal literals,” and “hexadecimal literals” as integer literals. 0b10011” and ‘0o12345670’ are the respective literals. (The displayed “19” and “2739128” are also literals.)

The following uses floating-point number literals and imaginary literals.

The following uses string literals. String literals must be enclosed in “single quotes” or “double quotes”. Failure to do so will result in an error.

Boolean literals are built-in constants. They look like strings, but do not need to be enclosed in “single quotes” or the like.

The following input checks the “type” of a literal. The type function of the “built-in functions” is used to check the “type“.

Built-in functions are functions that are provided with a programming language. Because they are prepared, you must know how to use them in order to use them.

That is all for this article.

「python」おすすめ書籍 ベスト3 | 現役エンジニア&プログラミングスクール講師「python」の学習でお勧めしたい書籍をご紹介しています。お勧めする理由としては、考え方、イメージなどを適切に捉えていること、「生のpython」に焦点をあてて解説をしている書籍であることなどが理由です。勿論、この他にも良い書籍はありますが、特に質の高かったものを選んで記事にしています。ページの下部には「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中。...

ブックマークのすすめ

「ほわほわぶろぐ」を常に検索するのが面倒だという方はブックマークをお勧めします。ブックマークの設定は別記事にて掲載しています。

「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】について解説している記事です。削除方法も掲載しています。...
【パソコン選び】失敗しないための重要ポイント | 現役エンジニア&プログラミングスクール講師【パソコン選び】失敗しないための重要ポイントについての記事です。パソコンのタイプと購入時に検討すべき点・家電量販店で見かけるCPUの見方・購入者が必要とするメモリ容量・HDDとSSDについて・ディスプレイの種類・バッテリーの持ち時間や保証・Officeソフト・ウィルス対策ソフトについて書いています。...
RELATED POST
05-Python

Python(Part.24)| python【反復構造(for-else文)】| [Iterative Structures(for-else statement)]

2024年7月1日
プログラミング学習 おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site