Basic python knowledge and basic python tutorials

Source: Internet
Author: User
Tags cmath

Basic python knowledge and basic python tutorials

Python has been used many times. I know all the basic things. However, if I have used it, I have mastered all the details. Today, we will go over python from start to end. From the original basic knowledge of python.

Although python3.x has been around for a long time, the most popular python version is still python2.x due to the poor popularity. I have generally used python2.7. Therefore, the syntax involved in the next step is python2.7.

from __future__ import division
  

After the introduction, the division operation results must be decimal. If you want to keep only the entire number of digits. Yes.

1 // 2 # The result is 0.
  

Power Operation

2 ** 3 is faster than pow (2, 3. We recommend that you use the former.

  

Hexadecimal numbers start with 0x and Octal numbers start with 0. Review.

  

Note the differences between the math module and the cmath module.

math.sqrt(-1)cmath.sqrt(-1)
  

If you want to directly run the python program when running the script without explicitly calling python, add the following sentence:

#!/usr/bin/env python

 

This script can be automatically executed if it has the executable permission. For example

Chmod a + x hello. py # Add executable permissions to all users. a stands for all, all, and x stands for executable permissions.
Hello. py # Simply write this sentence to enable python without using python hello. py.
  

Let's briefly talk about the repr function. I 've been confused by such a simple function for a long time. Essentially, it creates a new string that stores the value of a python expression in the string. Using the repr function, you can easily guide the expression result type

print repr(“hello”)

Result 'hello'

What if I use the str that I often confuse?

print str("hello")

 

Result hello. Note: no single quotes are provided.

Another example

print repr(1000L)print str(1000L)

 

1000L
1000

  

Input is not particularly useful. It is best to use raw_input when needed.

Next let's talk about string processing. Three single quotes in a row can be used for long strings. When the character string has a backslash \, you must note the escape. If you do not want to escape the character string

print r'hello'

 

Add an r to solve this problem.

Okay, so many reviews at the beginning. These are all details about python.

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.