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 integerYou 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 variablerAvoid being escaped
    • String index values from0Start 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
  • 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 fromThe tuple cannot be modified
    • Tuples can also be usedThe indexandslice, 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 ofUnordered and not repeatedThe sequence
  • The Dictionary (Dictionary)
    • {} is used to create an empty dictionary{key_1:value_1, ... }
    • Dictionaries (dict{}) are unorderedKey/value pairA collection of
    • In the same dictionaryKey values (keys)Must be the only

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