Python Course design Notes (iii) integers, floating-point numbers, and strings

Source: Internet
Author: User
Tags month name

Integer type (range unlimited)

Decimal 1-1

Binary 0b1-0b1

Octal 0o1-0o1

Hex 0x1-0x1

Floating-point type (limited in scope but negligible)

  an indeterminate mantissa is present in the operation:0.1+0.2!=0.3

Cause: Binary means that decimals can be infinitely close but not identical, generating indeterminate decimals during conversion

How to avoid: floating point key operation and comparison with round () function for auxiliary round (x, D) #对x进行四舍五入, D is to intercept the number of decimal digits

Scientific notation for floating-point numbers <a>e<b> # a*10^b

Numeric Operation operators

10/3 = 3.3333

10//3 = 3

X**y #x ^y

mixed operations between types: Generate results widest type complex number > floating point number > Integer 123

  

Numeric arithmetic functions

Absolute ABS ()

Shangyu divmod (x, y) output (quotient, remainder)

Power Surplus pow (x,y[, z]) output (x**y)%z for the last few of X**y

Rounding round (x [, d]) x reserved d decimal, default D is 0

Maximum minimum value max/min (x1,x2,x3,..)

cast int (decimal, string) discards decimals directly

Float (integer, String) is an integer plus a fractional part

String type

  Representation method:

1) A pair of single quotes, a pair of double quotation marks, representing only a single line string

2) A pair of three single quotes, or three double quotation marks, representing multiple lines of string

  the ordinal of a string ( two sequence numbers exist simultaneously in a string ): Forward increment inverse decrement (last-1, left- -2,-3,,,)

  Index and slice of a string

Index "Hello" "0" or str "0"

slicing (slice)     (copyright:https://www.cnblogs.com/cedrelaliu/p/6004352.html)

A slice operation (slice) can get a substring (part of a string) from a string. Start offset start, end offset end, optional step step define a shard.

Format: [Start:end:step]

      • [:] Extracts the entire string from the beginning (default position 0) to the end (default position-1)
      • [Start:] Extract from start to end
      • [: end] Extract from the beginning to End-1
      • [Start:end] extracted from start to end-1
      • Start:end:step] Extract from start to end-1, each step character extracts one

Special: Reverses the string by reversing the string (reverse) by setting the step to a negative number: [::-1]

  

  Operators for string Operations:

X+y: Link Two strings

N*x or X*n: Copy n-Times string X

Whether x in S:x is a substring of s, returns TRUE or False

 String handling functions

Len (x): The length of the string x, and the length of a Chinese character and an English letter is 1

STR (x): string coercion type conversion, any type x converted to string = = = Opposite eval ()

Hex (x) and Oct (x): Converts x into octal and hexadecimal form

Chr (U): x is Unicode encoded and returns its corresponding characters

Ord (x): X is a character that returns the corresponding Unicode encoding

Note: Python uses Unicode encoding uniformly

Str.lower () Str.upper ()

Str.split ("")

Str.count ("a")

Str.replace (old,new)

Str.center (Width[,fillchar])

Str.strip ("=pn" _

Str.join ("1234") STR join to "1234"

  

  Formatting of the string output

    slot : "< {} {} >". Format ("Ch1", "CH2")

    Format control parameters in slots

      

    

Time Library:

Standard library for processing time

 Time Acquisition:

Time () #获取时间戳: A floating-point number, in seconds, starting from 1970

CTime () #获取一可读的字符串 Mon May 14 22:51:40 2018

Gmtime () #其他程序可利用的时间格式 time.struct_time (tm_year=2018, tm_mon=5, tm_mday=14, tm_hour=14, tm_min=52, tm_sec=25, tm_wday= 0, tm_yday=134, tm_isdst=0)

  

  Time formatting:

Show time in a reasonable way

Strftime (Tpl,ts) #tpl是输出格式模板 (String) str is gmtime ()

Controls in the TPL:

%y year,%m month,%b month name,%b month name abbreviation ...

  Program Timings:

Measurement Time Perf_counter () #返回CPU级别的精确时间计数值, units in seconds, difference is meaningful

Sleep Time.sleep (time) #停滞t秒

Instance:

The creation of a text progress bar

Import Timeprint ("---execution start---") Scale=TenStart=Time.perf_counter () forIinchRange (scale+1): Du= Time.perf_counter ()-Start a='*'*I b="."* (scale-i) C= (i/scale) * -Print ("\r{:>3.0f}%[{}->{}]{:.2f}s". Format (C,A,B,DU), end=" ") Time.sleep (0.32)

Controlling the Print function

\ r cursor returns to the beginning of the current line

Print (< string >,end= "printing end character" >

Note that in idle in order to display the full information auto-mask \ r, the py file is run on the command line

  

  

Python Course Design notes (three) integers, floating-point numbers, and strings

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.