There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different.
For example, one difference between Python 2 and 3 is the print statement. In Python 2, the "print" statement is not a function, and therefore it is invoked without parentheses. However, in Python 3, it is a function, and must be invoked with parentheses.
Python2 >> print "hello"
Python3 >> print ("hello")
Variables
Python does not have a concept of variable TYPE. User needs to take care when using them
x = 5
y = "hello"
x +y >> Error
str(x) +y >> user has to convert "x" to string
Interesting x*y will return hellohellohellohellohello (same string 5 times)
Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.
Multiple variable assignment on same line
a, b = 3, 4 #Comma separated
print (a,b) #Comma separated
Mixing Numbers and String is NOT supported
a = 3
b = hi
print(a + b) #NOT supported
print(str(a) + b) #3hi
Python supports two types of numbers - integers and floating point numbers. (It also supports complex numbers)
myintvar = 5
myflatvar = 5.0
toFloat = float(5)
toInt = int (5.0)
Strings are defined either with a single quote or a double quotes.
mystring = 'hello'
mystring = "hello"
The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)
mystring = "Don't worry about apostrophes"
OR use escape
mystring = 'Don\'t worry about apostrophes'
print(mystring)
Concatenating two strings
x = "hello"
y = "world"
z = x + " " + y
print(z) # This will print hello world
Builtin values are
True Or 1
False or 0
None (its neither 0 nor 1, its None)
Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish
mylist = []
mylist.append(1)
mylist.append(2)
mylist.append("hi") # You can mix numbers and strings in same array
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints hi
# prints out 1,2,hi
for x in mylist:
print(x)
Accessing an index which does not exist generates an exception (an error).
print(mylist[20]) # IndexError: list index out of range
Python uses indentation for blocks, instead of curly braces. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces. For example:
x = 1
if x == 1:
# indented four spaces
print("x is 1.")
Conditions
var = <value> #Initialization
if <Condition> : # ended by colon
#Statement starts after Indentation
x = 5
if x == 5:
print ("X is 5")
elif x == 6:
print ("X is 6") # No line break is needed
print ("X is 6")
else:
print (" Bye")
Nested conditions
x = 5
y = 6
if x == 5:
if y ==6:
print ("X is 5 and y is 6")
OR use "and" operator
if x == 5 and y == 6:
print ("X is 5 & y is 6")
1) Python supports concatenating strings using the addition operator:
helloworld = "hello" + " " + "world"
2) Python also supports multiplying strings to form a string with a repeating sequence:
lotsofhellos = "hello" * 10
>> This will print hello ten times
3) Lists can be joined with the addition operators:
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
4) Just as in strings, Python supports forming new lists with a repeating sequence using the multiplication operator:
print([1,2,3] * 3)
https://coderbyte.com/course/learn-python-in-one-week
https://www.learnpython.org/en/Hello%2C_World%21