Python for Data Analysis CHINA UNIVERSITY OF PETROLEUM, BEIJING Zhu, Dandan College of Information Science and Engineering / College of Artificial Intelligence 2024 Spring
Chapter 2 Python Language Basics, IPython, and Jupyter Notebooks • 2.1 The Python Interpreter • 2.2 IPython Basics • 2.3 Python Language Basics • 2.4 Conclusion
Chapter 2 Python Language Basics, IPython, and Jupyter Notebooks • Much of this course focuses on table-based analytics and data preparation tools for working with datasets that are small enough to fit on your personal computer. • Sometimes you need to do some wrangling to arrange messy data into a more nicely tabular (or structured) form in the task of data analysis. Job seeker data Wrangling
Chapter 2 Python Language Basics, IPython, and Jupyter Notebooks • Some of the tools are best explored from a live IPython or Jupyter session. • Note: There are introductory Python concepts that this chapter does not cover, like classes and object-oriented programming, which you may find useful in your foray into data analysis in Python. Official Python tutorial: https:/ /docs.python.org/3/
2.1 The Python Interpreter • Python is an interpreted language. • The Python interpreter runs a program by executing one statement at a time. • The standard interactive Python interpreter can be invoked on the command line with the python command: • To exit the Python interpreter, you can either type exit() or press Ctrl-D (works on Linux and macOS only). The prompt after which you’ll type code expressions.
2.1 The Python Interpreter Running Python programs • Calling python with a .py file as its first argument. • IPython, an enhanced Python interpreter, or Jupyter notebooks. IPython %run command The prompt after which you’ll type code expressions.
2.2 IPython Basics Running the IPython Shell • You can launch the IPython shell on the command line just like launching the regular Python interpreter except with the ipython command. • You can execute arbitrary Python statements by typing them and pressing Enter.
2.2 IPython Basics Running the IPython Shell Python code statements creates a variable named data that refers to a newly created list prints the value of data in the console more readable, or pretty-printed Data variable in the standard Python interpreter
2.2 IPython Basics Running the Jupyter Notebook • One of the major components of the Jupyter project is the notebook, a type of interactive document for code, text, data visualizations, and other output. • The Jupyter notebook interacts with kernels, which are implementations of the Jupyter interactive computing protocol specific to different programming languages. The Python Jupyter kernel uses the IPython system for its underlying behavior.
2.2 IPython Basics Running the Jupyter Notebook • To start up Jupyter, run the command jupyter notebook in a terminal. On many platforms, Jupyter will automatically open in your default web browser (unless you start it with --no-browser). Otherwise, you can navigate to the HTTP address printed when you started the notebook.
2.2 IPython Basics Running the Jupyter Notebook • To create a new notebook, click the buttons: File - New - notebook – double click the created notebook - select the "Python 3" option. • Entering a line of Python code, then press Shift-Enter to execute it.
2.2 IPython Basics Running the Jupyter Notebook • When you save the notebook, it creates a file with the extension .ipynb. This is a self- contained file format that contains all of the content (including any evaluated code output) currently in the notebook. • When you want to close a notebook, select ”Shut Down Kernel." If you simply close the browser tab, the Python process associated with the notebook will keep running in the background.
2.2 IPython Basics Tab Completion • One of the major improvements over the standard Python shell is tab completion, found in many IDEs or other interactive computing analysis environments. • While entering expressions in the shell, pressing the Tab key will search the namespace for any variables (objects, functions, etc.) matching the characters you have typed so far and show the results in a convenient drop-down menu. Also, you can complete methods and attributes on any object after typing a period (.).
2.2 IPython Basics Introspection • Using a question mark (?) before or after a variable will display some general information about the object. This is referred to as object introspection. • If the object is a function or instance method, the docstring, if defined, will also be shown.
2.2 IPython Basics Introspection • ? has a final usage, which is for searching the IPython namespace in a manner similar to the standard Unix or Windows command line. A number of characters combined with the wildcard (*) will show all names matching the wildcard expression.
2.3 Python Language Basics An overview of essential Python programming concepts and language mechanics. Language Semantics The Python language design is distinguished by its emphasis on readability, simplicity, and explicitness. • Indentation, not braces: - Python uses whitespace (tabs or spaces) to structure code instead of using braces as in many other languages like R, C++, Java, and Perl.
2.3 Python Language Basics - A colon (:) denotes the start of an indented code block after which all of the code must be indented by the same amount until the end of the block. - IPython and Jupyter notebooks will automatically insert four spaces on new lines following a colon and replace tabs by four spaces. - Python statements do not need to be terminated by semicolons. Semicolons can be used, however, to separate multiple statements on a single line. Putting multiple statements on one line is generally discouraged in Python as it can make code less readable.
2.3 Python Language Basics Language Semantics • Everything is an object: - An important characteristic of the Python language is the consistency of its object model. Every number, string, data structure, function, class, module, and so on exists in the Python interpreter in its own “box,” which is referred to as a Python object. Each object has an associated type (e.g., integer, string, or function) and internal data. In practice this makes the language very flexible, as even functions can be treated like any other object.
2.3 Python Language Basics Language Semantics • Comments: - Any text preceded by the hash mark (pound sign) # is ignored by the Python interpreter. This is often used to add comments to code. At times you may also want to exclude certain blocks of code without deleting them. One solution is to comment out the code. - Comments can also occur after a line of executed code.
2.3 Python Language Basics Language Semantics • Function and object method calls: - You call functions using parentheses () and passing zero or more arguments, optionally assigning the returned value to a variable. - Almost every object in Python has attached functions, known as methods, that have access to the object’s internal contents. You can call them using the following syntax: - Functions can take both positional and keyword arguments: (We will look at this in more detail later)
2.3 Python Language Basics Language Semantics • Variables and argument passing: - When assigning a variable (or name) in Python, you are creating a reference to the object shown on the righthand side of the equals sign. In practical terms, consider a list of integers: - Suppose we assign a to a new variable b: - In some languages, the assignment of b will cause the data [1, 2, 3] to be copied. In Python, a and b actually now refer to the same object, the original list [1, 2, 3] Two references for the same object
2.3 Python Language Basics Language Semantics • Variables and argument passing: - When you pass objects as arguments to a function, new local variables are created referencing the original objects without any copying.
2.3 Python Language Basics Language Semantics • Dynamic references, strong types: - Variables in Python have no inherent type associated with them; a variable can refer to a different type of object simply by doing an assignment. There is no problem with the following:
2.3 Python Language Basics Language Semantics • Dynamic references, strong types: - Variables are names for objects within a particular namespace; the type information is stored in the object itself. Some observers might hastily conclude that Python is not a “typed language.” This is not true; consider this example: In some languages, the string ‘5’ might get implicitly converted (or cast) to an integer, thus yielding 10. In other languages the integer 5 might be cast to a string, yielding the concatenated string ‘55‘. In Python, such implicit casts are not allowed. In this regard we say that Python is a strongly typed language, which means that every object has a specific type (or class).
2.3 Python Language Basics Language Semantics • Dynamic references, strong types: - Implicit conversions will occur only in certain permitted circumstances, such as: Even though b is an integer, it is implicitly converted to a float for the division operation.
2.3 Python Language Basics Language Semantics • Dynamic references, strong types: - Knowing the type of an object is important, and it’s useful to be able to write functions that can handle many different kinds of input. You can check that an object is an instance of a particular type using the isinstance function: isinstance can accept a tuple of types if you want to check that an object’s type is among those present in the tuple:
2.3 Python Language Basics Language Semantics • Attributes and methods: Objects in Python typically have both attributes and methods. - Attributes: other Python objects stored “inside” the object. - Methods: functions associated with an object that can have access to the object’s internal data. Both of them can be accessed in two ways: ① Syntax <obj.attribute_name>: ② Name via the getattr function:
2.3 Python Language Basics Language Semantics • Duck typing: Often you may not care about the type of an object but rather only whether it has certain methods or behavior. To verify that an object is iterable by using the iter function:
2.3 Python Language Basics Language Semantics • Imports: - Module: simply a file with the .py extension containing Python code. - If we wanted to access the variables and functions defined in some_module.py, from another file in the same directory we could do: /
2.3 Python Language Basics Language Semantics • Imports: - By using the as keyword, you can give imports different variable names:
2.3 Python Language Basics Language Semantics • Binary operators and comparisons: - Most of the binary math operations and comparisons use familiar mathematical syntax used in other programming languages:
2.3 Python Language Basics Language Semantics • Binary operators and comparisons: - To check if two variables refer to the same object, use the is keyword. Use is not to check that two objects are not the same: - Comparing with is is not the same as the == operator The list function always creates a new Python list (i.e., a copy) A common use of is and is not is to check if a variable is None, since there is only one instance of None:
2.3 Python Language Basics Language Semantics • Mutable and immutable objects: - Mutable objects: Many objects in Python, such as lists, dictionaries, NumPy arrays, and most user-defined types (classes), are mutable. This means that the object or values that they contain can be modified. - Immutable objects: Others, like strings and tuples, are immutable, which means their internal data cannot be changed. Trying to avoid side effects and favor immutability, even though there may be mutable objects involved.
2.3 Python Language Basics Scalar Types Python has a small set of built-in types for handling numerical data, strings, Boolean (True or False) values, and dates and time. These "single value" types are sometimes called scalar types. Standard Python scalar types
2.3 Python Language Basics Scalar Types • Numeric types: - The primary Python types for numbers are int and float. Ø An int can store arbitrarily large numbers: Ø Floating-point numbers are represented with the Python float type. Under the hood, each one is a double-precision value. They can also be expressed with scientific notation: Ø Integer division not resulting in a whole number will always yield a floating-point number: Ø To get C-style integer division (which drops the fractional part if the result is not a whole number), use the floor division operator / /:
2.3 Python Language Basics Scalar Types • Strings: - Python strings are immutable; you cannot modify a string. - If we need to modify a string, we have to use a function or method that creates a new string, such as the string replace method: Afer this operation, the variable a is unmodified:
2.3 Python Language Basics Scalar Types • Strings: - Many Python objects can be converted to a string using the str function: - Strings are a sequence of Unicode characters and therefore can be treated like other sequences, such as lists and tuples: The syntax s[:3] is called slicing and is implemented for many kinds of Python sequences.
2.3 Python Language Basics Scalar Types • Strings: - The backslash character is an escape character, meaning that it is used to specify special characters like newline n or Unicode characters. To write a string literal with backslashes, you need to escape them: - If you have a string with a lot of backslashes and no special characters, you might find this a bit annoying. Fortunately you can preface the leading quote of the string with r (stands for raw), which means that the characters should be interpreted as is:
2.3 Python Language Basics Scalar Types • Strings: - Adding two strings together concatenates them and produces a new string:
2.3 Python Language Basics Scalar Types • Strings: - String objects have a format method that can be used to substitute formatted arguments into the string, producing a new string: - To substitute arguments for these format parameters, we pass a sequence of arguments to the format method: u {0:.2f} means to format the first argument as a floating-point number with two decimal places. u {1:s} means to format the second argument as a string. u {2:d} means to format the third argument as an exact integer.
2.3 Python Language Basics Scalar Types • Strings: - String objects have a format method that can be used to substitute formatted arguments into the string, producing a new string: - To substitute arguments for these format parameters, we pass a sequence of arguments to the format method: u {0:.2f} means to format the first argument as a floating-point number with two decimal places. u {1:s} means to format the second argument as a string. u {2:d} means to format the third argument as an exact integer.
2.3 Python Language Basics Scalar Types • Strings: - Python 3.6 introduced a new feature called f-strings (short for formatted string literals) which can make creating formatted strings even more convenient. To create an f-string, write the character f immediately preceding a string literal. Within the string, enclose Python expressions in curly braces to substitute the value of the expression into the formatted string: String formatting is a deep topic; there are multiple methods and numerous options and tweaks available to control how values are formatted in the resulting string.
2.3 Python Language Basics Scalar Types • Bytes and Unicode: - In modern Python (i.e., Python 3.0 and up), Unicode has become the first-class string type to enable more consistent handling of ASCII and non-ASCII text. In older versions of Python, strings were all bytes without any explicit Unicode encoding. - We can convert this Unicode string to its UTF-8 bytes representation using the encode method. - Assuming you know the Unicode encoding of a bytes object, you can go back using the decode method. An example Unicode string with non-ASCII characters:
2.3 Python Language Basics Scalar Types • Bytes and Unicode: - While it is now preferable to use UTF-8 for any encoding, for historical reasons you may encounter data in any number of different encodings: - It is most common to encounter bytes objects in the context of working with files, where implicitly decoding all data to Unicode strings may not be desired.
2.3 Python Language Basics Scalar Types • Booleans: - The two Boolean values in Python are written as True and False. Comparisons and other conditional expressions evaluate to either True or False. Boolean values are combined with the and and or keywords: - When converted to numbers, False becomes 0 and True becomes 1:
2.3 Python Language Basics Scalar Types • Booleans: - The keyword not flips a Boolean value from True to False or vice versa:
2.3 Python Language Basics Scalar Types • Type casting: - The str, bool, int, and float types are also functions that can be used to cast values to those types: Most nonzero values when cast to bool become True.
2.3 Python Language Basics Scalar Types • None: - None is the Python null value type. - None is also a common default value for function arguments:
2.3 Python Language Basics Scalar Types • Dates and times: - The built-in Python datetime module provides datetime, date, and time types. - The datetime type combines the information stored in date and time and is the most commonly used. - You can extract the equivalent date and time objects by calling methods on the datetime of the same name:
2.3 Python Language Basics Scalar Types • Dates and times: - The strftime method formats a datetime as a string: - Strings can be converted (parsed) into datetime objects with the datetime.strptime function. datetime.strptime is one way to parse a date with a known format.
2.3 Python Language Basics Scalar Types • Dates and times: - When you are aggregating or otherwise grouping time series data, it will occasionally be useful to replace time fields of a series of datetimes. For example, replacing the minute and second fields with zero: Since datetime.datetime is an immutable type, methods like these always produce new objects. So in the previous example, dt is not modified by replace:
2.3 Python Language Basics Scalar Types • Dates and times: - The difference of two datetime objects produces a datetime.timedelta type: The output timedelta(17, 7179) indicates that the timedelta encodes an offset of 17 days and 7,179 seconds.
2.3 Python Language Basics Scalar Types • Dates and times: - Adding a timedelta to a datetime produces a new shifted datetime:
2.3 Python Language Basics Control Flow Python has several built-in keywords for conditional logic, loops, and other standard control flow concepts found in other programming languages. • if, elif, and else: - The if statement is one of the most well-known control flow statement types. It checks a condition that, if True, evaluates the code in the block that follows:
2.3 Python Language Basics Control Flow • if, elif, and else: - An if statement can be optionally followed by one or more elif blocks and a catchall else block if all of the conditions are False: If any of the conditions are True, no further elif or else blocks will be reached.
2.3 Python Language Basics Control Flow • if, elif, and else: - With a compound condition using and or or, conditions are evaluated left to right and will short-circuit: - It is also possible to chain comparisons: In this example, the comparison c > d never gets evaluated because the first comparison was True.
2.3 Python Language Basics Control Flow • for loops: - for loops are for iterating over a collection (like a list or tuple) or an iterater. The standard syntax for a for loop is: - You can advance a for loop to the next iteration, skipping the remainder of the block, using the continue keyword. This code sums up integers in a list and skips None values
2.3 Python Language Basics Control Flow • for loops: - A for loop can be exited altogether with the break keyword. This code sums elements of the list until a 5 is reached: The break keyword only terminates the innermost for loop; any outer for loops will continue to run.
2.3 Python Language Basics Control Flow • while loops: - A while loop specifies a condition and a block of code that is to be executed until the condition evaluates to False or the loop is explicitly ended with break.
2.3 Python Language Basics Control Flow • pass: - pass is the “no-op” (or "do nothing") statement in Python. It can be used in blocks where no action is to be taken (or as a placeholder for code not yet implemented); it is required only because Python uses whitespace to delimit blocks:
2.3 Python Language Basics Control Flow • range: - The range function generates a sequence of evenly spaced integers: - A start, end, and step (which may be negative) can be given:
2.3 Python Language Basics Control Flow • range: - The range function generates a sequence of evenly spaced integers:
2.3 Python Language Basics Control Flow • range: - The default iterator form is often used. This snippet sums all numbers from 0 to 99,999 that are multiples of 3 or 5:
2.4 Conclusion • The IPython and Jupyter programming environments. • Some basic Python language concepts. Next chapter: many built-in data types, functions, and input-output utilities that will be used continuously throughout the rest of the course.

Chapter 2 Python Language Basics, IPython, and Jupyter Notebooks.pdf

  • 1.
    Python for DataAnalysis CHINA UNIVERSITY OF PETROLEUM, BEIJING Zhu, Dandan College of Information Science and Engineering / College of Artificial Intelligence 2024 Spring
  • 2.
    Chapter 2 PythonLanguage Basics, IPython, and Jupyter Notebooks • 2.1 The Python Interpreter • 2.2 IPython Basics • 2.3 Python Language Basics • 2.4 Conclusion
  • 3.
    Chapter 2 PythonLanguage Basics, IPython, and Jupyter Notebooks • Much of this course focuses on table-based analytics and data preparation tools for working with datasets that are small enough to fit on your personal computer. • Sometimes you need to do some wrangling to arrange messy data into a more nicely tabular (or structured) form in the task of data analysis. Job seeker data Wrangling
  • 4.
    Chapter 2 PythonLanguage Basics, IPython, and Jupyter Notebooks • Some of the tools are best explored from a live IPython or Jupyter session. • Note: There are introductory Python concepts that this chapter does not cover, like classes and object-oriented programming, which you may find useful in your foray into data analysis in Python. Official Python tutorial: https:/ /docs.python.org/3/
  • 5.
    2.1 The PythonInterpreter • Python is an interpreted language. • The Python interpreter runs a program by executing one statement at a time. • The standard interactive Python interpreter can be invoked on the command line with the python command: • To exit the Python interpreter, you can either type exit() or press Ctrl-D (works on Linux and macOS only). The prompt after which you’ll type code expressions.
  • 6.
    2.1 The PythonInterpreter Running Python programs • Calling python with a .py file as its first argument. • IPython, an enhanced Python interpreter, or Jupyter notebooks. IPython %run command The prompt after which you’ll type code expressions.
  • 7.
    2.2 IPython Basics Runningthe IPython Shell • You can launch the IPython shell on the command line just like launching the regular Python interpreter except with the ipython command. • You can execute arbitrary Python statements by typing them and pressing Enter.
  • 8.
    2.2 IPython Basics Runningthe IPython Shell Python code statements creates a variable named data that refers to a newly created list prints the value of data in the console more readable, or pretty-printed Data variable in the standard Python interpreter
  • 9.
    2.2 IPython Basics Runningthe Jupyter Notebook • One of the major components of the Jupyter project is the notebook, a type of interactive document for code, text, data visualizations, and other output. • The Jupyter notebook interacts with kernels, which are implementations of the Jupyter interactive computing protocol specific to different programming languages. The Python Jupyter kernel uses the IPython system for its underlying behavior.
  • 10.
    2.2 IPython Basics Runningthe Jupyter Notebook • To start up Jupyter, run the command jupyter notebook in a terminal. On many platforms, Jupyter will automatically open in your default web browser (unless you start it with --no-browser). Otherwise, you can navigate to the HTTP address printed when you started the notebook.
  • 11.
    2.2 IPython Basics Runningthe Jupyter Notebook • To create a new notebook, click the buttons: File - New - notebook – double click the created notebook - select the "Python 3" option. • Entering a line of Python code, then press Shift-Enter to execute it.
  • 12.
    2.2 IPython Basics Runningthe Jupyter Notebook • When you save the notebook, it creates a file with the extension .ipynb. This is a self- contained file format that contains all of the content (including any evaluated code output) currently in the notebook. • When you want to close a notebook, select ”Shut Down Kernel." If you simply close the browser tab, the Python process associated with the notebook will keep running in the background.
  • 13.
    2.2 IPython Basics TabCompletion • One of the major improvements over the standard Python shell is tab completion, found in many IDEs or other interactive computing analysis environments. • While entering expressions in the shell, pressing the Tab key will search the namespace for any variables (objects, functions, etc.) matching the characters you have typed so far and show the results in a convenient drop-down menu. Also, you can complete methods and attributes on any object after typing a period (.).
  • 14.
    2.2 IPython Basics Introspection •Using a question mark (?) before or after a variable will display some general information about the object. This is referred to as object introspection. • If the object is a function or instance method, the docstring, if defined, will also be shown.
  • 15.
    2.2 IPython Basics Introspection •? has a final usage, which is for searching the IPython namespace in a manner similar to the standard Unix or Windows command line. A number of characters combined with the wildcard (*) will show all names matching the wildcard expression.
  • 16.
    2.3 Python LanguageBasics An overview of essential Python programming concepts and language mechanics. Language Semantics The Python language design is distinguished by its emphasis on readability, simplicity, and explicitness. • Indentation, not braces: - Python uses whitespace (tabs or spaces) to structure code instead of using braces as in many other languages like R, C++, Java, and Perl.
  • 17.
    2.3 Python LanguageBasics - A colon (:) denotes the start of an indented code block after which all of the code must be indented by the same amount until the end of the block. - IPython and Jupyter notebooks will automatically insert four spaces on new lines following a colon and replace tabs by four spaces. - Python statements do not need to be terminated by semicolons. Semicolons can be used, however, to separate multiple statements on a single line. Putting multiple statements on one line is generally discouraged in Python as it can make code less readable.
  • 18.
    2.3 Python LanguageBasics Language Semantics • Everything is an object: - An important characteristic of the Python language is the consistency of its object model. Every number, string, data structure, function, class, module, and so on exists in the Python interpreter in its own “box,” which is referred to as a Python object. Each object has an associated type (e.g., integer, string, or function) and internal data. In practice this makes the language very flexible, as even functions can be treated like any other object.
  • 19.
    2.3 Python LanguageBasics Language Semantics • Comments: - Any text preceded by the hash mark (pound sign) # is ignored by the Python interpreter. This is often used to add comments to code. At times you may also want to exclude certain blocks of code without deleting them. One solution is to comment out the code. - Comments can also occur after a line of executed code.
  • 20.
    2.3 Python LanguageBasics Language Semantics • Function and object method calls: - You call functions using parentheses () and passing zero or more arguments, optionally assigning the returned value to a variable. - Almost every object in Python has attached functions, known as methods, that have access to the object’s internal contents. You can call them using the following syntax: - Functions can take both positional and keyword arguments: (We will look at this in more detail later)
  • 21.
    2.3 Python LanguageBasics Language Semantics • Variables and argument passing: - When assigning a variable (or name) in Python, you are creating a reference to the object shown on the righthand side of the equals sign. In practical terms, consider a list of integers: - Suppose we assign a to a new variable b: - In some languages, the assignment of b will cause the data [1, 2, 3] to be copied. In Python, a and b actually now refer to the same object, the original list [1, 2, 3] Two references for the same object
  • 22.
    2.3 Python LanguageBasics Language Semantics • Variables and argument passing: - When you pass objects as arguments to a function, new local variables are created referencing the original objects without any copying.
  • 23.
    2.3 Python LanguageBasics Language Semantics • Dynamic references, strong types: - Variables in Python have no inherent type associated with them; a variable can refer to a different type of object simply by doing an assignment. There is no problem with the following:
  • 24.
    2.3 Python LanguageBasics Language Semantics • Dynamic references, strong types: - Variables are names for objects within a particular namespace; the type information is stored in the object itself. Some observers might hastily conclude that Python is not a “typed language.” This is not true; consider this example: In some languages, the string ‘5’ might get implicitly converted (or cast) to an integer, thus yielding 10. In other languages the integer 5 might be cast to a string, yielding the concatenated string ‘55‘. In Python, such implicit casts are not allowed. In this regard we say that Python is a strongly typed language, which means that every object has a specific type (or class).
  • 25.
    2.3 Python LanguageBasics Language Semantics • Dynamic references, strong types: - Implicit conversions will occur only in certain permitted circumstances, such as: Even though b is an integer, it is implicitly converted to a float for the division operation.
  • 26.
    2.3 Python LanguageBasics Language Semantics • Dynamic references, strong types: - Knowing the type of an object is important, and it’s useful to be able to write functions that can handle many different kinds of input. You can check that an object is an instance of a particular type using the isinstance function: isinstance can accept a tuple of types if you want to check that an object’s type is among those present in the tuple:
  • 27.
    2.3 Python LanguageBasics Language Semantics • Attributes and methods: Objects in Python typically have both attributes and methods. - Attributes: other Python objects stored “inside” the object. - Methods: functions associated with an object that can have access to the object’s internal data. Both of them can be accessed in two ways: ① Syntax <obj.attribute_name>: ② Name via the getattr function:
  • 28.
    2.3 Python LanguageBasics Language Semantics • Duck typing: Often you may not care about the type of an object but rather only whether it has certain methods or behavior. To verify that an object is iterable by using the iter function:
  • 29.
    2.3 Python LanguageBasics Language Semantics • Imports: - Module: simply a file with the .py extension containing Python code. - If we wanted to access the variables and functions defined in some_module.py, from another file in the same directory we could do: /
  • 30.
    2.3 Python LanguageBasics Language Semantics • Imports: - By using the as keyword, you can give imports different variable names:
  • 31.
    2.3 Python LanguageBasics Language Semantics • Binary operators and comparisons: - Most of the binary math operations and comparisons use familiar mathematical syntax used in other programming languages:
  • 32.
    2.3 Python LanguageBasics Language Semantics • Binary operators and comparisons: - To check if two variables refer to the same object, use the is keyword. Use is not to check that two objects are not the same: - Comparing with is is not the same as the == operator The list function always creates a new Python list (i.e., a copy) A common use of is and is not is to check if a variable is None, since there is only one instance of None:
  • 33.
    2.3 Python LanguageBasics Language Semantics • Mutable and immutable objects: - Mutable objects: Many objects in Python, such as lists, dictionaries, NumPy arrays, and most user-defined types (classes), are mutable. This means that the object or values that they contain can be modified. - Immutable objects: Others, like strings and tuples, are immutable, which means their internal data cannot be changed. Trying to avoid side effects and favor immutability, even though there may be mutable objects involved.
  • 34.
    2.3 Python LanguageBasics Scalar Types Python has a small set of built-in types for handling numerical data, strings, Boolean (True or False) values, and dates and time. These "single value" types are sometimes called scalar types. Standard Python scalar types
  • 35.
    2.3 Python LanguageBasics Scalar Types • Numeric types: - The primary Python types for numbers are int and float. Ø An int can store arbitrarily large numbers: Ø Floating-point numbers are represented with the Python float type. Under the hood, each one is a double-precision value. They can also be expressed with scientific notation: Ø Integer division not resulting in a whole number will always yield a floating-point number: Ø To get C-style integer division (which drops the fractional part if the result is not a whole number), use the floor division operator / /:
  • 36.
    2.3 Python LanguageBasics Scalar Types • Strings: - Python strings are immutable; you cannot modify a string. - If we need to modify a string, we have to use a function or method that creates a new string, such as the string replace method: Afer this operation, the variable a is unmodified:
  • 37.
    2.3 Python LanguageBasics Scalar Types • Strings: - Many Python objects can be converted to a string using the str function: - Strings are a sequence of Unicode characters and therefore can be treated like other sequences, such as lists and tuples: The syntax s[:3] is called slicing and is implemented for many kinds of Python sequences.
  • 38.
    2.3 Python LanguageBasics Scalar Types • Strings: - The backslash character is an escape character, meaning that it is used to specify special characters like newline n or Unicode characters. To write a string literal with backslashes, you need to escape them: - If you have a string with a lot of backslashes and no special characters, you might find this a bit annoying. Fortunately you can preface the leading quote of the string with r (stands for raw), which means that the characters should be interpreted as is:
  • 39.
    2.3 Python LanguageBasics Scalar Types • Strings: - Adding two strings together concatenates them and produces a new string:
  • 40.
    2.3 Python LanguageBasics Scalar Types • Strings: - String objects have a format method that can be used to substitute formatted arguments into the string, producing a new string: - To substitute arguments for these format parameters, we pass a sequence of arguments to the format method: u {0:.2f} means to format the first argument as a floating-point number with two decimal places. u {1:s} means to format the second argument as a string. u {2:d} means to format the third argument as an exact integer.
  • 41.
    2.3 Python LanguageBasics Scalar Types • Strings: - String objects have a format method that can be used to substitute formatted arguments into the string, producing a new string: - To substitute arguments for these format parameters, we pass a sequence of arguments to the format method: u {0:.2f} means to format the first argument as a floating-point number with two decimal places. u {1:s} means to format the second argument as a string. u {2:d} means to format the third argument as an exact integer.
  • 42.
    2.3 Python LanguageBasics Scalar Types • Strings: - Python 3.6 introduced a new feature called f-strings (short for formatted string literals) which can make creating formatted strings even more convenient. To create an f-string, write the character f immediately preceding a string literal. Within the string, enclose Python expressions in curly braces to substitute the value of the expression into the formatted string: String formatting is a deep topic; there are multiple methods and numerous options and tweaks available to control how values are formatted in the resulting string.
  • 43.
    2.3 Python LanguageBasics Scalar Types • Bytes and Unicode: - In modern Python (i.e., Python 3.0 and up), Unicode has become the first-class string type to enable more consistent handling of ASCII and non-ASCII text. In older versions of Python, strings were all bytes without any explicit Unicode encoding. - We can convert this Unicode string to its UTF-8 bytes representation using the encode method. - Assuming you know the Unicode encoding of a bytes object, you can go back using the decode method. An example Unicode string with non-ASCII characters:
  • 44.
    2.3 Python LanguageBasics Scalar Types • Bytes and Unicode: - While it is now preferable to use UTF-8 for any encoding, for historical reasons you may encounter data in any number of different encodings: - It is most common to encounter bytes objects in the context of working with files, where implicitly decoding all data to Unicode strings may not be desired.
  • 45.
    2.3 Python LanguageBasics Scalar Types • Booleans: - The two Boolean values in Python are written as True and False. Comparisons and other conditional expressions evaluate to either True or False. Boolean values are combined with the and and or keywords: - When converted to numbers, False becomes 0 and True becomes 1:
  • 46.
    2.3 Python LanguageBasics Scalar Types • Booleans: - The keyword not flips a Boolean value from True to False or vice versa:
  • 47.
    2.3 Python LanguageBasics Scalar Types • Type casting: - The str, bool, int, and float types are also functions that can be used to cast values to those types: Most nonzero values when cast to bool become True.
  • 48.
    2.3 Python LanguageBasics Scalar Types • None: - None is the Python null value type. - None is also a common default value for function arguments:
  • 49.
    2.3 Python LanguageBasics Scalar Types • Dates and times: - The built-in Python datetime module provides datetime, date, and time types. - The datetime type combines the information stored in date and time and is the most commonly used. - You can extract the equivalent date and time objects by calling methods on the datetime of the same name:
  • 50.
    2.3 Python LanguageBasics Scalar Types • Dates and times: - The strftime method formats a datetime as a string: - Strings can be converted (parsed) into datetime objects with the datetime.strptime function. datetime.strptime is one way to parse a date with a known format.
  • 51.
    2.3 Python LanguageBasics Scalar Types • Dates and times: - When you are aggregating or otherwise grouping time series data, it will occasionally be useful to replace time fields of a series of datetimes. For example, replacing the minute and second fields with zero: Since datetime.datetime is an immutable type, methods like these always produce new objects. So in the previous example, dt is not modified by replace:
  • 52.
    2.3 Python LanguageBasics Scalar Types • Dates and times: - The difference of two datetime objects produces a datetime.timedelta type: The output timedelta(17, 7179) indicates that the timedelta encodes an offset of 17 days and 7,179 seconds.
  • 53.
    2.3 Python LanguageBasics Scalar Types • Dates and times: - Adding a timedelta to a datetime produces a new shifted datetime:
  • 54.
    2.3 Python LanguageBasics Control Flow Python has several built-in keywords for conditional logic, loops, and other standard control flow concepts found in other programming languages. • if, elif, and else: - The if statement is one of the most well-known control flow statement types. It checks a condition that, if True, evaluates the code in the block that follows:
  • 55.
    2.3 Python LanguageBasics Control Flow • if, elif, and else: - An if statement can be optionally followed by one or more elif blocks and a catchall else block if all of the conditions are False: If any of the conditions are True, no further elif or else blocks will be reached.
  • 56.
    2.3 Python LanguageBasics Control Flow • if, elif, and else: - With a compound condition using and or or, conditions are evaluated left to right and will short-circuit: - It is also possible to chain comparisons: In this example, the comparison c > d never gets evaluated because the first comparison was True.
  • 57.
    2.3 Python LanguageBasics Control Flow • for loops: - for loops are for iterating over a collection (like a list or tuple) or an iterater. The standard syntax for a for loop is: - You can advance a for loop to the next iteration, skipping the remainder of the block, using the continue keyword. This code sums up integers in a list and skips None values
  • 58.
    2.3 Python LanguageBasics Control Flow • for loops: - A for loop can be exited altogether with the break keyword. This code sums elements of the list until a 5 is reached: The break keyword only terminates the innermost for loop; any outer for loops will continue to run.
  • 59.
    2.3 Python LanguageBasics Control Flow • while loops: - A while loop specifies a condition and a block of code that is to be executed until the condition evaluates to False or the loop is explicitly ended with break.
  • 60.
    2.3 Python LanguageBasics Control Flow • pass: - pass is the “no-op” (or "do nothing") statement in Python. It can be used in blocks where no action is to be taken (or as a placeholder for code not yet implemented); it is required only because Python uses whitespace to delimit blocks:
  • 61.
    2.3 Python LanguageBasics Control Flow • range: - The range function generates a sequence of evenly spaced integers: - A start, end, and step (which may be negative) can be given:
  • 62.
    2.3 Python LanguageBasics Control Flow • range: - The range function generates a sequence of evenly spaced integers:
  • 63.
    2.3 Python LanguageBasics Control Flow • range: - The default iterator form is often used. This snippet sums all numbers from 0 to 99,999 that are multiples of 3 or 5:
  • 64.
    2.4 Conclusion • TheIPython and Jupyter programming environments. • Some basic Python language concepts. Next chapter: many built-in data types, functions, and input-output utilities that will be used continuously throughout the rest of the course.