|
| 1 | +# Hello World |
| 2 | + |
| 3 | +## Expressions |
| 4 | + |
| 5 | +```python |
| 6 | +# Use expressions to evaluate values in Python. |
| 7 | +# -------------------------------------------------------------------------------- |
| 8 | +# An expression is anything that evaluates to a value. The following is true |
| 9 | +# for all expressions: |
| 10 | +# |
| 11 | +# - An expression always returns a value. |
| 12 | +# - Expressions can be used in statements (e.g. if, while, for). |
| 13 | +# - Expressions can be part of other expressions. |
| 14 | + |
| 15 | +1 # Simple expression |
| 16 | +1 + 2 + 3 # Arithmetic expression |
| 17 | +``` |
| 18 | + |
| 19 | +## Hello World |
| 20 | + |
| 21 | +```python |
| 22 | +# Hello World |
| 23 | +# -------------------------------------------------------------------------------- |
| 24 | +# This simple program prints "Hello world!" and performs a basic |
| 25 | +# addition (1 + 1) to highlight Python's core syntax. |
| 26 | + |
| 27 | +print("Hello world!") |
| 28 | +print(1 + 1) |
| 29 | +``` |
| 30 | + |
| 31 | +## Modules |
| 32 | + |
| 33 | +```python |
| 34 | +# Use import to access python modules |
| 35 | +# -------------------------------------------------------------------------------- |
| 36 | +# A module is a file containing Python code. It can define functions, |
| 37 | +# classes, and variables that you can use in your code. You can import a |
| 38 | +# module using the `import` statement. |
| 39 | + |
| 40 | +import math |
| 41 | +from math import pi |
| 42 | + |
| 43 | +print(math.pi) |
| 44 | +print(pi) |
| 45 | +``` |
| 46 | + |
| 47 | +## Print Statement |
| 48 | + |
| 49 | +```python |
| 50 | +# Use the print() function to output text to the console. |
| 51 | +# -------------------------------------------------------------------------------- |
| 52 | +# The print() function is used to output data to the screen. It can take |
| 53 | +# multiple arguments and will convert them to strings before printing them. |
| 54 | + |
| 55 | +# By default, print() ends with a newline character. |
| 56 | +print("Hello") |
| 57 | +print("World") |
| 58 | +# Output: |
| 59 | +# Hello |
| 60 | +# World |
| 61 | + |
| 62 | +# You can change this behavior by specifying the end parameter. |
| 63 | +print("Hello", end="") |
| 64 | +print("World", end="") |
| 65 | +# Output: |
| 66 | +# HelloWorld |
| 67 | + |
| 68 | +# A print statement with no arguments prints a newline character. |
| 69 | +print("Hello", end="") |
| 70 | +print() |
| 71 | +print("World", end="") |
| 72 | +# Output: |
| 73 | +# Hello |
| 74 | +# World |
| 75 | + |
| 76 | +# You can also specify the separator symbol |
| 77 | +print("Hello", "World", sep="") |
| 78 | +print("Hello", "World", sep=" ") |
| 79 | +print("Hello", "World", sep=", ") |
| 80 | +# Output: |
| 81 | +# HelloWorld |
| 82 | +# Hello World |
| 83 | +# Hello, World |
| 84 | +``` |
| 85 | + |
| 86 | +## Statements |
| 87 | + |
| 88 | +```python |
| 89 | +# Use statements to perform actions in Python. |
| 90 | +# -------------------------------------------------------------------------------- |
| 91 | +# A statement is a piece of code that performs an action. It can be as simple |
| 92 | +# as a single line or a more complex block of code. The following is true |
| 93 | +# for all statements: |
| 94 | +# |
| 95 | +# - A statement does not return a value. |
| 96 | +# - Statements cannot be used in expressions (e.g. if, while, for). |
| 97 | +# - It is a standalone operation. |
| 98 | + |
| 99 | +# Valid statements |
| 100 | +x = 5 |
| 101 | +print(x) |
| 102 | + |
| 103 | +# Invalid statements |
| 104 | +# if (x = 5): # SyntaxError (statement cannot be used in an expression) |
| 105 | +``` |
| 106 | + |
| 107 | +## Zen Of Python |
| 108 | + |
| 109 | +```python |
| 110 | +# Import the `this` module to print the Zen of Python |
| 111 | +# -------------------------------------------------------------------------------- |
| 112 | +# This is a fun Easter egg in Python that prints the Zen of Python, the |
| 113 | +# guiding principles for writing computer programs in Python. It emphasizes |
| 114 | +# simplicity, readability, and the importance of explicitness in code design. |
| 115 | + |
| 116 | +import this |
| 117 | + |
| 118 | +# Output: |
| 119 | +# The Zen of Python, by Tim Peters |
| 120 | +# |
| 121 | +# Beautiful is better than ugly. |
| 122 | +# Explicit is better than implicit. |
| 123 | +# Simple is better than complex. |
| 124 | +# Complex is better than complicated. |
| 125 | +# Flat is better than nested. |
| 126 | +# Sparse is better than dense. |
| 127 | +# Readability counts. |
| 128 | +# Special cases aren't special enough to break the rules. |
| 129 | +# Although practicality beats purity. |
| 130 | +# Errors should never pass silently. |
| 131 | +# Unless explicitly silenced. |
| 132 | +# In the face of ambiguity, refuse the temptation to guess. |
| 133 | +# There should be one-- and preferably only one --obvious way to do it. |
| 134 | +# Although that way may not be obvious at first unless you're Dutch. |
| 135 | +# Now is better than never. |
| 136 | +# Although never is often better than *right* now. |
| 137 | +# If the implementation is hard to explain, it's a bad idea. |
| 138 | +# If the implementation is easy to explain, it may be a good idea. |
| 139 | +# Namespaces are one honking great idea -- let's do more of those! |
| 140 | +``` |
0 commit comments