Explaining "variables" to people who have no programming experience, they are literally "boxes" for storing "changing values".
** Variable name = Value **
You can create a variable with.
var1 = 1 var1 = var1 + 1
Variables can store not only constants (numbers, strings) but also variable values. In this case, the value of var1 will eventually be 2. Although it is called a variable, it can also store values (constants) that do not change. (Once you store the value, if you don't change it, it's a constant)
TAX = 0.08
In Python, there is a common rule to capitalize variable names when you create constants. The program works without capitalization, but when others see your program, Follow the common rules so that you can immediately see that this variable is a constant. Python is also case sensitive for variable names. Note that "tax" and "TAX" are separate variables.
Variables can store various forms of data such as numbers, strings, and lists of them. Unlike other programming languages such as C and Java, Python is used to declare variables when declaring variables. It is not necessary to specify the Data type.
◆ For C language int a a = 1
◆ For Java double b b = 3.14 String c c = "text"
◆ For Python a = 1 b = 3.14 c = "text"
Python automatically determines whether a variable is a numeric or string type (Dynamic typing).
Next, I will introduce the basic data types of Python, the numeric type and the string type.
a = 1 b = 3.14 c = 2 + 3j
Integers, decimals and complex numbers are available. Calculator +,-, \ *, /,% (remainder) can be used for arithmetic operations on numeric data. By using "//", it is possible to divide by rounding down to the nearest whole number. Use "**" to return the number to the Nth power as a result.
display_number.py
print(1 + 2.5)
print(5 / 2)
print(5 // 2)
print(5 % 2)
print(3**2)
When executed, the following result will be output.
3.5 2.5 2 1 9
The string type stores the characters enclosed in "" or''.
text = "this is string" text2 = '1'
Note that text2 is the letter '1', not the number 1.
You can store a string containing a line break in a variable by enclosing it in "" "or"'.
** text3 = "" "1st line 2nd line "" "**
A sequence means "things that are arranged in order. Process in order". Lists (discussed in the next chapter) are typical sequences, but Python also treats string types as "character" sequences. In Python, you can perform common processing on sequence data, You can also perform the operation on character string type data. Write and execute the following program and check the output result.
string_func.py
text1 = "abc"
a_in_text = "a" in text1
b_in_text = "d" in text1
print("in_text_a {0}".format(a_in_text))
print("in_text_d {0}".format(b_in_text))
text2 = "abc" + "def"
print("abc + def {0}".format(text2))
print("abc * 3 {0}".format("abc" * 3))
print("text2[3] {0}".format(text2[3]))
print("text2[-1] {0}".format(text2[-1]))
print("text2[1:4] {0}".format(text2[1:4]))
print("text2[0:6:2] {0}".format(text2[0:6:2]))
print("len(text2) {0}".format(len(text2)))
print("min(text2) {0}".format(min(text2)))
print("max(text2) {0}".format(max(text2)))
a in xxx
Determines if the character a is included in the string variable xxx. Returns True if it is included, False if it is not included.
str1 + str2
Combine strings. Please note that numbers can also be stringed by enclosing them in "" or''.
10 + 20
Returns 30
"10" + '20'
Returns "1020". Well then
10 + "20"
What will return? Let's experiment.
TypeError: unsupported operand type(s) for +: 'int' and 'str'
You will see an error message like this: Numeric type and character string type cannot be combined with +. If you want to convert a numerical value to a character string and a character string to a numerical value, write as follows.
convert_explain.py
str1 = str(10) + '20'
int1 = 10 + int("20")
print("{0}".format(str1))
print("{0}".format(int1))
You can convert a numeric value to a string value with str (numeric). Similarly, you can convert a string type value that can be converted to a numeric type with int (string) to a numeric type value. If you try to convert a string such as "abc" that cannot be converted to a number with int (), an error will occur.
str * x
For numeric type, it is "*" that performs multiplication, but for string type, "repeat the string x times" Is executed.
str[x] Returns the xth character of the string str. Note that this x starts at 0th. If x is negative, the xth character from the end is returned.
str[x:y] Returns the ** y-1 ** th string from the xth string str.
str[x:y:z] Returns the ** y-1 ** th string from the xth string str, at z character intervals.
len(str) Returns the string length of the string str.
min(str) / max(str) min (str) returns the smallest of the characters that make up the string str. If the string is "54321", it returns "1", and if it is "abcde", it returns "a".
max (str) returns the largest of the characters that make up the string str. Returns "5" if the string is "54321" and "e" if it is "abcde".
When numbers and character strings are mixed, the maximum / minimum is determined in the following order.
Small
0 1 2 ・ ・ ・ 8 9 A B ・ ・ ・ Y Z a b ・ ・ ・ y z
Large
Next: Python Basic Course (5 List Tuples)
Recommended Posts