W3schools - Python_Type
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
Number
Int : or integer, is a whole number, positive or negative, without decimals, of unlimited length
Float : or “floating point number” is a number, positive or negative, containing one or more decimals
Can also be scientific numbers with an “e” to indicate the power of 10
Complex : Are written with a “j” as the imaginary part, Can’t convert into another number type
String
Can assign a multiline string to a var by using three quotes
In the result, the line breaks are inserted at the same position as in the code
a = “””
I
am
Spongebob
”””
print(a)
# output
“I
am
Spongebob”
In python are arrays of bytes representing Unicode characters
However, Python doesn’t have a character data type
A single character is simply a string with a length of 1
Square brackets can be used to access elements of the string
a = “Hello, World!”
print(a[0]) # output “H”
# To check if a certain phrase or character is present in a string, can use “in”
print(“Hello” in a) #output True
Slicing
Can return a range of characters by using the slice syntax
Specify the start index and the end index, separated by a colon, to return a part of the string
a = “Hello, World!”
print(a[2:5]) # output “llo”
# By leaving out the start or end index, the ragne will start or end, at the first or end character
print(a[:5]) # output “Hello”
# Use negative indexes to start the slice from the end of the string
print(a[-1]) # output “!”
Modify
Python has a set of built-in methods that you can use on strings
a = “ Sponge, Bob ”
print(a.upper()) # output “ SPONGE, BOB ”
print(a.lower()) # output “ sponge, bob ”
print(a.strip()) # output “Sponge, Bob”
print(a.replace(“S”, “s”)) # output “ sponge, Bob ”
print(a.split(“,”)) # output [‘ Sponge’, ‘ Bob ’]
Format
In python, can’t combine strings and numbers using +
But can combine strings and numbers by using the format()
It takes the passed arguments, formats them, and places them in the string where the placeholders {}
are
It takes unlimited number of arguments, and are placed into the respective placeholders
age = 20
txt = “I am {}”
print(txt.format(age)) # output “I am 20”
# Can use index numbers {0} to be sure the arguments are placed in the correct placeholders
quantity = 3
itemno = 567
price = 49.95
order = “I wanna pay {2} dollars for {0} pieces of item {1}”
print(order.format(quantity, itemno, price))
Escape
To insert characters that are illegal in a string, use an escape character
An escape character is a backslash \ followed by the character you want to insert
txt = “We are the so-called \“Vikings\” from the north”
\’ : Single quote
\ : Backslash
\n : New Line
\r : Carriage Return
\t : Tab
\b : Backspace
\f : Form Feed
\ooo : Octal value
\xhh : Hex value
Boolean
Represent one of two values : True
or False
Can evaluate any expression in Python, and get one of two answers
# Can compare two values
print(10 > 9) # output True
print(10 == 9) # output False
Almost any value is evaluated to True if it has some sort of content
Any string is True, except empty strings
Any number is True, except 0
Any list, tuple, set, and dict are True, except empty ones
Collection
List : Is ordered and changeable, allows duplicate members
Tuple : Is ordered and unchangeable, allows duplicate members
Set : Is unordered, unchangeable(but can remove or add item), and unindexed, no duplicate members
Dictionary : Is ordered(In python 3.6 and earlier, unordered) and changeable, no duplicate members
List
Are used to store multiple items in a single var
Are created using square brackets
list = [“apple”, “banana”]
List items are indexed, the first item has index[0]
Are ordered, changeable, and allow duplicate values
Ordered means that the items have a defined order, and that order will not change
- If add new items to a list, the new items will be placed at the end of the list
Changeable means that can change, add, and remove items in a list after it has been created
Allow duplicate means that can have items with same value
Items can be of any data type, also list can contain different data types
Tuple
Are used to store multiple items in a single var
Are written with round brackets
tuple = (“apple”, “banana”)
Are ordered, unchangeable, and allow duplicate values
Tuple items are indexed, the first item has index[0]
Ordered means that the items have a defined order, and that order will not change
Unchangeable means that we can’t change, add or remove items after the tuple has been created
Since tuples are indexed, they can have items with the same value
To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple
Items can be of any data type, also tuple can contain different data types
Set
Are used to store multiple items in a single var
Are unordered, unchangeable, and unindexed, don’t allow duplicate values
-
Set items are unchangeable, but can remove items and add new items
-
Can’t be sure in which order the items will appear
Unordered means that the items in a set don’t have a defined order
Unchangeable means that can’t change the items after the set has been created
Are written with curly brackets
set = {“apple”, “banana”}
Dictionary
Are used to store data values in key:value pairs
Is ordered, changeable and don’t allow duplicates
Are written with curly brackets, and have keys and values
dict = {“brand”: “Ford”, “model” : “Mustang”}
Dict items are ordered, changeable, and doesn’t allow duplicates
Ordered means that the items have a defined order, and that order will not change
Changeable means that we can change, add or remove items after the dict has been created
The values in dict items can be of any data type