Python Lab 1
Python Lab 1
Python Lab 1
In [1]:
print("Hello World")
Hello World
In [2]:
print("Print statement") # python automatically adds a new line when using print()
print("by default, ends with a")
print("newline ", end="")
print("unless you do", end=" ")
print("this")
Print statement
by default, ends with a
newline unless you do this
Comments
Comments can be used to explain Python code.
"""This
is a multiline
Comment"""
print("No comments")
No comments
Python as a calculator
Python has mainly 7 operators:-
1.
: Addition
2.
: Subtraction
3.
: Multiplication
4. / : Division
5. % : Modulo
6. ** : Exponentiation
7. // : Integer Division
In [6]:
30 + 1 + 2022
Out[6]:
2053
In [7]:
print("3 + 2 =", 3+2)
print("3 - 2 =", 3-2)
print("3 * 2 =", 3*2)
print("3 / 2 =", 3/2)
print("3 % 2 =", 3%2)
print("3 ** 2 =", 3**2)
print("3 // 2 =", 3//2)
3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1.5
3 % 2 = 1
3 ** 2 = 9
3 // 2 = 1
Variables
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume). Rules for Python variables: A variable name must start
with a letter or the underscore character A variable name cannot start with a
number A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and
AGE are three different variables) A variable name cannot be any of the Python
keywords. For example:-
In [4]:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
print(myvar)
print(my_var)
print(_my_var)
print(myVar)
print(MYVAR)
print(myvar2)
John
John
John
John
John
John
In [3]:
age = 20
name = "Rahul"
print(age)
print(name)
20
Rahul
In [9]:
You can also change the value stored in a variable (its a variable afterall)
In [11]:
age = 25
print(age)
25
In [12]:
age = age + 1
print(age)
26
In [13]:
In [14]:
x = str(3)
y = int(3)
z = float(3)
print(x)
print(y)
print(z)
3
3
3.0
Assignment Operators
These are the operators used for assigning value to a variable
1. =
2. +=
3. -=
4. *=
5. /=
6. many more Note: Unlike many programming languages, python does not
have ++ operator
In [4]:
x = 10
x += 5
print("x = x + 1:", x)
x -= 5
print("x = x - 1:", x)
x *= 5
print("x = x * 2:", x)
x = x + 1: 15
x = x - 1: 10
x = x * 2: 50
Data Types
1. Numbers
2. Booleans
3. Strings
4. Lists
5. Tuples
6. Dictionaries
In [11]:
age = 25
#print(age)
type(age)# type(variable) returns the datatype
Out[11]:
int
In [7]:
x = 20.5
#display x:
print(x)
20.5
Out[12]:
float
In [23]:
#display x:
print(x)
x = bool(5)
#display x:
print(x)
True
<class 'bool'>
In [25]:
x = bool(5)
#display x:
print(x)
#display x:
print(x)
x = dict(name="John", age=36)
#display x:
print(x)
Numbers
There are three numeric types in Python:
int float complex Variables of numeric types are created when you assign a
value to them:
In [28]:
x = 1 # int
y = 2.8 # float
z = 1j # complex
In [29]:
print(type(x))
print(type(y))
print(type(z))
<class 'int'>
<class 'float'>
<class 'complex'>
Int
Int, or integer, is a whole number, positive or negative, without decimals, of
unlimited length.
In [30]:
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
<class 'int'>
<class 'int'>
<class 'int'>
Float
Float, or "floating point number" is a number, positive or negative, containing
one or more decimals.
In [31]:
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
<class 'float'>
<class 'float'>
<class 'float'>
In [32]:
x = 35e3 #Float can also be scientific numbers with an "e" to indicate the power of 10.
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
<class 'float'>
<class 'float'>
<class 'float'>
Complex
Complex numbers are written with a "j" as the imaginary part:
In [33]:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
<class 'complex'>
<class 'complex'>
<class 'complex'>
Type Conversion
You can convert from one type to another with the int(), float(), and complex()
methods:
In [34]:
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
In [ ]: