31 July 2024
Integers are whole numbers. 整数是整数。
1
my_number = 354
Floats are numbers with decimal places. When you do a calculation that results in a fraction e.g. 4 ÷ 3 the result will always be a floating point number.
1
my_float = 3.14159
A string is just a string of characters. It should be surrounded by double quotes.
1
my_string = "Hello"
You can add strings to string to create a new string. This is called concatenation. It results in a new string.
1
2
"Hello" + "Angela"
#becomes "HelloAngela"
Because the double quote is special, it denotes a string, if you want to use it in a string, you need to escape it with a “"
1
2
3
speech = "She said: \"Hi\""
print(speech)
#prints: She said: "Hi"
You can insert a variable into a string using f-strings. The syntax is simple, just insert the variable in-between a set of curly braces {}. ```python days = 365 print(f”There are {days} in a year”) ···
You can convert a variable from 1 data type to another. Converting to float: float() Converting to int: int() Converting to string: str()