Introduction to Python
Write Your First Python Program.
# First python output with 'Print' functions
print('Hello World!')
print('Hi, Python!')
output: Hello World!
Hi, Python!# Python version check
import sys
print(sys.version) # version control
print(sys.winver) # [Windows only] version number of the Python DLL
print(sys.gettrace) # get the global debug tracing function
print(sys.argv) # keeps the parameters used while running the program we wrote in a list.
Basic Data Types In Python:-
# String
print("Hello, World!")
# Integer
print(12)
# Float
print(3.14)
# Boolean
print(True)
print(False)
print(bool(1)) # Output = True
print(bool(0)) # Output = False
output:
Hello, World!
12
3.14
True
False
True
False
Let's Discuss about Inbuilt- function:-
1. type()
sys.int_info(bits_per_digit=30, sizeof_digit=4)
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585
072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-
16, radix=2, rounds=1)
# String
print(type('Hello, World!'))
# Integer
print(type(15))
print(type(-24))
print(type(0))
print(type(1))
# Float
print(type(3.14))
print(type(0.5))
print(type(1.0))
print(type(-5.0))
# Boolean
print(type(True))
print(type(False))
output:
<class 'str'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'bool'>
<class 'bool'>
# Let's convert the integer number 6 to a string and a float
number = 6
print(str(number))
print(float(number))
print(type(number))
print(type(str(number)))
print(type(float(number)))
str(number)
output:
6
6.0
<class 'int'>
<class 'str'>
<class 'float'>
Let's conver the float number 3.14 to a string and an integer
number = 3.14
print(str(number))
print(int(number))
print(type(number))
print(type(str(number)))
print(type(int(number)))
str(number)
output:
3.14
3
<class 'float'>
<class 'str'>
<class 'int'>
# Let's convert the booleans to an integer, a float, and a string
bool_1 = True
bool_2 = False
print(int(bool_1))
print(int(bool_2))
print(float(bool_1))
print(float(bool_2))
print(str(bool_1))
print(str(bool_2))
print(bool(1))
print(bool(0))
output:
1
0
1.0
0.0
True
False
True
False
# Let's find the data types of 9/3 and 9//4
print(9/3)
print(9//4)
print(type(9/3))
print(type(9//4))
output:
3.0
2
<class 'float'>
<class 'int'>
Expression and variables:-
# Addition
x = 56+65+89+45+78.5+98.2
print(x)
print(type(x))
output:
431.7
<class 'float'>
# Substraction
x = 85-52-21-8
print(x)
print(type(x))
output:
4
<class 'int'>
# Multiplication
x = 8*74
print(x)
print(type(x))
output:
592
<class 'int'>
# Floor division
x = 125//24
print(x)
print(type(x))
output:
5
<class 'int'>
# Modulus
x = 125%24
print(x)
print(type(x))
output:
5
<class 'int'>
# Exponentiation
x = 2**3
print(x)
print(type(x))
output:
8
<class 'int'>
# An example: Let's calculate how many minutes there are in 20 hours?
one_hour = 60 # 60 minutes
hour = 20
minutes = one_hour *hour
print(minutes)
print(type(minutes))
# An example: Let's calculate how many hours there are in 348 minutes?
minutes = 348
one_hour = 60
hours = 348/60
print(hours)
print(type(hours))
output:
1200
<class 'int'>
5.8
<class 'float'>
# Mathematica expression
x = 45+3*89
y = (45+3)*89
print(x)
print(y)
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x**y)
print(x//y)
print(x%y)
output:
312
4272
4584
-3960
1332864
0.07303370786516854
1067641991672876496055543763730817849611894303069314938895568785412634039540022
1668842874389034129806306214264361154798836623794212717734310359113620187307704
8553130787246373784413835009801652141537511130496428252345316433301059252139523
9103385944143088194316106218470432254894248261498724877893090946822825581242099
3242205445735594289393570693328984019619118774730111283010744851323185842999276
1218679164101636444032930435771562516453083564435414559235582600151873226528287
4086778132273334129052616885240052566240386236622942378082773719975939989126678
9683171279214118065400092433700677527805247487272637725301042917923096127461019
9709972018821656789423406359174060212611294727986571959777654952011794250637017
9853580809082166014475884812255990200313907285732712182897968690212853238136253
3527097401887285523369419688233628863002122383440451166119429893245226499915609
9033727713855480854355371150599738557878712977577549271433343813379749929657561
1090329888355805852160926406122231645709135255126700296738346241869701327318850..
Complex Logical Question Asked in Interview Question:
x, y, z = 8, 4, 2 # the values of x, y, and z can be written in one line.
print(x, y, z)
print(x)
print(y)
print(z)
print(x/y)
print(x/z)
print(y/z)
print(x+y+z)
print(x*y*z)
print(x-y-z)
print(x/y/z)
print(x//y//z)
print(x%y%z)
output:
8 4 2
8
4
2
2.0
4.0
2.0
14
64
2
1.0
1
0
String Operations:
message = 'hello python!'
print('Before uppercase: ', message )
# convert uppercase the elements in a string
message_upper = message.upper()
print('After uppercase: ', message_upper)
# convert lowercase the elements in a string
message_lower = message.lower()
print('Again lowercase: ', message_lower)
# convert first letter of string to uppercase
message_title = message.title()
print('The first element of the string is uppercase: ', message_title)
output:
Before uppercase: hello python!
After uppercase: HELLO PYTHON!
Again lowercase: hello python!
The first element of the string is uppercase: Hello Python!
# replace() method in a string
message = 'Hello Python!'
message_hi = message.replace('Hello', 'Hi')
message_python = message.replace('Python', 'World')
print(message_hi)
print(message_python)
output:
Hi Python!
Hello World!
text = 'Jean-Paul Sartre somewhere observed that we each of us make our own hell out of the people around us. Had
# find the first index of the substring 'Nancy'
text.find('Nancy')
output:122
# format() method
"""
The format() method formats the specified value(s) and insert them inside the string's placeholder.
The placeholder is defined using curly brackets: {}.
"""
txt = "Hello {word}"
print(txt.format(word = 'World!'))
message1 = 'Hi, My name is {} and I am {} years old.'
print(message1.format('Bob', 36))
message2 = 'Hi, My name is {name} and I am {number} years old.'
print(message2.format(name ='Bob', number = 36))
message3 = 'Hi, My name is {0} and I am {1} years old.'
print(message3.format('Bob', 36))
output:
Hello World!
Hi, My name is Bob and I am 36 years old.
Hi, My name is Bob and I am 36 years old.
Hi, My name is Bob and I am 36 years old.
0 Comments