Regular Expression!! ^ Matches the beginning of a line $ Matches the end of the line . Matches any character \s Matches whitespace \S Matches any non-whitespace character * Repeats a character zero or more times *? Repeats a character zero or more times (non-greedy) + Repeats a character one or more times +? Repeats a character one or more times (non-greedy) [aeiou] Matches a single character in the listed set [^XYZ] Matches a single character not in the listed set [a-z0-9] The set of characters can include a range ( Indicates where string extraction is to start ) Indicates where string extraction is to end
Posts
- Get link
- X
- Other Apps
Type Conversion In Python, it's impossible to complete certain operations due to the types involved. For instance, you can't add two strings containing the numbers 2 and 3 together to produce the integer 5, as the operation will be performed on strings, making the result '23'. The solution to this is type conversion . In that example, you would use the int function . Type Conversion !! Another example of type conversion is turning user input (which is a string ) to numbers ( integers or floats ), to allow for the performance of calculations. >>> float (input("Enter a number: ")) + float (input("Enter another number: ")) Enter a number: 40 Enter another number: 2 42.0 Passing non- integer or float values will cause an error. >>> "2" + "3" '23' >>> int ("2") + int ("3") 5 In Python, the types we have used so far have been integers , floats , and strings ....
- Get link
- X
- Other Apps
Strings If you want to use text in Python, you have to use a string . A string is created by entering text between two single or double quotation marks . When the Python console displays a string , it generally uses single quotes. The delimiter used for a string doesn't affect how it behaves in any way. >>> "Python is fun!" 'Python is fun!' >>> 'Always look on the bright side of life' 'Always look on the bright side of life' Newlines !! Python provides an easy way to avoid manually writing "\n" to escape newlines in a string . Create a string with three sets of quotes , and newlines that are created by pressing Enter are automatically escaped for you. >>> """ Customer: Good morning. Owner: Good morning, Sir. Welcome to the National Cheese Emporium. """ 'Customer: Good morning. \n Owner: Good morning, Sir. Welcome to the National Cheese Emporium.' There is a...
- Get link
- X
- Other Apps
Exponentiation !! Besides addition, subtraction, multiplication, and division, Python also supports exponentiation , which is the raising of one number to the power of another. This operation is performed using two asterisks. >>> 2**5 32 >>> 9 ** (1/2) 3.0 Quotient & Remainder !! To determine the quotient and remainder of a division, use the floor division and modulo operators, respectively. Floor division is done using two forward slashes. The modulo operator is carried out with a percent symbol (%). These operators can be used with both floats and integers. This code shows that 6 goes into 20 three times, and the remainder when 1.25 is divided by 0.5 is 0.25. >>> 20 // 6 3 >>> 1.25 % 0.5 0.25 In the example above, 20 % 6 will return 2, because 3*6+2 is equal to 20. You can chain exponentiations together. In other words, you can rise a number to multiple powers...
- Get link
- X
- Other Apps
Floats Floats are used in Python to represent numbers that aren't integers. Some examples of numbers that are represented as floats are 0.5 and -7.8237591. They can be created directly by entering a number with a decimal point, or by using operations such as division on integers. Extra zeros at the number's end are ignored. >>> 3/4 0.75 >>> 9.8765000 9.8765 Floats As you saw previously, dividing any two integers produces a float . A float is also produced by running an operation on two floats, or on a float and an integer . >>> 8 / 2 4.0 >>> 6 * 7.0 42.0 >>> 4 + 1.65 5.65 A float can be added to an integer , because Python silently converts the integer to a float . However, this implicit conversion is the exception rather the rule in Python - usually you have to convert values manually if you want to operate on them. Computers can't sto...
- Get link
- X
- Other Apps
Simple Operations Python has the capability of carrying out calculations. Enter a calculation directly into the Python console, and it will output the answer. >>> 2 + 2 4 >>> 5 + 4 - 3 6 Simple Operations The minus sign indicates a negative number. Operations are performed on negative numbers, just as they are on positive ones. >>> -7 -7 >>> (-7 + 2) * (-4) 20 Python also carries out multiplication and division, using an asterisk to indicate multiplication and a forward slash to indicate division. Use parentheses to determine which operations are performed first. >>> 2 * (3 + 4) 14 >>> 10 / 2 5.0 Using a single slash to divide numbers produces a decimal (or float , as it is called in programming). We'll have more about floats in a later lesson. The spaces around the plus and minus signs here are optional (the code would work without them), b...
- Get link
- X
- Other Apps
Printing Text !! The print statement can also be used to output multiple lines of text. For Example: >>> print('Hello world!') Hello world! >>> print('Hello world!') Hello world! >>> print('Spam and eggs...') Spam and eggs... Try It Yourself Python code often contains references to the comedy group Monty Python . This is why the words, "spam" and "eggs" are often used as placeholder variables in Python where "foo" and "bar" would be used in other programming languages.