Getting started with python

Source: Internet
Author: User

>>> 2+24>>> # This is a comment... 2+24>>> 2+2  # and a comment on the same line as code4>>> (50-5*6)/45.0>>> 8/5 # Fractions aren't lost when dividing integers1.6

To do integer division and get an integer result, discarding any fractional result, there is another operator ,//:

 

[Python]
>>># Integer division returns the floor:
... 7 // 3
2
>>> 7 //-3
-3

>>># Integer division returns the floor:
... 7 // 3
2
>>> 7 //-3
-3

Complex numbers are also supported; imaginary numbers are written with a suffix ofj orJ. complex numbers with a nonzero real component are written as (real + imagj), or can be created with thecomplex (real, imag) function.
 

[Python]
>>> 1j * 1J
(-1 + 0j)
>>> 1j * complex (0, 1)
(-1 + 0j)
>>> 3 + 1j * 3
(3 + 3j)
>>> (3 + 1j) * 3
(9 + 3j)
>>> (1 + 2j)/(1 + 1j)
(1.5 + J)

>>> 1j * 1J
(-1 + 0j)
>>> 1j * complex (0, 1)
(-1 + 0j)
>>> 3 + 1j * 3
(3 + 3j)
>>> (3 + 1j) * 3
(9 + 3j)
>>> (1 + 2j)/(1 + 1j)
(1.5 + J)

To extract these parts from a complex number z, usez. real andz. imag.

[Python]
>>> A = 1.5 + 0.5j
>>> A. real
1.5
>>> A. imag
0.5

>>> A = 1.5 + 0.5j
>>> A. real
1.5
>>> A. imag
0.5

The last printed expression is assigned to the variable _

[Python]
>>> Tax = 12.5/100
>>> Price = 100.50
>>> Price * tax
12.5625
>>> Price + _
113.0625
>>> Round (_, 2)
113.06

>>> Tax = 12.5/100
>>> Price = 100.50
>>> Price * tax
12.5625
>>> Price + _
113.0625
>>> Round (_, 2)
113.06

2: Strings
[Python]
> 'Spam eggs'
'Spam eggs'
>>> 'Doesn \ 't'
"Doesn' t"
>>> "Doesn' t"
"Doesn' t"
>>> '"Yes," he said .'
'"Yes," he said .'
>>> "\" Yes, \ "he said ."
'"Yes," he said .'
>>> '"Isn \' t," she said .'
'"Isn \'t," she said .'

>>> 'Spam eggs'
'Spam eggs'
>>> 'Doesn \ 't'
"Doesn' t"
>>> "Doesn' t"
"Doesn' t"
>>> '"Yes," he said .'
'"Yes," he said .'
>>> "\" Yes, \ "he said ."
'"Yes," he said .'
>>> '"Isn \' t," she said .'
'"Isn \'t," she said .'
Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line:

[Python]
>>> Hello = "this is not \ n \
Asdfasdf \ n \
Aksdfasd"
>>> Print (hello );

>>> Hello = "this is not \ n \
Asdfasdf \ n \
Aksdfasd"
>>> Print (hello); [python] view plaincopyprint? This is not
Asdfasdf
Aksdfasd

This is not
Asdfasdf
Aksdfasd

A "raw" string:

[Python]
>>> Hello = r "this is not \ n \
Asdfasdf \ n \
Aksdfasd"
>>> Print (hello );

>>> Hello = r "this is not \ n \
Asdfasdf \ n \
Aksdfasd"
>>> Print (hello); [python] view plaincopyprint? This is not \ n \
Asdfasdf \ n \
Aksdfasd

This is not \ n \
Asdfasdf \ n \
Aksdfasd

Strings can be concatenated (glued together) with the + operator, and repeated *:

[Python]
>>> Hello = 'hello ';
>>> World = 'World ';
>>> Hello = hello + world;
>>> Hello
'Helloworld'

>>> Hello = 'hello ';
>>> World = 'World ';
>>> Hello = hello + world;
>>> Hello
'Helloworld' [python] view plaincopyprint? >>> '<' + Hello * 5 + '>'
'<HelloWorldHelloWorldHelloWorldHelloWorldHelloWorld>'

>>> '<' + Hello * 5 + '>'
'<HelloWorldHelloWorldHelloWorldHelloWorldHelloWorld>'

 

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.