Comments start with a # in Python
Variables and Assignment Operators
Unlike other programming languages (ex PHP, with $), Python has no command for declaring a variable. A variable is created at the moment you first assign a value to it.
Variables don’t need to be declared with any particular type and You can change the type after they have been set.
month = 3 month = "march" print(month) #Result: march
Assign value to Multiple Variables
Python allows you to assign values to multiple variables in one line:
x, y, z = 1, 2, 3
Rules for Python variables:
- Must start with a letter or the underscore.
- Can use only letters, numbers, and underscores in your variable names. They can’t have spaces.
- Can’t use reserved words or built-in identifiers
- The pythonic way to name variables is to use all lowercase letters and underscores to separate words (ex my_age = 45).
!!Variable names are Case Sensitive in Python
In scientific notation, all numbers are written in the form: m × 10n
7.33e2 is equal to 7.33 * 10 ** 2 which is equal to 733
Integers and Floats
int – for integer values
float – for a decimal or floating-point values
Floating points numbers are approximations!
x = 3 = integer
y = 4.0 – float
y = 4. – float
print(3/4) – 0.75 – float
Even one integer divided by another integer exactly, the result will be float
print(4/4) / 1.0 – float
You can create a value that follows the data type by using the following syntax:
x = int(4.7) # x is now an integer 4
The part after the decimal is cut
y = float(4) # y is now a float of 4.0
You can check the type by using the type function:
print(type(x)) # int print(type(y)) # float
Python Arithmetic Operators
// Floor division
In Python, there are two kinds of division: integer division and float division.
print(4 / 3) print(4 // 3)
Gives the output in Python 3
Python Logical Operators
and Returns True if both statements are true
or Returns True if one of the statements is true
not Reverse the result, returns False if the result is true
Python uses and, not and or conditionals, instead of &&, ! or ^^.
PHP logical operators
$a and $b And TRUE if both $a and $b are TRUE. $a or $b Or TRUE if either $a or $b is TRUE. $a xor $b Xor TRUE if either $a or $b is TRUE, but not both. ! $a Not TRUE if $a is not TRUE. $a && $b And TRUE if both $a and $b are TRUE. $a || $b Or TRUE if either $a or $b is TRUE.
Python Identity Operators
In Python Identity operators are
is – evaluates if both sides have the same identity. Is operator is stronger than ==.
is not – evaluates if both sides have different identities
!Comparisons to singletons like None should always be done with ‘is’ or ‘is not’, never with the equality operators. Read more here – Style Guide for Python Code
someobj = None
if not someobj:
a = [1, 1, 2] b = a print(a == b) # True print(a is b) # True
List a and list b are equal and identical.
a = [1, 1, 2] b = [1, 1, 2] print(a == b) # True They have the same content! print(a is b) # False
They have the same content but point to two different objects, so they aren’t identical objects.
a = [1, 1, 2] b = [1, 1, 2, 4] print(a == b) # False They don't have the same content! print(a is b) # False They don't have the same content and point to different objects!
In PHP identical operator is ===
In PHP the Identical operator is the triple equal sign “===”
This operator returns true if both variable contains same information and same data types otherwise return false.
Why is === faster than == in PHP?
=== is faster because don’t converts the data type to see if two variables have same value
Python Bitwise Operators
& AND (Sets each bit to 1 if both bits are 1)
| OR (Sets each bit to 1 if one of two bits is 1)
^ XOR (Sets each bit to 1 if only one of two bits is 1)
~ NOT (Inverts all the bits)
<< Zero fill left shift (Shift left by pushing zeros in from the right and let the leftmost bits fall off)
>> Signed right shift (Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off)
Divide By Zero in Python
What happens if you divide by zero in Python?
ZeroDivisionError: division by zero
You can evaluate any expression in Python, and get one of two answers, True or False.
The bool() function allows you to evaluate any value, and give you True or False in return
print(bool("abc")) # True print(bool(10)) # True
The None keyword is used to define a null value or no value at all.
None is a variable, a value, that we can distinctly detect differently than numbers.
None is not the same as 0, False, or an empty string. None is a datatype of its own (NoneType) and only None can be None.
x = None print(x) //None
How do we get data from the user in Python?
You can read data from the user using the input() function.
The input() function returns a string!
name = input(“What is Your name?”)
Even if the user will tipe “123” the result will be string, not int!
Strings in Python are arrays of bytes representing Unicode characters.
In Python, You can define a string with either double quotes ” or single quotes ‘.
In Python, a string is immutable. You cannot overwrite the values of immutable objects.
Read more here about Working with strings in Python ✅ Methods, Slicing, String Mutations ✅ Exercises, and examples
Type & Type Conversion
int + float = float
print(.1 + .1 + .1 + .1 == .4)
Because the float, 0.1 is actually slightly more than 0.1.
When we add several of them together will see the difference between the mathematically correct answer and the one that Python creates.
print(0 + 7) # 7 print("0" + "7") # 07 print("0" + 7) # TypeError: can only concatenate str (not "int") to str print(0 + "7") TypeError: unsupported operand type(s) for +: 'int' and 'str'
The type() function returns the type of the specified object
print(type(10)) # <class 'int'> print(type("10")) # <class 'str'> print(type(10.)) # <class 'float'>
You can convert from one type to another with the int(), float(), str() and complex() methods:
#convert from float to int: a = int(2.0) print(type(a)) # <class 'int'>
#convert from string to int: a = str(10) print(type(a)) # <class 'str'>
Hello there!I hope you find this post useful!
I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.
If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!