24
loading...
This website collects cookies to deliver better user experience
>>> 5 + 2 # Addition
7
>>> 4 - 3 # Subtraction
1
>>> 4 * 4 # Multiplication
16
>>> 10 / 2 # Division
5.0 # Return float value
>>> 10 // 2 # Division, but return integer value
5
>>> 10 % 2 # Modulas(Remainder)
0
>>> 2 ** 3 # Exponenet(Power)
8
Data Type | Example |
---|---|
Integer | -2,-1,0,1,2 |
Float | -1.5, -1.92, 2.25, 16.4 |
String |
'python' ,'is' ,'love'
|
Complex |
1j , 1+2j
|
Boolean |
x = True , x = False
|
name = input("Enter Your Name:")
print("Hello " + name)
myString = "Python is easy!"
myString = "Python"
myString2 = "is easy"
ConString = myString + myString2
print(ConString)
string = "Python" * 5
print(string)
# OUTPUT: PythonPythonPythonPythonPython
lst = ["python","cpp","java"]
>>> lst = ["python","cpp","java"]
>>> lst[0]
"python"
>>> lst[1]
"cpp"
>>> lst[2]
"java"
lst = [1,2,3]
lst.append(4)
print(lst)
# OUTPUT : [1, 2, 3, 4]
lst = [1,3,4]
# SYNTAX : .insert(index number, element)
lst.insert(1,2)
print(lst)
# OUTPUT : [1,2,3,4]
lst = [1,2,3,5]
lst.remove(5)
print(lst)
# OUTPUT : [1,2,3]
lst1 = [1,2,3]
lst2 = ["x","y","z"]
combineList = lst1 + lst2
print(combineList)
# OUTPUT: [1, 2, 3, "x", "y", "z"]
lst1 = [1,2,3]
lst2 = ["x","y","z"]
nestedList = [list1,list2]
print(nestedList)
# OUTPUT : [[1, 2, 3], ["x", "y", "z"]]
lst = [123,51,214,23,56]
lst.sort()
print(lst)
#OUTPUT : [23, 51, 56, 123, 214]
lst = ["a","b","c"]
lstcpy = lst.copy()
print(lstcpy)
# OUTPUT: ["a", "b", "c"]
myTuple = (1,2,3,4)
print(myTuple)
myTuple = (1,2,3,4)
print(myTuple[2])
# OUTPUT: 3
myTuple = (1,2,3)
convertedTuple = list(myTuple)
# Now we can edit the "convertedTuple" list.
convertedTuple[0] = 120
# Converting back to Tuple.
myTuple = tuple(ConvertedTuple)
print(myTuple)
# OUTPUT: (120,2,3)
mySet = {"python", "java", "cpp"}
print(mySet)
mySet = {"python",142,True,"abc"}
mySet = {1,2,3}
s.add(4)
print(s)
# OUTPUT: {1,2,3,4}
mySet = {1,2,3}
mySet.update([4,5,6,7])
print(mySet)
# OUTPUT : {1,2,3,4,5,6,7}
# SYNTAX
# dictionary_name = {
# "key_name" : "Value",
# "key2_name" : "Value2"
# }
dict = {
"name" : "Van Rossum",
"ID" : 1234,
"year" : 1956
}
print(dict["name"])
print(len(dict))
# OUTPUT : 2
dict = {
"name" : "Van Rossum",
"ID" : 1234,
"year" : 1956
}
# Adding Item
dict["profession"] = "Programmer"
# Syntax
# dict_name.update({"key":"value"})
dict.update({"ID":"9493"})
for i in dict:
print(dict[i])
# OUTPUT:
# Van Rossum
# 9493
# 1956
If
statement contains a logical expressions using which data is compared and a decision is made.if expression:
statements or code
Note : Indentation is required in IF statement
# Let's a create a program to check if person is eligible to vote or not
age = 15
if age < 18: # If age is less than 18 execute the block
print("Not Eligible to Vote")
if age >= 18: # If age is greater than or equal to 18 execute the block
print("Eligible to Vote")
# Here age is 15 so, 1st condition will be true and print the message.
Elif
keyword used if the previous if
condition is false then it will execute the elif
condition.# Let's take the same example but using elif condition
age = 20
if age < 18:
print("Not Eligible to Vote")
elif age >= 18:
print("Eligible to Vote")
#OUTPUT: Eligible to Vote
else
keyword is used if all previous condition becomes false then it execute the else
block.# Let us Understand using example of "If Number is +ve or -ve."
n = 0
if n > 0:
print("N is +ve")
elif n < 0:
print("N is -ve")
# In this case both the 'if' condition is false so we use 'else' block
else:
print("N is Zero")
# OUTPUT: N is Zero
# print hello World 100 times
i = 1
while i <= 100:
print("Hello World")
i=i+1
# Same example but using for loop
# for i in range(101)-> range function is used instead of "i<=100" from while loop
# Syntax for range() function -> range(n-1), So If n=100, range will only include 99.
for i in range(101):
print("Hello World")
def
keyword. SYNTAX
def function_name():
code inside function
def my_function():
print("Hello World")
def greeting():
print("Good Morning")
# Calling Of Function
greeting()
# let us understand using example
# -> Take username and greet
# passing name argument
def greet(name):
print("Hello " + name + ", Good Morning" )
greet("Rohan")
## Passing Two Arguments:
def greet(name, lastname):
print("Hello " + name + lastname + ", Good Morning" )
greet("Rohan", "Kiratsata")
SYNTAX
lambda arguments: expression
# Lambda function to square of number
n = lambda x : x*x
print(n(5))