variables have the same naming convention where the variable is defined. Therefore arithmetic types are scalar types. Arrays (list, tuple and range) are aggregate types.
Quotes can be used inside a string only if they do not match the quotes surrounding the string.
Quotes Inside Strings
Sample Codestr1 = “It’s easy”
str2 = “Call me ‘Johnny'”
str3 = ‘Call me “Johnny”‘
print(str1, str2, str3)
Assign a multiline string to a variable using 3 quotes.
Multiline Strings
Sample Codestr1 = “””This is a double quoted
multiple line string
spanning 3 rows.”””
str1 = ”’This is a single quoted
multiple line string
spanning 3 rows.”’
print(str1, str2)
Return a part of the string using the slice syntax. To slice a string, specify the start index and end index separated by a colon. The first character has index 0.
Slicing
Sample Codestr1 = “Hello, World!”
print (str1[2:5]) # Prints from position 2 to position 5
print(str1[:5]) # Prints from position 0 to position 5
print(str1[2:]) # Prints from position 2 to the end
print(str1[-5:-2]) # Prints from position -5 to position -2
Table : Common Functions For Manipulating Strings
Function
Description
Example
upper()
Returns upper case
str2 = str1.upper()
lower()
Returns lower case
str2 = str1.lower()
strip()
Removes beginning or ending whitespace
str2 = str1.strip()
replace()
Replace a string with another
str2 = str1.replace(“Hello”, “Join”)
split()
Split string into substrings by specified separator
str2 = str1.split(“,”)
Strings can be combined using string concatenation via the plus “+” symbol
Booleans represent only 2 values, True or False. Any expression can be evaluated to get one of the two answers, True of False. Empty and zero values will be evaluated as False.
Boolean Values
Sample Codename = “John”
num1 = 5
num2 = 3
print(bool(name)) # Prints True
print(bool(num1)) # Prints True
print(num1 > num2) # Prints True
print(num1 == num2) # Prints False
print(num1 < num2) # Prints False
print(False) # Prints False
print(bool(None)) # Prints False
print(bool(0)) # Prints False
print(bool(“”)) # Prints False
print(bool(())) # Prints False
print(bool([])) # Prints False
print({}) # Prints False
A float of floating point number can be positive or negative, containing one or more decimals. Scientific numbers with the letter “e” can be used to indicate the power of 10.
A data type is a grouping of data values. Variables are created to store a data type. In Python, the assigning of a value will indicate the data type. PHP does not require a specific format in order to display the value.
Variables store information such as values or other variables.
Python variables do not need to be declared with any type and the type can be changed after been set.
Variables cannot start with a number because they must start with an underscore or letter. The second character can contain only alphanumeric characters or underscores.
Variable names are case-sensitive.
A variable name cannot be any of the Python keywords.
Variable Names
Sample Codename = ‘John’ #name is type str
age = 18 # age is type int
height = 5.3 # height is type float
myVarName = ‘Johnny’ # Camel Case is where each word, except the first, starts with a capital letter
MyVarName = ‘Johnny’ # Pascal Case is where each word starts with a capital letter
my_var_name = ‘Johnny’ # Snake Case is where each word is separated by an underscore
car = fruit = color = ‘red’ # Assign the same value to multiple variables on one line
Output Variables
Sample Code
print(name) # Outputs a single variable
print(name, age, height) # Outputs multiple variables, separated by a comma
print(name + myVarName + car) # Outputs multiple non numeric variables, separated by a plus symbol