17
loading...
This website collects cookies to deliver better user experience
Print(‘hello World’)
Print () is an in-built python function that displays a message on the screen. In our example it will output ‘hello World’
hello World
and | assert | break | for | not | is |
lambda | else | from | finally | pass | for |
raise | None | False | del | if | import |
All the keywords except True, False and None are in lowercase and keywords are case sensitive.
An identifier is a name given to a variable, class, function, string, list, dictionary, module, and other objects.
#single line comment
'''
multiline comment
using a doctstring
'''
student ="John smith"
age = 20
amount = 20.05
paid = False
You don’t need to tell python that a variable should contain a specific type of data. Python automatically identifies the data type based on the values you assign to the variable.
single_string = 'this is a single string'
multiline_string ='''
like
this
example
'''
#for escaping characters
esc_string = "The teacher said:\ "You are a genuis.\""
print(esc_string) # The teacher said: "You are a genuis"
#concatinate stringg
greet ="hello"
name ="john"
print(greet+ ""+ name)# hello john
#interpolation
# method 1
name = "John"
age ="20"
message= f"{name} is {age} years."
#method 2
name = "John"
age ="20"
message = '{}is {}years.formart(name,age)'
#method 3
name = "John"
age ="20"
message = '{} is {}.format(first=name, last=age)'
#accessing characters in a string
name = "John Smith"
print(name[0])#J
print(name[6:9])#Smith
normal_integers = 46
floating_point_numbers = 50.04
complex_numbers = 3 +5i
# converting from one numerical Type to Another
float(56)
int(3.0004)
complex(45)
print(3+2) # 5 (addition)
print(10-5) # 5 (subtraction)
print (3*6) # 18 (multiplication)
print(46/2) # 23 (division)
print(3**3) # 9 (exponent)
print(11%4) # 3 (modulus)
print(60//4) # 15 (floor division)
d= True
b= False
# can create a an empty list with this syntax
my_list =[]
# lists with integers
num_list = [1,2,3,4,5]
# list with strings
string-list = ["John", "Mary", "Peter"]
# list with mixed data types
mixed_list = ["John", 24, True]
# methods on lists
children = ["John", "Mary", "Sophy", "Peter"]
# append
children.append("Jerry")
# insert
children.insert(2,"Tom")
# remove
children.removed("Sophy")
# get length
print(len(children)) # 5
new_tuple = ("a","b","c","d","e")
print(len(new_tuple)) # 5
print(new_tuple[3]) # d
ages= {
"John": 20,
"Mary":21,
"Tom":25,
"Jerry":19
}
# access, modify, delete
print(ages[Mary]) # 20
print(len(ages)) # 4