程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python basics variables and simple data types

編輯:Python

Variable : Each variable stores a value —— Information associated with a variable

So let's set up a python file , be known as hello_world.py, Let's enter a piece of code first , use python To run it .

print("hello python world")

Run the above code , We'll see the following output :

hello python world

So we're done python The first line of code outputs , Now let's try in hello_world.py Use a variable in the , The contents are as follows :

message = "hello python world"
print(message)

Run this program , We see the output :

hello python world

The output is the same as before , This is where we added a new one called message The variable of , The value stored in the variable is “hello python world”, Same value as the previous output , The creation of variables only provides a temporary storage space for text , It can simplify the procedure , Make the program more readable , More understanding . In the program, the value of a variable can be changed at any time , and python The latest value of the variable will always be recorded . for example :

message = "hello python world"
print(message)
message = "how are you"
print(message)

Run this program , as follows :

hello python world
how are you

According to the above example , We can see : In the program, the value of a variable can be changed at any time , and python The latest value of the variable will always be recorded .

Variable naming and use

When we use variables to name them , There are also rules and guidelines to follow , Breaking these rules will lead to errors , The guidelines make the code you write easier to understand and read . Like any other language , The following rules apply to variable naming :

  1. Variable names can only contain letters 、 Numbers and underscores . Variable names can start with letters or underscores , But don't start with numbers .
  2. Variable names cannot contain spaces , But you can use underscores to separate the words .
  3. Don't put Python Keywords and function names are used as variable names , Do not use Python Keep words for special purposes ( Please refer to Baidu for specific keywords ).
  4. Variable names should be short and descriptive .
  5. Use capital letters with caution I And letters O, They may be seen as 1 and 0.

character string

A string is a series of characters . stay Python in , What is caused by parentheses is a string . The quotation marks can be single quotation marks or double quotation marks , as follows :

"this is a string"
'this is also a string'

Use the method to modify the case of the string

For strings , The simplest operation you can perform is to change the case of words in it , The following describes how to change the case of strings .

String words are capitalized :title()

word = "love you"
print(word.title())

Run this program , Output is as follows :

Love You

It can be seen that , The first letter of both words in the string is capitalized , therefore title() The function of the is to display each word in capital letters .

Change the string to all uppercase or all lowercase :upper(), lower()

word = "love you"
print(word.upper())
print(word.lower())

Run the code , Output is as follows :

LOVE YOU
love you

therefore ,upper() Change all strings to uppercase , and lower() Change all strings to lowercase , The two functions are just the opposite .

Merge ( Splicing ) character string

In most cases , We need to merge strings , stay Python We Use plus sign (+) Merge strings :

first_word = "love"
last_word = "you"
full_word = first_word + '' + last_word
print(full_word)

function , Output :( Spaces enclosed in single quotation marks are used to separate the combined two characters )

love you

This method of combining strings is called concatenation . By joining together , You can use information stored in variables to create complete information :

first_word = "love"
last_word = "you"
full_word = first_word + '' + last_word
print("I " + full_word.title())

function , Output :

I Love You

We can create and output a complete message by string merging , You can also put the spliced whole message in a variable .

Use tabs and line breaks to add white space

To add tabs to a string , Character combinations can be used \t   ,    To add a newline character to a string , Character combinations can be used \n.

The specific use will not be introduced here

Delete blanks

Python Be able to find extra white space at the beginning and end of a string , want Make sure there is no white space at the end of the string , Serviceable method rstrip().

>>> favorite_language = "python "
>>> favorite_language
'python '
>>> favorite_language.rstrip()
'python'
>>> favorite_language
'python '

We can see that , Deleting spaces is only temporary , To permanently delete whitespace in a string , The result of the delete operation must be saved back to the variable :

>>> favorite_language = "python "
>>> favorite_language = favorite_language.rstrip()
>>> favorite_language
'python'

Remove the blank space at the beginning of the string lstrip() , Remove whitespace at the end of the string rstrip(), Delete whitespace at both ends of the string strip().

In programming , You often need to change the value of a variable , Save the new value back into the original variable , This is why the value of a variable may change as the program runs or the user enters data .

Using functions str() Avoid typos : Call function str(), It makes Python Represents a non string as a string .

View keywords

import keyword
keyword.kwlist

Variable assignment

Keyed assignment :name = user = 'petter'

Sequence unpacking assignment :name, age = 'jake', 21

You can use sequence unpacking assignment to exchange values between variables

a = 10
b = 5
a, b = b, a

Variable : To save things , Carrying data ,

in other words , When a constant pointed to by an identifier is replaced by another constant , The original constant will no longer occupy memory .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved