美文网首页
Python Notes (1) - Syntax and St

Python Notes (1) - Syntax and St

作者: SilentSummer | 来源:发表于2018-03-18 04:53 被阅读14次

    The note introduces basic Python syntax and strings.

    Python notes of open courses @Codecademy.

    Brief Introduction

    Python is a high level scripting language with object oriented features.

    Python programs can be written using any text editor and should have the extension .py.

    You may click to Python Basics for more details.

    Python Syntax

    • Variables and Data Types

      • Var_name = X. X could be int 5 / float 1.23 / boolean True (or False)
    • Whitespace

      • Important. Have to be careful with how to use it.
      • Should indent your code with correct (usually four) spaces.
    • Comments

      • Single Line: #
      • Multi-Line: """ bla...bla... """
    • Operators and Maths

      • Add, subtract, multiply, divide numbers like +, -, *, /

      • Exponentiation: **

      • Modulo (which returns the remainder from a division): %

        # Examples for `/`
        5 / 2
          # 2
          
          5.0 / 2
          # 2.5
          
          float(5) / 2
          # 2.5
        
      • When you divide an integer by another integer, the result is always an integer (rounded down, if needed).

        • When you divide a float by an integer, the result is always a float.
        • To divide two integers and end up with a float, you must first use float() to convert one of the integers to a float.
    • External Resources

    Strings

    • Strings

      • String: can contain letters, numbers, and symbols. A string could created by 'String', "String", or str(3.14).
      • Escaping Characters: 'There's' should be written as 'There\'s'
      • Access by Index: In Python, we start counting the index from zero instead of one. E.g., c = "cats"[0].
    • String Methods

      • len(): gets the length (the number of characters) of a string. E.g., len(str_name) or len("String")
      • lower(): gets rid of all the capitalization in strings. E.g., str_name.lower()
      • upper(): makes a string completely upper case. E.g., str_name.upper()
      • str(): turns non-strings into strings. E.g., str(str_name)
      • Dot Notation: Methods that use dot notation only work with strings. Additionally, len() and str() can work on other data types.
    • Print

      • print: simply displays your code in the console.

      • String Concatenation: The + operator between strings will 'add' them together, one after the other. E.g., print "I " + "love " + "python", you might get I love python

      • Explicit String Conversion: combines a string with something that isn't a string using str(). E.g., print "I love python " + str(3.0), you might got I love python 3.0

      • String Formatting with %: The % operator after a string is used to combine a string with variables. The % operator will replace a %s in the string with the string variable that comes after it. E.g.,

        ```python
        # Example 1
        string_1 = "you"
        string_2 = "me"
        
        print "I love %s! Do you love %s?" % (string_1, string_2)
            
        # Example 2  
        name = raw_input("Question1")
        quest = raw_input("Question2")
        color = raw_input("Question3")
        
        print "Answer to Q1 is %s, answer to Q2 is %s, " \
        "and answer to Q3 is %s." % (A1, A2, A3)
        ```
        
    • Date and Time

      • datetime: keeps track of time when something happened.

      • datetime.now(): to retrieve the current date and time.

      • Extracting Information: year, month, day, hour, minute, and second from datetime.now().

      • Print as yyyy-mm-dd, mm/dd/yyyy and hh:mm:ss: see examples below

        ```python
        # Using the first row to import the **datetime library**.
        from datetime import datetime
        
        now = datetime.now()
        
        # Extracting Information
        current_year = now.year
        current_month = now.month
        current_day = now.day
        
        # print date as `yyyy-mm-dd` and `mm/dd/yyyy`
        print '%s-%s-%s' % (now.year, now.month, now.day)
        print '%s/%s/%s' % ( now.month, now.day, now.year)
        
        # print time as `hh:mm:ss`
        print '%s:%s:%s' % ( now.hour, now.minute, now.second)
        ```
        

    External Resources

    相关文章

      网友评论

          本文标题:Python Notes (1) - Syntax and St

          本文链接:https://www.haomeiwen.com/subject/dbeytxtx.html