This is the 13th day of my participation in the August More Text Challenge
If ❤️ my article is helpful, welcome to like, follow. This is the biggest encouragement for me to continue my technical creation. More previous articles in my personal column
Basic Python syntax
variable
# variable
width = 1280
height = 960
s = width * height # width by height
print( s )
Copy the code
The basic data
Python3 basic data is:
Don’t memorize, just understand. Then follow the practice, use how long will become familiar
- Number (Number)
- True = 1, False = 0
- Division in Python math
/
) returns a floating point number that you want to return by divisionThe integer
You need to use//
(4/2 # 4.0 8 // 2 # 4)
- String(String)
- String usage
'
或"
Either way, and\
It’s still an escape character - If you don’t want backslashes
\
To escape the content, add at the beginning of the variabler
Avoid being escaped - String index values from
0
Start with -1 as the end of the string +
For two string concatenation,*
You can copy the current string, followed by the number of times you need to copy it
- String usage
- The List (List)
[]
Creates an empty list- Elements (values) are written between square brackets, separated by commas
- Lists can be concatenated using the + operator
- Elements in a list can be added or deleted after they are created
- The Tuple (a Tuple)
(a)
Used to create an empty tuple- Tuples are similar to lists, but different from
The tuple cannot be modified
- Tuples can also be used
The index
andslice
, the method withThe List (List)
The same - Tuples can also be concatenated using the + operator
- Sets (set)
- Set () is used to create an empty collection
{value_1, ... }
- The set of
Unordered and not repeated
The sequence
- Set () is used to create an empty collection
- The Dictionary (Dictionary)
- {} is used to create an empty dictionary
{key_1:value_1, ... }
- Dictionaries (dict{}) are unordered
Key/value pair
A collection of - In the same dictionary
Key values (keys)
Must be the only
- {} is used to create an empty dictionary
string
# -*- coding: utf-8 -*-
print(""" hha """) # """ """ preserves content formats, such as newline Spaces, but "" does not
print("Here \n\t\t\t\t newline")
s="Moonlight by my bed."
print( s[4])Output: light (starting from 0, following the number)
print( s[-4])# output: before (starting from 0, counting backwards)
print( s[0:5:3])# Output: Bedmoon (from 0 to 5, every 3 bits)
user_1 = 'Joe'
user_2 = 'bill'
print( '{} says to {}, "Hello!" '.format( user_1, user_2 ) ) # Output: Joe says "Hello!"
print( f'{user_1}right{user_2}Say: "hello!" ' ) # Output: Joe says "Hello!"
print( 'are' + 'you' + 'ok' ) Use the + sign to concatenate the string
Copy the code