Posts

Python Booleans

Booleans represent one of two values: True or False. Boolean Values In programming you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of two answers, True or False. When you compare two values, the expression is evaluated and Python returns the Boolean answer: Example print(10 > 9) print(10 == 9) print(10 < 9) When you run a condition in an if statement, Python returns True or False: Example Print a message based on whether the condition is True or False: a = 200 b = 33 if b > a:   print("b is greater than a") else:   print("b is not greater than a") Evaluate Values and Variables The bool() function allows you to evaluate any value, and give you True or False in return, Example Evaluate a string and a number: print(bool("Hello")) print(bool(15)) Example Evaluate two variables: x = "Hello" y = 15 print(bool(x)) print(bool(y)) Most Values are True Almost any value is evaluated t

Python Strings

String Literals String literals in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". You can display a string literal with the print() function: Example print("Hello") print('Hello') Assign String to a Variable Assigning a string to a variable is done with the variable name followed by an equal sign and the string: Example a = "Hello" print(a) Multiline Strings You can assign a multiline string to a variable by using three quotes: Example You can use three double quotes: a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.""" print(a) Or three single quotes: Example a = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''' print(a)  Note: in the result, the line breaks ar

Python Casting

Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions:     int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)     float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)     str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals Example Integers: x = int(1)   # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3 Example Floats: x = float(1)     # x will be 1.0 y = float(2.8)   # y will be 2.8 z = float("

Python Numbers

There are three numeric types in Python:     int     float     complex Variables of numeric types are created when you assign a value to them: Example x = 1    # int y = 2.8  # float z = 1j   # complex To verify the type of any object in Python, use the type() function: Example print(type(x)) print(type(y)) print(type(z)) Int Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Example Integers: x = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z)) Float Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Example Floats: x = 1.10 y = 1.0 z = -35.59 print(type(x)) print(type(y)) print(type(z)) Float can also be scientific numbers with an "e" to indicate the power of 10. Example Floats: x = 35e3 y = 12E4 z = -87.7e100 print(type(x)) print(type(y)) print(type(z)) Complex Complex numbers are written with a "j" as the imaginary part: Exa

Python Data Types

Built-in Data Types In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories: Text Type:     str Numeric Types:     int, float, complex Sequence Types:     list, tuple, range Mapping Type:     dict Set Types:     set, frozenset Boolean Type:     bool Binary Types:     bytes, bytearray, memoryview Getting the Data Type You can get the data type of any object by using the type() function. Example Print the data type of the variable x. x = 5 print(type(x)) Setting the Data Type In Python, the data type is set when you assign a value to a variable. Example                                                                   Data Type x = "Hello World"                                                         str x = 20                                                                                i nt x = 20.5            

Python Variables

Creating Variables Variables are containers for storing data values. Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Example x = 5 y = 'John' print(x) print(y) Variables do not need to be declared with any particular type and can even change after they have been set. Example x = 4 # x is of type int x = 'Sally' # x is not type str print(x) String variables can be declared wither by using single or double quotes. Example x = "John" # is the same as x = 'John' Variable Names A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z. 0-9, and _) Variable names are case sensitive (age, Age and AG

Comments in Python

Comments can be used to explain Python Code. Comments can be used to make the code more readable. Comments can be used to prevent execution while testing the code. Creating a comment Comments starts with a #, and Python will ignore them. Example #This is a comment print('Hello, World!') Comments can be placed at the end of the line, and Python will ignore the rest of the line. Example print('Hello, World!') #This is a comment Comments does not have to be text to explain the code, it can also be used to prevent Python from executing code. Example #print('Hello, World!') print('Cheers, Mate!') Multi Line Comments Python does not really have a syntax for multi line comments. To add a multi line comment you could insert a # for each line. Example #This is a comment #written in #more than just one line print('Hello, World!') Or, not quite as intended, you can use a multi line string. Since Python will ignore string literals that are not assigned to a