Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

The Print Statement

In [1]:

# Python code for "Hello World"


# nothing else to type...see how simple is the syntax.

print("Hello World")
Hello World
In [2]:

print("Printing", 'Multiple', "Things", 'like', 1, 2, "and", 3.14159265358979)


Printing Multiple Things like 1 2 and 3.14159265358979
In [3]:

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.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.


In [4]:
print("Comments don't show up") # This is a comment
Comments don't show up
In [5]:

"""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]:

print(name, "is", age, "years old")


Rahul is 20 years old

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]:

age += 1 # age = age + 1


print(age)
27
In [ ]:

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]:

name= str("Hello World")


#display the data type of x:
print(type(name))
<class 'str'>
In [19]:

#display the data type of name:


type(name)
Out[19]:
str
In [12]:

x = 20.5

#display x:
print(x)

#display the data type of x:


type(x)

20.5
Out[12]:
float
In [23]:

x = list(("apple", "banana", "cherry"))

#display x:
print(x)

#display the data type of x:


type(x)
['apple', 'banana', 'cherry']
Out[23]:
list
In [24]:

x = bool(5)

#display x:
print(x)

#display the data type of x:


print(type(x))

True
<class 'bool'>
In [25]:

x = bool(5)

#display x:
print(x)

#display the data type of x:


type(x)
True
Out[25]:
bool
In [26]:

x = tuple(("apple", "banana", "cherry"))

#display x:
print(x)

#display the data type of x:


type(x)

('apple', 'banana', 'cherry')


Out[26]:
tuple
In [27]:

x = dict(name="John", age=36)

#display x:
print(x)

#display the data type of x:


type(x)
{'name': 'John', 'age': 36}
Out[27]:
dict

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

#convert from int to float:


a = float(x)

#convert from float to int:


b = int(y)

#convert from int to complex:


c = complex(x)

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 [ ]:

You might also like