34
loading...
This website collects cookies to deliver better user experience
# Number Datatypes in Python
a = 2384735738437957379835798
b = 123
c = 1.0
d = 4 + 5j
# Let's check the type of above variables.
type(a)
# Output <class 'int'>
type(b)
# Output <class 'int'>
type(c)
# Output <class 'float'>
type(d)
# Output <class 'complex'>
# Strings
a = 'Hello'
b = "Python is awesome"
type(a)
# Output: <class 'str'>
type(b)
# Output: <class 'str'>
# Length of String
s = "Hello World"
print(len(s))
# Output: 11
s = "Python"
s1 = s[0]
s2 = s[4]
print('s1: ', s1)
print('s2: ', s2)
#Output
s1: 'P'
s2: 'O'
s[0] == s[-6]
# Output: True
s[-1] == s[5]
# Output: True
# String Slicing
s = 'String'
str1 = s[0:3]
str2 = s[2:4]
print('str1:', str1)
print('str2:', str2)
# Output
str1: 'Str'
str2: 'ri'
str3 = s[:4]
str4 = s[3:]
str5 = s[:]
print('str3:', str3)
print('str4:', str4)
print('str5:', str5)
# Output
str3: 'Stri'
str4: 'ing'
str5: 'String'
# String slicing with step index
s = 'String Slicing'
str1 = s[2:len(s): 2]
print(str1)
# Output: rn lcn
first_name = 'John'
second_name = 'Snow'
full_name = first_name + ' ' + second_name
print(full_name)
# Output
'John Snow'
a = 'Hello'
b = s * 3
print(b)
#Output
'HelloHelloHello'
a = 'I am learning Python'
b = 'Python'
c = 'hello'
print(b in a)
print(c in a)
# Output
True
False
name = 'Ramesh'
print('Hello {}'.format(name))
# Output
Hello Ramesh
name = 'Ramesh'
language = 'Python'
print('I am {} and I am learning {}.'.format(name, language)
# Output
# I am Ramesh and I am learning Python
# f-string syntax
name = 'Ramesh'
print(f'Hello {name}')
# output
# Hello Ramesh
fruit = 'mangoes'
quantity = 20
print(f'I love {fruit}, I can eat {quantity} {fruit} in a day.')
# Output
# I love mangoes and I can eat 20 mangoes in a day.
# Using double quotes
s1 = "I'm a software developer"
print(s1)
# Output: I'm a software developer
# Using escape sequence backslash(\)
print('We are using '\single quotes\' in the string'.)
# Output: We are using 'single quotes in the string.
# print newline character.
print('Hello world!\nWe are learning \'Python\'.')
# Output:
# Hello world!
# We are learning 'Python'.
'''
Here the output is not like - Hello World! We are learning 'Python'.
The reason is we used escape sequence newline(\n), so the interpreter knows that it's a newline character.
'''
# Boolean Data Type
type(True)
# Output: <class 'bool'>
type(False)
# Output: <class 'bool'>
# Boolean context of other values
bool(1)
# Output: True
bool(0.0)
# Output: False
bool('')
# Output: False