# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# All the IPython Notebooks in this lecture series based on those at https://github.com/rajathkumarmp/Python-Lectures
# Those are focused on python 2. the Updated version for Python 3 are found at https://github.com/JeffreyILipton/ME480

# # Variables

# A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows,

x = 2
y = 5
xy = 'Hey'

print(x+y, xy)

# Multiple variables can be assigned with the same value.

x = y = 1

print(x,y)

x=2
print(x,y)

# Variables are case sensitive!

x=1
X=2
print(x)
print(X)

# Variables can have numbers in them, but cannot start with numbers

x1=1
x2=2
print(x1)
print(x2)

2x=1
print(2x)

# # Number types

# Booleans ($\mathbb{B}$) are the simplest type of number in python representing True and False

T = bool(1)
F = bool(0)
print(type(T))
print(T)
print(True)
print(F)
print(False)

# Integers are represented by whole numbers $\mathbb{Z}$

small = -1*2**31-2
large = 2**32-1
print(type(small))
print(small)
print(large)

# Floats are the real numbers $\mathbb{R}$ and can be very large or very small. The only limit is the computer memory

pi = 3.141592
big = 1.79e308
small = 5e-324
print(type(pi))
print(pi)
print(big)
print(small)


# Complex numbers $\mathbb{C}$ can be represented by 2 floats x+yj

d = 1+5j
print(type(d))
print(d)


# # Operators

# ## Boolean Operators

# | Symbol | Task Performed |
# |----|---|
# | and  | And |
# | or  | or |
# | not | negation |
# | ^  | xor |
#
#
# ![image.png](attachment:image.png)

not False

not True

True and True

True and False

False or True

False ^ False

(True or False) and (False ^ True)

# ## Arithmetic Operators

# | Symbol | Task Performed |
# |----|---|
# | +  | Addition |
# | -  | Subtraction |
# | /  | division |
# | %  | mod |
# | *  | multiplication |
# | //  | floor division |
# | **  | to the power of |

1+2

2-1

1*2

1*2.0

(1+5j)+3

(1+5j)*(1-5j)

type((1+5j)*(1-5j))

2**3.0

# New in python 3, integer / integer return a float

1/2

1//2

# Floor division is nothing but converting the result so obtained to the nearest integer.

2.8//2.0

15%10

# ## Relational Operators

# | Symbol | Task Performed |
# |----|---|
# | == | True, if it is equal |
# | !=  | True, if not equal to |
# | < | less than |
# | > | greater than |
# | <=  | less than or equal to |
# | >=  | greater than or equal to |

z = 1

z == 1

z > 1

4.0 < 4

not (4.1 ==4.0)

# # Built-in Functions

# Python comes loaded with pre-built functions

# # Conversion from one system to another

int(4.9)

float(4)

complex(4)

float(complex(4))

abs(complex(4))

# **isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once.

print(isinstance(1, int))
print(isinstance(1.0,int))
print(isinstance(1.0,(int,float)))
print(isinstance(1.0+3j,(int,complex)))

# ## Simplifying Arithmetic Operations

# **round( )** function rounds the input value to a specified number of places or to the nearest integer. 

print (round(5.499)) 
print (round(4.55892, 2))

# **divmod(x,y)** outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). 

divmod(9,2)

# **pow(x,y,z)** can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z).

print (pow(3,3))
print (pow(3,3,5))

3**3

3**3%5

# ## Accepting User Inputs

string = "This is a string"
print(string)

# **complex( )** and other type functions can used to define a number from a string

c =complex('5+2j')
print(type(c))
print(c)

# **input( )** accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed.

abc = int(input("Type something here and it will be stored in variable abc \t"))

type(abc)

print(abc)

# **int( )** can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function **str( )** can be used to convert the integer back to string format

x = int("12")
y = str(x)
print(type(x))
print(x)
print(type(y))
print(y)

# ## Put it all Together to do something ##

# Lets make some code that calculates the area of a square from a side

side = float(input("Give a side length in cm"))
area = side**2
print(area)

StarcoderdataJupyterScriptsDedupFiltered