In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to master Python". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Python Foundation
1. Variable
You can think of a variable as a word used to store values. Let's look at an example.
It is easy to define a variable in Python and assign a value to it. If you want to store the number 1 into the variable "one", let's try it:
One = 1
Isn't it super easy? You just need to assign a value of 1 to the variable "one".
Two = 2 some_number = 10000
You can assign any value to any other variable as long as you want. As you can see from above, the variable "two" stores integer variable 2 and the variable "some_number" stores 10000.
In addition to integers, we can use Boolean values (True/Flase), strings, floating-point types, and other data types.
# booleanstrue_boolean = Truefalse_boolean = False# stringmy_name = "Leandro Tk" # floatbook_price = 15.80
two。 Control flow: conditional statement
"If" uses an expression to determine whether a statement is True or False, and if it is True, then execute the code in if, as shown in the following example:
If True: print ("Hello Python If") if 2 > 1: print ("2 is greater than 1")
2 is larger than 1, so the print code is executed.
When the expression in "if" is false, the "else" statement will be executed.
If 1 > 2: print ("1 is greater than 2") else: print ("1 is not greater than 2")
1 is smaller than 2, so the code in "else" executes.
You can also use the "elif" statement:
If 1 > 2: print ("1 is greater than 2") elif 2 > 1: print ("1 is not greater than 2") else: print ("1 is equal to 2")
3. Loop and iteration
In Python, we can iterate in different forms. I'll talk about while and for.
While loop: when the statement is True, the code block inside the while executes. So the following code will print out 1 to 10.
Num = 1while num% s "% (key, dictionary [key]) # some_key-- > some_value
The above is an example of how to use For loops in a dictionary. For each key in the dictionary, we print out the value corresponding to key and key.
Another way is to use the iteritems method.
Dictionary = {"some_key": "some_value"} for key, value in dictionary.items (): print ("% s-- >% s"% (key, value)) # some_key-- > some_value
We named the two parameters key and value, but this is not necessary. We can name it as we like. Let's take a look:
Dictionary_tk = {"name": "Leandro", "nickname": "Tk", "nationality": "Brazilian", "age": 24} for attribute, value in dictionary_tk.items (): print ("My% s is% s"% (attribute, value)) # My name is Leandro # My nickname is Tk # My nationality is Brazilian # My age is 24
You can see that we use attribute as a parameter to key in the dictionary, which has the same effect as using key naming. This is great!
Class & object
Some theories:
Objects are representations of real-world entities, such as cars, dogs, or bicycles. These objects have two main characteristics in common: data and behavior.
Cars have data, such as the number of wheels, the number of doors and seating space, and they can show their behavior: they can accelerate, stop, show how much fuel is left, and many other things.
We think of data as attributes and behaviors in object-oriented programming. It is also expressed as:
Data → properties and behavior → methods
A class is a blueprint for creating a single object. In the real world, we often find many objects of the same type. Like a car. All cars have the same structure and model (with an engine, wheels, doors, etc.). Each car is made up of the same set of blueprints and has the same components.
Python object-oriented programming mode: ON
Python, as an object-oriented programming language, has such concepts: classes and objects.
A class is a blueprint and a model of an object.
So, a class is a model, or a way to define properties and behavior (as we discussed in the theory section). For example, a vehicle class has its own properties to define what kind of vehicle the object is. The properties of a car include the number of wheels, energy type, seat capacity and speed.
With this in mind, let's look at the syntax of Python's class:
Class Vehicle: pass
In the above code, we use the class statement to define a class. Isn't it easy?
An object is an instantiation of a class, which can be instantiated by the class name.
Car = Vehicle () print (car) #
In this case, car is an object (or instantiation) of class Vehicle.
Remember that the vehicle class has four properties: number of wheels, type of fuel tank, seat capacity and speed per hour. When we create a new vehicle object, we set all the properties. So here, we define a class that accepts parameters when it is initialized:
Class Vehicle: def _ _ init__ (self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity): self.number_of_wheels = number_of_wheels self.type_of_tank = type_of_tank self.seating_capacity = seating_capacity self.maximum_velocity = maximum_velocity
This init method. We call it a constructor. So when we create a vehicle object, we can define these properties. Imagine that we like Tesla Model S, so we want to create an object of this type. It has four wheels, uses electric energy, has five and has a speed of 250 kilometers (155 miles) per hour. We started to create an object like this:
Tesla_model_s = Vehicle (4, 'electric', 5,250) "how to master Python" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.