There are many python detail tutorials available all over the internet. The purpose of these tutorial is to quickly go thru the basic variables available and user inputs in Python. Always remember that python is case sensitive language, therefore variable names num1, Num1 & NUM1 will be treated different variables.
x=10
def variableTest(x):
x = x +1 #x is the local variable
print("Value inside function x= "+str(x))
>>> variableTest(x)
Output: Value inside function x= 11
>>> print("Value outside function x= "+str(x))
Output: Value outside function x= 10
x=10
def variableTest(num):
global x #x is declared Global variable
x = x +1
print("Value inside function x= "+str(x))
>>> variableTest(x)
Output: Value inside function x= 11
>>> print("Value outside function x= "+str(x))
Output: Value outside function x= 11
>>> num1=11
>>> type(num1)
Output: <type 'int'>
>>> print(num1)
Output: 11
>>> num2=13
>>> num1+num2
Output: 24
>>> num4=2.5
>>> type(num4)
Output: <type 'float'>
>>> num5=num1 + num4
>>> num5
Output: 13.5
>>> type(num5)
Output: <type 'float'>
>>> boolTest=False
>>> type(boolTest)
Output: <type 'bool'>
>>> boolTest2=True
>>> type(boolTest2)
Output: <type 'bool'>
>>> boolTest3=TRUE
Output:
Traceback (most recent call last):
File "", line 1, in
NameError: name 'TRUE' is not defined
>>> str1='DBMS'
>>> type(str1)
Output: <type 'str'>
>>> str2='TUTORIALS'
>>> print(str2)
Output: TUTORIALS
>>> str3 = str1+' '+str2
>>> print(str3)
Output: DBMS TUTORIALS
>>> print(str3[0]+str3[-1])
Output: DS
>>> print(str3[6:-1]) #Extracting part of the string
Output: UTORIAL
>>> print(str3[1:-1:2]) #Extracting pattern from the string
Output: BSTTRA
>>> print(str3[-1::-1]) #Reverse String
Output: SLAIROTUT SMBD
>>> print(str3[14])
Output: Traceback (most recent call last):
File "", line 1, in
IndexError: string index out of range
>>> str4='''This is multiline
string'''
>>> print(str4)
Output:
This is multiline
string
>>> input_var = raw_input()
Input: Dbmstutorials
>>> print(input_var)
Output: Dbmstutorials
>>> input_var1 = input()
Input: 3
>>> print(input_var1)
Output: 3
>>> input_var=input() #This will only fail in lower version and not in Python 3
Input: Dbmstutorials
Output: Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
NameError: name 'Dbmstutorials' is not defined
>>> input_var1 = input()
Input: 'Dbmstutorials'
>>> print(input_var1)
Output: Dbmstutorials
>>> input_var=input() #This will work fine in Python 3
Input: Dbmstutorials
>>> print(input_var)
Output: Dbmstutorials
>>> input_var1=input() #This will work fine in Python 3 but quote will be the first character in the string.
Input: 'Dbmstutorials'
>>> print(input_var)
Output: 'Dbmstutorials'
>>> print(input_var1[0])
Output: '(single Quote)
>>> alist = [1, 'a',{1,'a'}]
>>> type(alist)
Output: <type 'list'>
>>> alist=[1, 'a','b']
>>> alist.extend('c')
>>> alist
Output: [1, 'a', 'b', 'c']
>>> alist.pop()
Output: 'c'
>>> for i in alist:
... print(i)
Output:
1
a
b
>>> atuple = (2,3,1,'a')
>>> type(atuple)
Output: <type 'tuple'>
>>> for i in atuple:
... print(i)
Output:
2
3
1
a
>>> aset = {2,3,4,5,2,4,'a'}
>>> type(aset)
Output: <type 'set'>
>>> aset.add('b')
>>> aset
Output: set(['a', 2, 3, 4, 5, 'b'])
>>> aset.pop()
Output: 'a'
>>> print(aset)
Output: set([2, 3, 4, 5, 'b'])
set([2, 3, 4, 5, 'b'])
>>> for i in aset:
... print(i)
Output:
2
3
4
5
b
>>> adict = {"a":"Python",1:"Java"}
>>> type(adict)
Output: <type 'dict'>
>>> adict.update({'4':'Oracle'})
>>> adict
Output: {'a': 'Python', 1: 'Java', '4': 'Oracle'}
>>> adict.pop("a")
Output: 'Python'
>>> print(adict)
Output: {1: 'Java', '4': 'Oracle'}
>>> for key in (adict):
... print('key='+str(key)+' val='+str(adict[key]))
Output:
key=1 val=Java
key=4 val=Oracle
>>> str3
Output: 'DBMS TUTORIALS'
>>> del str3
>>> str3
Output: Traceback (most recent call last):
File "", line 1, in
NameError: name 'str3' is not defined