Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Saturday, July 17, 2021

# 4. Conversions with Python

 CONVERSATIONS WITH PYTHON

NOTE: You can find answers to the below practice conversion questions in the blog Answers of the blog no. 4 Conversations with Python. While you can also find some other user links down here:

We have thinked about it a lot and have organized some of the basic math and science conversions while some of here will be sophisticated too!

We will have two posts for conversions. One will contain the questions and the other will have answers.

Easy Conversions:đŸ‘‡

  • Length conversions:

  1. Convert 59.5 inches to centimeters? 
  2. Convert 59.5 centimeters to inches? 
  3. Convert 5.9 miles to kilometers? 
  4. Convert 5.9 kilometers to miles? 
  5. Convert 45 meters to feet? 
  6. Convert 45 feet to meters? 
  7. Convert 1277 millimeters to inches? 
  8. Convert 127 inches to millimeters? 
  9. Convert 13 yards to meters? 
  10. Convert 23 meters to yards? 

  • Temperature Conversions:

  1. Convert 44 degrees Celsius to Fahrenheit? 
  2. Convert 250 Kelvin to Celsius? 
  3. Convert 130 fehrenheit to Celsius? 
  4. Convert 120 fehrenheit to Kelvin? 
  5. Convert 30 Celsius to Kelvin? 

  • Weight Conversions:

  1. Convert 260 pounds to kilograms? 
  2. Convert 121 kilograms to pounds? 
  3. Convert 45 ounces to kilograms and pounds? 
  4. Converts 217 pounds to grams? 
  5. Convert 27 ounces to carats? 
  6. Convert 7 kilograms to carats? 
  7. Convert 2701 carats to grams? 

  • Area Conversions:

  1. Convert 33 square meters to square feet? 
  2. Convert 112.5 square feet to square meters? 
  3. Convert 3 square kilometers to square feet?

  • Volume Conversions:

  1. Convert 73 Gallons to micro litres? 
  2. Convert 110.4 micro yards to UK pints? 
  3. Convert 1281 milli litres to milli yards? 

  • Speed Conversions:

  1. Convert 64 kilometers per hour (kph) to miles per hour (mph)? 
  2. Convert 13.4 meter per second (mps) to kilometers per hour (kph)? 
  3. Convert 98 inches per second to kilometer per hour? 

  • Time Conversions:

  1. Convert 4 years 11 months 27 days 19 hours to seconds? 

  • Pressure Conversions:

  1. Convert 12 bars to pascal? 
  2. Convert 23 pascal to bars? 
  3. Convert 33 bars to inches per square feet? 
  4. Convert 88 Pascale to Newton per square meter? 

Intermediate Conversions:đŸ‘‡

  • Angle Conversions:

  1. Convert 187 degrees to radians? 
  2. Convert 2 Radains to degrees, minutes and seconds? 

  • Temperature Conversions:

  1. Convert 33 Celsius to Rankine? 
  2. Convert 33 Celsius to delisle? 
  3. Convert 33 Celsius to Newton? 
  4. Convert 33 Celsius to RĂ©aumur? 
  5. Convert 33 Celsius to Rømer? 

  • Weight Conversions:

  1. Convert 140 pounds to stones? 
  2. Convert 200 kilograms to long underweights? 
  3. Convert 25 stones to long underweights? 
  4. Convert 11372.1 tones to metric tones? 
  5. Convert 27.4 ounces to Troy ounces? 
  6. Convert 97.4 kilograms to Troy pounds? 
  7. Convert 112 Troy pounds to Troy ounces? 
  8. Convert 34 ounces to penny weights? 

  • Area Conversions:

  1. Convert 34 hectares to acres? 
  2. Convert 12 Acres to square yards? 

  • Volume Conversions:

  1. Convert 75.3 Cubic meters to litres, cubic feet, kilolitre, microlitre and cubic yards?

  • Speed conversions:

  1. Convert 1223 millimeters per minute to kilometers per hour, miles per second, nanometer per hour, meters per minute, inches per minute and yards per hour? 

  • Pressure Conversions:

  1. Convert 12 Newton per meter square to inches per foot square? 
  2. Convert 87 inches per meter square to Newton per square foot? 

Difficult conversions:đŸ‘‡

  • Angle Conversions:

  1. Convert 274 degrees, 12 minutes and 36 seconds to centrad, gradians and milliradians? 

  • Pressuse Conversions:

  1. Convert 66 bars to Torr? 
  2. Convert 12 torr to Newton per square meter? 
  3. Convert 13 pascal to pounds per square foot? 

  • Temperature Conversions:

  1. Recently 54.2 degrees Celsius have been recorded in the state of californe of USA. We don't know how much it is in other scale and we have to use it in different scientific experiments. Convert the above recorded temperature to Kelvin, fehrenheit, Rankine, delisle, Newton, RĂ©aumur and Rømer scales. 

  • Weight Conversions:

  1. Convert 12 kilograms to hundred weights? 
  2. Convert 66 tones to short tons and long tons? 
  3. Convert 22 pounds to milligrams and micrograms? 

  • Area Conversions:

  1. Convert 1745.3 square millimeters to square microns? 
  2. Convert 334.2 square inches to square microns and square Deci meters? 

  • Volume Conversions:

  1. Convert 45.7 liters to UK gallons, US pints, UK quarts, UK tablespoons, US teaspoons, Gills ND Ounces? 

  • Speed Conversions:

  1. Convert 350.7 knots to Mach, lightspeed, micron per second, feet per day and miles per year? 

Monday, June 14, 2021

# 3. Python Variables

 

VARIABLES IN PYTHON




Variables are containers used to store a value or variable is used to hold a value.

        Example: name = ‘ Satish ‘

Rules for variable declaration:

1.    A variable name must start with a letter or the underscore ‘ _ ‘ character.

Example: Age, _age

2.    A variable name cannot start with a number

Example: 2age is wrong

3.    A variable name can only contain alphanumeric character and underscore

Example: A – Z, a – z, 0 – 9, _.

4.    In python variable names are case sensitive

Example: age, Age, AGE all are different variables.

5.    Variable names do not contain any spaces

6.    A variable can have a short name or more descriptive name.

7.    Keywords are not used as variable names.

Example : a, ageofperson

Types of variable declaration:

v Camel case : Second and subsequent words first letters are capital

Example : ageOfPersonInCompany

v Pascal case: Same as camel case but first word first letter also capital

      Example : AgeOfPersonInCompany

v Snake case: Words are separated by underscore and all are small letters

            Example : age_of_person_in_company

>>> n = 100

Print(n)

Output: 100

>>> n

Output : 100

Chained assignment:

Assigning the same value to several variables simultaneously.

>>> a = b = c = 10

Print(a, b, c)

Output: 10  10  10

>>> number = 20.5

Print( number )

Output : 20.5

>>> str = “ I am learning python variables ”

Print( str )

Output : I am learning python variables

>>> print (300)

Output : 300   

‘ ‘ ‘ here we have not declared any variable name for 300, but internally the   interpreter creates an integer object and assign 300 to that object and display it to the console ‘ ‘ ‘

>>> a, b, c = 10, 20, 30     #simple the format in a single line

Print ( a, b, c )

Output : 10  20  30

Swapping variables:

>>>A = 10

B = 20

Temp1 = B

Temp2 = A

Print (Temp1, Temp2)

Output: 20  10     

‘’’ In the above program we declared two more variables which is not more memory efficient so to overcome this problem we use only one temporary variable’’’

>>> a =10

b = 20

temp = a

a = b

b = temp

print ( a, b)

output: 20 10      

‘’’ But what if we don’t want to use a temporary variable to make our program more memory efficient, but we want to swap with the same variables. Here is a simple solution to this problem’’’

>>> a = 10

 b = 20

a , b = b , c

print( a , b )

Output :  20  10

Concatenation:

>>> x =25

y = ‘python’

print(x+y)

Type Error: unsupported operand type(s) for +: ‘int’ and ‘str’

In python, we cannot concatenate different data types otherwise, it will give an error.

>>>abc     #  whenever we are trying to use undefined the variable we will get an error

Name Error: name ‘abc’ is not defined

>>> x=10

>>>x

10

>>> _ + x    # if we want to use the output of the previous operation we use underscore ‘_’

20

>>> name = ‘ Satish ‘

>>> name

‘Satish’

>>> name + ‘ Student’

Satish student

>>> name[3]     # indexing

‘i’

>>>name[3:5]    # end before index number 5 means indexno-1 means 5-1=4. slicing

                            #  [start : end]

‘is’

>>> name [3: ]

‘ish’

>>> name [ : 5]

‘Satis’

>>> name [8]

Index Error: string index out of range

>>> name [-2]

‘s’

>>> len(name)

6

Memory Address:

To find out the address of a variable we will use id(identity) keyword

>>> id (name)

5201293

Reference count:

We can check the reference counts of every object by using the getrefcount function of a  sys module. This function takes the object as an input and returns the number of references.

We can pass variable, name, value, function, class as an input to getrefcount() and in return, it will give a reference count for a given object.

a = 10

b = 10

print(id(a))

print(id(b))

c = 20

d = 20

e = 20

print(id(c))

print(id(d))

print(id(e))

Output:

140722211837248

140722211837248

140722211837568

140722211837568

140722211837568

Example: 

import sys

print(sys.getrefcount(a))

print(sys.getrefcount(c))

Output:

2

3

To know the data type of a variable, we will use type keyword

>>> type(name)

< class ‘string’ >

Local and Global variables:

Local variables: Local variables are defined and declared inside a function, we cannot call this variable outside the function.

>>> def fun():

              Name = ‘ python ‘

              Print(Name)

      fun()

Global variables: Global variables are defined and declared outside the function and we can use that variable in any function.

>>> def fun():

             Print(Name)

# Global scope

Name = ‘python’

           fun()

Constant:

In python, we cannot fix the constant values but we can set an intention by indicating it using ‘ capital letters ‘.

That means whenever we set a name as capital in python it is constant

Example : PI = 3.14.


  Practice programs

1. Make a program that solves and shows the summation of 64 + 32.

2. Do the same as in 2, but make it sum x + y.

3. How many glasses of water you drink every day?

         Hint: take a variable and assign a number of glasses of water and print
4. Create a variable named car name and assign the value of your car name to it.
5.There are two friends in a school started talking to each other about their hobbies? Write a program to print their conversation and take two variables to store their names. While printing display their names in each conversation to differentiate their roles.

 

Project1:

Imagine you’re working in a transportation company, and your boss asked you to prepare a status report. This should include the total number of buses, space in each bus, number of drivers, the number of passengers, buses that are not used, buses used, the total buses capacity, and the average passengers per bus.

buses = 100
space_in_a_bus = 20.0
drivers = 30
passengers = 130
buses_not_driven = to be calculated
buses_driven = to be calculated
total_capacity = to be calculated
average_passengers_per_bus = to be calculated

1.     Declare and initiate all the variables for this project # Use the given variable names and values to do so, if the variable has no value, then you need to do some math to calculate it's vale, E.G. average_passengers_per_bus = passengers / buses_driven

2.     Print the obtained values in useful sentences, E.G. print "There are", buses, "buses available."

Project 2:

The purpose of this project is to let Python talks about your friend, using his/her information, such as age, height, weight, hair color, eyes color and create a small descriptive paragraph about your friend.

Your_friend_name = 'name here'
Your_friend_age = age here
Your_friend_height = height here
Your_friend_weight = weight here
Your_friend_eyes = 'eyes color here'
Your_friend_teeth = 'teeth color here'
Your_friend_hair = 'hair color here'

After inserting your friend’s information into the code below, you need to let Python build a small descriptive paragraph about your friend. The output should be:

Let's talk about (your friend's name'
He's 'your friend height' inches tall.
He's 'your friend weight' pounds heavy.
Actually, that's not too heavy.
He's got 'your friend eyes color' eyes and 'your friend hair color' hair.
His teeth are usually 'your friend's teeth color' depending on the coffee.

If I add 'age', height, and 'weight' I get 'addition result'. 

 

# 7. Type conversion in Python

                                      Type Conversion in Python Python defines type conversion functions to directly convert one data type t...