Python Learning (c) variables and simple data types

Source: Internet
Author: User
Tags split words

Linux installation: Compiling and installing the installation package download

Python installation package:

Python-3.5.5.tgz

Tar zxvf python-3.6.4.tgz CD Python-3.6.4

Specifying the installation directory

./configure--prefix=/home/sshuser/python3.6 Make do install

Check version

/home/dxl/python3.5/bin/python3.6–v

Install the three-party library

Cd/home/dxl/python3.6/lib mkdir site-packages CD site-packages/

Third-party libraries are placed in the "Site-packages" folder

/home/dxl/python3.6/bin/python3 setup.py Install

Welcome to Print

Python 2

print "Hello World" Hello world!

Python 3

Print ("Hello world!") Hello world!

Variable

Mesage = "Hello world!"

Print (mesage)

Hello world!

Naming and use of variables

    1. Variable names can contain only letters, numbers, and underscores. Variable names can start with a letter underline and cannot begin with a number.
    2. Variable names cannot use spaces, you can use underscores to split words
    3. Variable name do not use Python keyword and function name as variable name
    4. Variable names should be short and descriptive. For example: Name and n both represent names, and name is the best.
    5. Use the lowercase letters "l" and "O" with caution, as the value "1" "0" is apt to be mistaken.
    # python写法    gf_of_oldboy = "chen chen"    # 驼峰写法    GFoFOldboy = "chen chen"    # 常量用全大写    PIE = "Jor Lei"~~
String

There is no difference between single and double quotes in Python

    1. Modify String case

Name = "Hello world!"

Capitalize the first letter of the word

Print (Name.title ())

Hello world!

Capitalize all letters in a word

Print (Name.upper ())

HELLO world!

Word All Letter Lowercase

Print (Name.lower ())

Hello world!

    1. string concatenation

Use "+" to merge strings, do not use "+", + open up multiple blocks of memory, inefficient

Namea = "Hello"

NAMEB = "World"

Full_name = Namea + "" + Nameb + "!"

Results Demo

Print ("===>>%s"%full_name)

===>>hello world!

info = "------Info of%s------name:%s age:%s jor:%s salary:%s"% (name,name,age,job,salary)

%s:字符串%d:数字%f:浮点(小数)
    1. Use tabs and line breaks to add whitespace
制表符“\t”,换行符“\n”

Print ("\npython")

Python

Print ("Languages:\npython\nc\njava")

Languages

Python

C

Java

Print ("Languages:\n\tpython\n\tc\n\tjava")

Languages

Python

C

Java

    1. Delete spaces in a string

Name = "Hello world!"

Remove trailing spaces

Print (Name.rstrip ())

Hello world!

Remove Head space

Print (Name.lstrip ())

Hello world!

Numeric integer

You can directly operate on integers in Python (+,-,*,/)

>>> 3 + 25>>>
Floating point number

Python will be a floating-point number with decimals

35 / 217.5>>> 35 / 2.017.5

The number of decimal digits contained may be indeterminate

>>> 35 / 2.0117.412935323383085>>> 0.2 +0.10.30000000000000004>>>
Use function ser () to avoid type errors

#打印数据类型

Age = Input ("Age:")

Print (Type (age))

age = Int (input ("Age:"))

#查询数据类型

Print (Type (age))

#查询并转换数据类型为字符串

Print (Type (age), type (str))

Age:15

<class ' str ' >

Age:15

<class ' int ' >

<class ' int ' > <class ' str ' >

Python Learning (c) variables and simple data types

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.