Learn Python the hard

Source: Internet
Author: User

Reference

Zed Shaw-learn Python the Hard

Ex0:the Setup

Windows

Python 2, Atom, Terminal.

EX1:A Good First Program

Keyword

1 Print 2 Print " "

Run the file

1 python ex1.py

Error

Garbled Chinese

Possible Solutions (unfortunately, none of them works.)

1. Add ASCII Encodings

1 # -*-coding:utf-8-*-

2. Decode & ENCODE01

1 Print ' English '. Decode ("utf-8"). Encode ("gb2312") 2 Print ' English '. Decode ("utf-8"). Encode ("gbk")

3. CHCP COMMAND01, 02

1 chcp 65001

01 about the Python output in the win terminal Chinese garbled problem

Windows Setup cmd command line encoding

Ex2:comments and Pound characters

Keywork

1 # 2 # Comments

Q: If # is for comments and then how come # -*-coding: utf-8 -*- works?

A: Python still ignores as code, but it's used as a kind of "hack" or workaround for problems with setting and detecting The format of a file. You also find a similar kind of the comment for editor settings.

Q: Why does the # in print "Hi # there." not get ignored?

A: The # in that code was inside a string, so it would be put-into the string until the ending "character was hit. These pound characters is just considered characters and aren ' t considered comments.

Ex3:numbers and Math

Keywords

1+#Plus2-#minus3/#Slash:7/4 = 1; 7.0/4.0 = 1.754*#Asterisk5%#percent; modulus:x divided by Y with J-remaining, the result of is the J part.6<#Less-than7>#Greater-than8<=#less-than-equal9>=#greater-than-equal

Ex4:variables and Names

Ex5:more Variables and Printing

Keywords

1 ' Black ' 2 ' Black ' 3 Print " She ' s got%s eyes and%s hair. " % (My_eyes, My_hair)

Operations String Formatting

Differences between%r and%s in Python

Learn Python the hard

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.