[Summary of Python Basics] Basic Data Types

1. [Summary of Python Basics] Basic Data Types-- python basic syntax


1.1 Identifiers
(1) Identifiers: Programmers themselves define words with specific meanings, such as class names, attribute names, variable names, and function names.
(2) Provision: It starts with a letter or underscore, and can be followed by letters, numbers, and underscores ; (the first question of the language test is basically to judge whether the definition of the identifier is correct) It cannot be the same as the keyword (such as class break if, etc.) ;Case Sensitive. For example: class Panda, def get_Panda_name , panda2=3
Note: The camel case naming method is introduced here, that is, an identifier name composed of multiple words should be lowercase for the first word, and the first letter of the following words is uppercase, such as: getPandaName , underscore combination is more commonly used in python)
1.2 Identifier syntax
(1) # : line comment
(4);: Write multiple statements on the same line (and python does not need to write a semicolon at the end of each statement!)
(5): separate code block header and body
( 6) Indentation: Code blocks (a code block between two braces) are represented by indentation, and different code blocks are separated by different indentation depths (so everyone should develop a good code style!)

2. [Summary of Python Basics] Basic Data Types-- Numerical type


2.1 Data Types
(1) Variables do not need to be declared. int a = 1; (C++) a = 1 (python)
(2) =: assignment operator (representing equality or ==)
(3) Allow multiple variable assignments

(4) Six data types in python: Number (number), String (string), List (list), Tuple (tuple), Sets (collection), Dictionary (dictionary)
2.2 Numerical value
(1) Four numeric types: int (integer), float (floating point), bool (boolean), complex (complex)
(2) type(): object type query ( typeid in C++ and typeof in JS are similar)
(3) Del: delete the object reference (analogous to C's delete)
( 4) Boolean type: True: 1, False: 0

(5) Symbolic constant: all uppercase letters, such as: PI=3.1415
2.3 Operators
(1) Arithmetic operator: ±*/% is still the same as C language, //returns the integer of the quotient, 5//3.0=1.0, is a power, 3 2=9
(Note: Let’s talk about division /, for example, C language is a compiled language, and the result of dividing an integer by an integer has been declared as an integer, and the result obtained at runtime can only be an integer; while python is a scripting language, it is obtained first The type is dynamically checked after the result, so what type the result should be)
(2) Bit operators (<<, &, etc.) and relational operators (==, >, <=, etc.) are the same as the C language
(3) Logical operators (change to English instead, the semantics are more direct)

3. String type
3.1 Creating Strings
(1) There is no difference between char and string in python, single quotes and double quotes both represent strings (double quotes are generally recommended)
3.2 String manipulation
(Take a="Hello" b="World" as an example)

3.3 String interception method
(Take str="0123456789" as an example)
(1) str[0:3], intercept the first to third characters, the result: 012
(2) str[:], intercept all characters of the string, the result: 0123456789
( 3) str[6:], intercept the seventh character to the end, the result: 6789
(4) str[:-3]), intercept from the beginning to the third last character, the result: 0123456
(5) str[-1], truncate the penultimate character, result: 9
(6) str[::-1]), create a string in the reverse order of the original string, the result: 9876543210
(7) str[-3:-1]) , truncate the characters before the third from the bottom and the first from the bottom, the result: 78
(8) str[-3:], intercept the third-to-last digit to the end, the result: 789
(9) str[:-5:-3], intercepted in reverse order, result: 96
Small Exercise: Palindromes
"Palindrome" is a string that reads the same forward and backward. How can we judge whether a string is a palindrome with the python knowledge we learned today?
str1 = input( "Enter a string: " )
str2 = str1 [:: -1 ]
if (str1==str2):
print( "is a palindrome" )
else :
print( "Not" )

Copyright statement: The content of this article is contributed by Alibaba Cloud real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00