Python [Post 02] | Variables

In Python a variable is created when we assign value to it. It does not required any explicit declaration. Use the assignment operator = to assign the value to a variable.

Variable Names Rules

  • Must start with a letter or the underscore character. Cannot start with a number.
  • Can only contain alpha-numeric characters and underscores (A-z, 0-9, & _ ).
  • Variable names are case-sensitive.
  • Avoid using Python Built-keywords. Like str, type, list etc.
#Example:

name = "John" # String Variable Assignment
age = 50 # Integer Variable Assignment
height = 150.50 # Float Variable Assignment

Using Python’s built-in function type() you can check what type of object is assigned to a variable.

Here are the list of Python data type:

  • int
  • float
  • str
  • list
  • tuple
  • dict
  • set
  • bool
# Exapmple

type(name)

str

## Dynamic Typing

In Python you can reassign variables to different data types. Python is very flexible in assigning data types.

# Example, Here Integer Assignment

x=50
type(x)

int

x="Hello, World!" # Re-assigned X Now String Assignment
type(x)

str

x=12.20+4j # Re-assigned X Now String Assignment
type(x)

complex

x=(1,2,3,4,5) # Re-assigned X Now Tuple Assignment
type(x)

tuple

## Re-assigning Variables

Python allow you to reassign variables with a reference to the same object.

a=50
print(a)

50

a = a + a
print(a)

100

a += 50
print(a)

150

a -= 10
print(a)

140

a *= 2
print(a)

280

a /= 5
print(a)

56.0

# Multiple Assignment

Python allows you to assign a value to multiple variables simultaneously.

# Example 1

length = width = height = 50
print (length)
print (width)
print (height)

50
50
50

# Example 2

length, width, height = (10,50,60)
print (length)
print (width)
print (height)

10
50
60


Thanks!

Happy Learning! Your feedback would be appreciated!

shobhitsingh.in

GitHub Code

Advertisements

Python [Post 03] | Numbers

Folks,
In this blog we will explore different Python numeric types, arithmetic operations, mathematical fuctions, variable assignment & number conversion.

For Python Overview & Basic – Refer this Blog Post

Python has mainly three diffrent of numeric types.

  • int
  • float
  • complex

Integers (int)


Int/Integer, is basically a whole number (+ve or -ve), without decimals & of unlimited length.

Example of Integers: -5, 500, 20001543646546231654646465465454544545454554

 

# Integer Example 1
print(-5)
type(5)

-5
int

# Integer Example 2
print(500)
type(500)

500
int

# Integer Example 3
print(20001543646546231654646465465454544545454554)
type(20001543646546231654646465465454544545454554)

20001543646546231654646465465454544545454554
int

Float


Floating point numbers is a number, (+ve or -ve), represent real numbers and they have a decimal points in them, or use an exponential (e) to define the number.

Exampe of Floats: -5.5 , 500.5, 5E2

5E2 (5 times 10 to the power of 2) is also an example of a floating point number in Python.

# Float Example 1
print(-5.5)
type(-5.5)

-5.5
float

# Float Example 2
print(500.5)
type(500.5)

500.5
float

# Float Example 3
print(5E2)
type(5E2)

500.0
float

Complex Number


Numbers in the form of (a + bj), where a and b are floats and j represents the square root of -1. Real part of the number is a, and the imaginary part is b.

Exampe of Complex Number: 5+500j

# Complex Example 1
5+500j

(5+500j)

type(5+500j)

complex

Artithmetic Operations


# Additon of numbers
5+5

10

# Substraction of numbers
10-5

5

# Multiplication of numbers
10*5

50

# Division of nuumbers
10/5

2.0

# Floor Divison (// operator truncates the decimal without rounding)
10//4

2

# Exponent
5**2

25

# Modulus (% operator returns the remainder after division)
5%2

1

Mathematical Fuctions


Python also have some functions that perform mathematical calculations

# importing "math" for mathematical operations
import math
# Ceil Function
print (math.ceil(5.6))

6

# Floor Function
print (math.floor(5.6))

5

# Exponential Function
print (math.exp(5))

148.4131591025766

# Square Root Function
print (math.sqrt(16))

4.0

# Factorial Function
print (math.factorial(5))

120

# Absolute Function - Positive Number
print(abs(-5))

5

# Round Function - Number rounded to n digits from the decimal point
print(round(5.56))

# Round Function with 2nd argument for how many digits to round
print(round(5.56,1))

6
5.6

# Minimun Function - Finds smallest of its arguments
print (min(5,10,2,22))

2

# Mamimun Function - Finds largest of its arguments
print (min(5,10,2,22))

2

Variable Assignemnt


# Create an object/Variable called "length" and assign it the number 50
length = 50
print(length)

50

# Reassignment - Overrites value
length = 10
print(length)

10

length = 10
width = 5.6
height = 10.6

print(length * width * height)

593.6

Numerical Type Conversion


# Example 1:
height = 10.6
type(height)

float

# Let's convert this variable into int
height= int(height)
print(height)
type(height)

10
int

# Example 1:
length = 10
type(length)

int

# Let's convert this variable into foat
length= float(length)
print(length)
type(length)

10.0
float


Thanks!

Happy Learning! Your feedback would be appreciated!

shobhitsingh.in
Guthub Code

How to send email using Python.

How to send email using Python.

Folks,

In this blog we will explore how send email to single/multiple email id using python Script.


Python Script: See detailed comments in the Python Script.


# import the smtplib module.
import smtplib

# from user details
from_user_email_id =  [provide_your_email_id_here]
from_user_email_pwd = [provide_your_email_id_password]

# Email Subject
email_subject = [provide_your_email_subject]

# To user email ids
to_user_email_id = [provide_list_of_to_email_ids_comma_seperated]

# set up the SMTP server
s = smtplib.SMTP(host=[provide_smtp_host_address_here], port=[provide_smtp_port_here]])
s.ehlo()
s.starttls()
s.ehlo

# login the SMTP server using from email id and password.
s.login(from_user_email_id, from_user_email_pwd)

# Preparing email message header and body.
email_header = 'To:' + ", ".join(to_user_email_id)  + '\n' + 'From: ' + from_user_email_id + '\n' + 'Subject: ' +  email_subject + '  \n'
email_body = email_header + [provide_here_email_text]

# sending email
s.sendmail(from_user_email_id, to_user_email_id, email_body)

s.close()

 

After providing all the required information & save your fileĀ  in .py extension. Refer below example.

email

Execute Script:

execute

Result:

email4


Thanks!

Happy Learning! Your feedback would be appreciated!