Python Notes from scratch (i)

Source: Internet
Author: User
Tags logical operators

Author identity: Beginner python, rookie

=================================================

1. >>> main prompt - wait for input Next statement

... Secondary prompt- waits for the remainder of the current statement to be entered

2. Python Two ways: statements and Expressions

2-1 Print statement complete Hello World:

>>> print ("Hello world!")

2-2 expression does not have a keyword

Can be a mathematical operator to form an arithmetic expression

Functions called by parentheses

(Note: A function that does not specify a return value returns None equivalent to NULL)

Call ABS () to return the absolute value function:

1 >>> abs ( -4)2 43 >>> abs ( -12) + 4 5 >>> ABS (3/2)6 1.57 >>>

3. Program output

View string contents with print :

>>> myString = ' Hello string! ' >>> print (myString) Hello string!

You can also view this:

>>> myString ' Hello string! ' >>>

Note: string contents in single quotation marks

4. In the expression, the underscore _ denotes the value of the last expression, followed by:

>>> _ ' Hello string! '

5. Use of %s

>>> Print ("%s is%d"% ("Bro.young", 1)) Bro.young was 1>>> print ("%s is%d"% (7788,1)) 7788 is 1

(Note: You can pass a number to%s)

>>> print ('%s is%d '% (77.88,1)) 77.88 is 1

(Note: Floating-point type is also possible)

6. Program input (built-in function input())

Get user input data :

2.x series using raw_input ()

>>> user = Raw_input (' Please Enter Your Name: ')

(Note: The 3.x series is not available raw_input() because the 3.x series is no longer using raw_input ( ) but instead uses input () )

3.x series using input ()

>>> user = input (' Please enter Your name: ') "Please enter Your name:

You can enter your name at this time Bro.young

7. Enter a numeric string

The previous example is limited to text input, and the following is a numeric string input

>>> number = input (' Please enter Your number: ') Please enter Your number:1234>>>print (' Your number is:%d '% (int (number) *) Your number is:2468

(Note: int () converts a numeric string to an integer value)

8. View the Help documentation

Call Help (), using input as an example, as follows:

>>> Help (Input)

9. # Symbol comment (starting from # until line ends)

>>> #one Word ... print (' Single Dog ') a single dog

10. Standard Operators

+  -  *  /  //  %   **

Add, subtract, multiply, divide, and withdraw are standard operators

/- Traditional Division

- Floating point Division (rounding)

* *- exponentiation operator

>>> Print ( -1*2+3**3) 25

11. Standard comparison operator - Returns a Boolean value

< <= > >= = = = = <>

>>> 1<2true>>> 1<= 2true>>> 1>2false

(Note: <> is not equal to operator abc/pascal style, basically eliminated )

12. Logical operators can connect any expression to return a Boolean value

And Or not

>>> 1 = = 2false>>> 1!=2true>>> 1<2 and 1==2false>>> 1<2 or 1==2true>> > Not 6.5<2true>>> 1<2<3true

Python is a dynamic language - There is no need to pre-declare the variable type, and the type and value of the variable are initialized at the moment of assignment.

>>> name = ' Tom ' >>> age = 20>>> Peer = ' senior student ' >>> print ("%s is%d,%s"% (name , Age,peer)) Tom is 20,senior student>>> print ('%s was%d,%s '% (Name,age,peer)) Tom is 20,senior student

14. Support for incremental assignment - operators and equals signs are merged together

Example:n *=

>>> n=1>>> n *= 10>>>--n10>>>-n-10>>> ++n10

(Note: self-increment (+ +) self-decrement (--) is not supported,such as:--n is considered to be -(-N ) is n )

15. Five basic types of numbers

INT- signed integral type

Long- integer

BOOL- Boolean type

Float- Float type

Complex- plural

(Note 1: The long integer in Python is larger than the number represented by a long integer in the C language, and its size is limited by computer memory)

: Boolean Span style= "FONT-FAMILY:CALIBRI;" >true and false put in numeric context true 1 false 0 )

16. Use the index operator ([]) and the slice operator ([:]) to get a string rule: The index of the first character is 0, the last character is indexed -1 .

The plus sign (+) is used for string join operations asterisk (*) for string repetition

[Start:end]- access range from start to end(not including end) for all elements

>>> str = ' ABCD ' >>> str[1:2] ' B ' >>> str[1:3] ' BC ' >>> str[1:4] ' BCD ' >>> str[ -3:-1] ' BC ' >>> str2 = ' EFG ' >>> str + str2 ' ABCDEFG ' >>> str*2 ' abcdabcd ' >>> ' _ ' *10 ' ___ _______‘

(Note: [:] 0 can not write)

>>> Str[0:2] ' ab ' >>> str[:2] ' ab '

17. Lists and tuples <==> Arrays can hold any number of Python objects of any type starting from 0 to access elements

Differences from arrays: lists and tuples can hold different types of objects

18. The dictionary is a mapping data type in Python, consisting of a key-value pair (key-value). The value can be any type of Python object, and the dictionary element is wrapped with {}

>>> mydict = {' Hello ': ' Host '}>>> mydict{' hello ': ' Host '}>>> mydict[' world ' = 100>> > mydict{' World ': +, ' hello ': ' Host '}

  

Python Notes from scratch (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.