Let’s learn about python’s basic data types and variables.
Variables: The space to store data
When the computer computes, it first creates storage space (memory) and puts a specific value there. This space is called a variable, and when creating a variable in Python, use the assignment operator ‘=’ (please note that it doesn’t mean as equal).
a = 1 # integer b = 5.5 # float c = None # Used when not defined which data type to put
The variable name is on the left side of ‘=’ as like a and b. Another variable or specific value is located to the right of ‘=’. Please note that the comment comes after ‘#’. The code after ‘#’ doesn’t run.
Variables must be declared and values initialized at the same time. If you don’t know what value to put, type “None”.
■ Why variableize?
Variables are used for ‘abstraction’. For example, let’s create a program that prints out age. If you want to express your age with print (“My age is 30 years old,”) you have to change the code to 31, 32, and 33 each year.
We need to formulate the code without directly responding to the value in that code. You can use a variable called ‘age’ and apply a function that calculates for age.
In addition, variables are used for reasons such as the fact that you can modify the code by the name of a variable even if you do not know the exact value, and that the value corresponding to that variable can be changed in bulk.
■ Variable naming rules
Variable names can be freely specified by the user. However, there are several rules.
- It can be composed of english case, _, and numbers.
- You can’t start with numbers.
- Reserved keywords cannot be named as variables.
Variables are simple words that ordinary people can easily understand. For co-work, variables such as student_id and teacher_id are better than random words like example1.
Variable names cannot be started with numbers because they are confused with a specific value.
You can’t also specify a word (reserved word) that Python has already preempted. For example, there are for, while, and these reserved words are displayed in green. If you want to create a variable name called for, you can transform it into a _for.
■ Numeric data: integers and floats
Numbers are integer types and float types.
When adding, multiplying, or subtracting integers, the integer value is the result. Dividing can lead to float If you only want to get a share of division, you can use // instead of / To replace the float type with an integer type, you can use int().
The data type that can display decimal points is the float type. Float() allows you to convert an integer to float.
■ Operation of numeric data
Basically, all the operations we are accustomed to are possible. The example below will make sense more intuitively.
# How to use the comparison operator. The result is called boolean value which has only two values(true or false). print (a > b) # a is greater than b. print (a< b) # a is smaller than b print (a>= b) # a is greater than or equal to b. print (a<= b) # a is smaller than or equal to b. print (a == b) # a and b are the same. print (a != b) # a and b are not the same. print (a + b) # addition print (a * b) # multiplication print (a - b) # subtraction print (a / b) # Division print (a % b) # remaining value print (a**b) # Exponential Print (a+b * 2) # the priority of the operator follows the general order. Print (a+b) * 2) # If you want to force operator priorities, tie them in braces().
Note that the value of the variable does not change before the assignment occurs.
a = 9 print (a - 3) # Results: 6 (9-3 = 6) print(a) # results: 9 # If you want to change the value to a-3, A = a -3 # should be assigned a -= 3 # It can also be expressed like the left.
String data (str)
Generating string data is also simple. You can use ‘ or ”
a = 'Hi' # Use when you want to use double quotes within a string b = "Hi" # Use when you want to use single quotes within a string c = '''Hi, Lucy!'' # When you want to leave a space within a string d = ""Hi", Lucy!"""
You can also return a specific string by using the index. An index is the order of strings, and symbols like spaces also take place.
The range of the index is valid only from zero to the length of the string. Python supports a negative index. -1 index means the last index of the string.
a = 'Hi,t Lucy! n Welcome!') # Escape character: n entert: tab print(a) #[1] i in the first place of the string is returned. print(a)[-1] # Output the last index Print(a) #[-2] Output the second index from last print(a) # [1000]error => Out of range of strings print(a) [-1000]# error occurs
Slicing refers to the partial extraction of the string. The last is not included and is printed before that.
Print (a)[4:9] # output from 4 to 8 Print (a)[4:8] # output from 4 to 7 Print (a) #[4:] output from #4 to the end print(a) # Out[:4]put from the beginning to 3 times print(a[:]) # Output from the beginning to the end
■ Functions in string data
After a variable, type ‘.’ and press ‘tab’ to display a list of various functions.
a.upper() # All capitalized a.replace ('h', 'b') # substitute all 'h' to 'b'
Format initializes a specific value in a string as a variable to generate a dynamic string.
month = 7 day = 1 ex = 'Today is the day of {} monthly.' ex.format (month, day) print (ex)
Split is separated by a specific character and replaced by a list of strings.
ex = 'Hello! My name is Lucy.' ex.split() # Separated by spacing ex.split (!) # divided by exclamation point
■ Print function
Finally, let’s look at the print function. The print function outputs the value of that variable. You can also output multiple variables on a single line by listing them based on ‘.’
print (a, b) # Output in a, b order print (b, a) # Output in b, a order Print (1, 2.0, 3, a) # Output in 1, 2.0, 3, a order
If you want to see a description of the function, you can press shift+tab. For print, the separator (sep) is ”. You can freely change the output value by looking at the description.
print(a, b, sep='@', end='^^') # Replace the separator to '@' and end with '^^'.
Quite interesting. I truly loved visiting your site. Thanks.
Thanks for the kind comment 🙂
Wonderful items from you, man. I’ve understand your stuff prior to and you are simply too great.
I actually like what you’ve bought right here, really like what you are saying and the way through which you say it.
You’re making it entertaining and you still take care of to stay it wise.
I can not wait to learn much more from you. That
is actually a terrific web site.
thank you so much for sharing 🙂 your comments made my day!
Comments are closed.