Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of basic knowledge of python variables

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "summary of the basic knowledge of python variables", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn the "basic knowledge summary of python variables" bar!

Variables and data types

Variable

In order to better deal with data in programming languages, some variables are needed. Variables in the Python language can be of different data types and can be used directly without declaration.

Variable naming rules

Variable naming in Python 3 has certain requirements:

Variable names can only contain letters, numbers, and underscores. Variable names can start with a letter or an underscore, but not with a number. For example, a variable can be named message_1, but not 1_message.

two。 Variable names cannot contain spaces, but underscores can be used to separate the words. For example, the variable name greeting_message works, but the variable name greetingmessage raises an error.

3. Do not use Python keywords and function names as variable names, that is, do not use Python to reserve words for special purposes

Keywords refer to words reserved for the Python language, such as import.

We can use the keyword module to view keywords in an interactive environment:

Xinsz08@xinsz08:~$ python3

Python 3.5.2 (default, Nov 17 2016, 17:05:23)

[GCC 5.4.0 20160609] on linux

Type "help", "copyright", "credits" or "license" for more information.

> import keyword

> keyword.kwlist

['False',' None', 'True',' and', 'as',' assert', 'break',' class', 'continue',' def', 'del',' elif', 'else',' finally', 'for',' from', 'global',' if', 'import',' in', 'is',' lambda', 'nonlocal',' not', 'or' 'pass', 'raise',' return', 'try',' while', 'with',' yield']

Let's see what happens by using keywords to define what happens.

> None=1

File "", line 1

SyntaxError: can't assign to keyword

Another thing to note: be careful with lowercase I and uppercase O, as it may be seen as 1 and 0, but ubuntu is easy to distinguish.

Generally speaking, in a company, we require variable names to be short and descriptive, for example, name is better than n and name_length is better than length_of_persons_name.

Basic data type

The following basic data types are included in Python 3:

Integer: for example, 100th, 200th, 0th, etc.

Boolean number: True or False

Floating point number: decimal, such as 1.5 and 2.5.

None: null, note that it is different from 0 and can be understood as an undefined value.

In addition to these four, there are other types that are not commonly used, such as plural, but because they are used less, we will not introduce them too much here.

Use variables and print

Enter python3 in the XFce terminal, enter the interactive environment, try to enter the following code, and understand the meaning of the output. Do not quit after execution. You need to continue the experiment in the next section:

> a = 10

> b = 10.6

> c = True

> d = None

>

10 10.6 True None

> print (type (a), a)

ten

> print (type (b), b)

10.6

> print (type (c), c)

True

> print (type (d), d)

None

In the above code, type is a built-in function in Python 3 that displays the data types of variables

Operation

Continue to do the following in the interactive environment of python 3 in the previous section to understand the mathematical operations in Python 3:

E = a + b

Print (e)

F = BBG a

Print (f)

G = b-a

Print (g)

H = b * a

Print (h)

You can see that integers are converted to floating-point numbers in the mixed calculation of integers and floating-point numbers.

In addition to mathematical operations, there are logical operations of and and or:

True and False

True or False

C and False

C or False

And representation and operation, only two operation values are True to return True, and or representation or operation, one of which is True returns True.

String

Strings in Python 3 can be marked with double or single quotation marks, and if quotation marks appear in the string, you can use\ to remove the special function of the quotation mark string.

Several representations of strings:

> str1 = "hello"

> str2 = 'xinsz08'

> str3 = 'hello.\' xinsz08\'

> str4 = "hello,'xinsz08'"

> str5 = 'hello, "" xinszou "'

Note that neither str4 nor str5 use\, but you can still use quotation marks in strings. I'm sure you've found out why.

What if you need to enter multiple lines of strings? You can try using three double quotes:

Str6 = "hello

Xinsz08 ""

Support the use of + connection string:

Str1 +''+ str2

Strings can be indexed using a number, the number 0 is the first character, and so on. The number-1 is the last character and is sliced with a colon:

Str1

Str1 [0]

Str1 [1]

Str1 [- 1]

Str1 [- 2]

Str1 [: 2]

Str1 [3:]

The location of slices can be confusing, so you need to try more slicing ways to understand it.

The built-in function len () in Python 3 gets the number of characters included in the string:

Len (str2)

Add:

Use the method to modify the case of a string

Xinsz08@xinsz08:~$vim name.py

Xinsz08@xinsz08:~$cat name.py

Name= "xinsz08love python"

Print (name.title ())

Xinsz08@xinsz08:~$python3 name.py

Xinsz08Love Python

You can see that the first letter of each word becomes capitalized.

What if it's all uppercase or all lowercase?

Name.upper () all uppercase

Name.lower () all lowercase

Xinsz08@xinsz08:~$vim name.py

Xinsz08@xinsz08:~$cat name.py

Name= "xinsz08love python"

Print (name.title ())

Print (name.upper ())

Print (name.lower ())

Xinsz08@xinsz08:~$python3 name.py

Xinsz08Love Python

XINSZ08LOVE PYTHON all uppercase

Xinsz08love python all lowercase

The Zen of python

The programming language Perl used to dominate the Internet for a long time, and most of the early interactive websites used Perl scripts. At that time, "there are many ways to solve the problem" was regarded as the motto of the Perl community.

This idea was once loved by everyone because the inherent flexibility of the language led to many different solutions to most problems. This flexibility was acceptable during the development project, but it was eventually realized that too much emphasis on flexibility could make large projects difficult to maintain: it was difficult, troublesome and time-consuming to figure out what the people who solved complex problems thought by studying the code.

Experienced programmers advocate avoiding complexity and simplicity as much as possible. The concept of the Python community is contained in the Zen of Python written by TimPeters. To learn about these guidelines for writing good Python code, simply execute the command import this in the interpreter.

> import this

The Zenof Python, by Tim Peters

Beautifulis better than ugly.

Explicitis better than implicit.

Simple isbetter than complex.

Complexis better than complicated.

Flat isbetter than nested.

Sparse isbetter than dense.

Readabilitycounts.

Specialcases aren't special enough to break the rules.

Althoughpracticality beats purity.

Errorsshould never pass silently.

Unlessexplicitly silenced.

In theface of ambiguity, refuse the temptation to guess.

Thereshould be one-- and preferably only one-- obvious way to do it.

Althoughthat way may not be obvious at first unless you're Dutch.

Now isbetter than never.

Althoughnever is often better than * right* now.

If theimplementation is hard to explain, it's a bad idea.

If theimplementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea-let's do more ofthose!

Summary:

How to use variables

How to create variables

What is a string?

Display strings in all uppercase, lowercase, and form

Thank you for your reading, the above is the content of the "basic knowledge summary of python variables", after the study of this article, I believe you have a deeper understanding of the basic knowledge summary of python variables, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report