python logo

Type Casting

Type casting is the process of converting one data type to another data type, like a string to an integer or float and vice versa. Example: num_string1 = "30" num_string2 = "20" num_strings = num_string1 + num_string2 print(num_strings) print(type(num_strings)) nums_typecast = int(num_string1) + int(num_string2) print(nums_typecast) print(type(nums_typecast)) try_typecast = int(num_string1 + num_string2) print(try_typecast) print(type(try_typecast)) In the code above, the data type of num_string1 and num_string2 are strings so when they are added using + operator, the result will just be a string concatenation and the data type is still a string....

February 12, 2022 · 1 min · Kei
python logo

Numbers

Type of Numbers : Integers : Whole numbers Floats (Floating Point numbers) : Decimal numbers Complex numbers : Numbers that have real and imaginary parts Some maths operations that can be done using Python are: Addition + : Adding numbers Subtraction - : Subtracting numbers Division / : Dividing numbers Multiplication * : Multiply numbers Exponential ** : x to the power of y Modulo % : Leftover number from division Floor division // : Returns the value of division without decimal points Example of Floor Division // There’s a math module that can be imported to perform mathematical operations on numbers...

February 12, 2022 · 1 min · Kei
python logo

Special Characters

Special characters in strings are: \ (Backslash) : an escape character \n : specifies new line within the string \t : adds a tab print("My name is Marshmallow. \ I wag my tail when I'm happy. \ I bark at speedy motorcycles \ and unwanted visitors.") print("\n") print("\\n") print("My name is Marshmallow.\nI wag my tail when I'm happy.\nI bark at speedy motorcycles\nand unwanted visitors....

February 11, 2022 · 1 min · Kei
python logo

Print Formatting

There are several ways to output the result using print() apples_in_basket = "111" print("There are " + apples_in_basket + " apples in the basket") print("There are",apples_in_basket,"apples in the basket") print("There are {}apples in the basket".format(apples_in_basket)) print(f"There are {apples_in_basket}apples in the basket")

February 11, 2022 · 1 min · Kei
python logo

Importing Module

To use a Python module I can import it by typing import followed by the name of the module. This will import the module with all its functions and methods import string print(string.ascii_lowercase) I can just import a particular method or function in a module instead of the whole module from string import ascii_uppercase print(ascii_uppercase)

February 11, 2022 · 1 min · Kei
python logo

String Methods and Functions

Methods and Functions to use on Strings The Python Standard Library is the standard library that is distributed with Python. This library reference manual is available at Python Standard Library Some of the functions and methods I can use on string objects are: len(), type(), id(), capitalize(), upper(), lower(), strip(), find(), split(), join() Functions do not tied to the object. I have to type the function name followed by parentheses and pass in the object as an argument....

February 11, 2022 · 2 min · Kei
python logo

Strings Concatenation, Indexing, Slicing

Concatenation Strings can be added to each other name = "Kei" message = "Good morning," print(message + name) Strings are immutable, it can’t be changed although it can be reassigned with the same variable name but they are stored in different memory location name = "Kei" message = "Good morning," print(id(message)) message = message + " " + name print(id(message)) Indexing Strings are sequence of characters therefore they can be indexed....

February 11, 2022 · 1 min · Kei
python logo

Variables

Variables are memory location references which store values morning_message = "Hello, Good Morning!" evening_message = "Hello, Good Evening!" print(id(morning_message)) print(morning_message) print(id(evening_message)) print(evening_message) Variable Name Rules : A variable name must start with a letter or an underscore Can only contain alphanumeric characters (A-Z, a-z, 0-9) and underscores (_) Case sensitive

February 11, 2022 · 1 min · Kei
python logo

Strings

Strings in Python can be represented by wrapping them by using ’ ’ (single quotes), " “(double quotes), or ’’’ ’’’ (triple quotes) 'This is Hello World using single quotes.' "This is Hello World using double quotes." """This is Hello World using triple quotes This is also called multi-line strings.""" To display the string as an output to the screen I can use Python bulit-in function, print() print('This is Hello World using single quotes....

February 11, 2022 · 1 min · Kei
Building a website

How to Code

Notes: Computers really only do two things well: store values and perform operations on those values. Everything computers do can be broken down into simple operations that are performed on simple values. To learn how to code, first I have to learn to think computationally. Computational thinking is a way of thinking about problem solving that grew out of computer science. To write code, first I have to break a problem down into a simple set of actions that solves the problem....

February 11, 2022 · 3 min · Kei