Python Basics Q&A - 1

 Q1. In the below elements which of them are values or an expression? 
       eg:- values can be integers or strings and expressions will be mathematical operators. 
        'hello',
        -87.8,
        6,
        +, -, *, /

Ans. 'hello' - It is a string used in python, string means that it comes under inverted commas like
         "Hello World", "10", "2+1j", "True", "None", " "
          or anything which you want to wrap in between " "commas turn into a string.

#Python program to print a string
a = "Hello From Python" #string and integers are values as well like the value of a is "Hello From Python" and b is "10"
b = "10" # I am a number but used as a string
c = "2+1j" # I am a complex number but used as a string 
d = "True" # I am a keyword but now as a string
e = "" # blank string
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))

The result shows class<str>

Ans . '-87.8' - It is float in python any numbers which have point values are float, not integers. like 10.0, not 10

#To print float values
a = -87.8
print(type(a))
Result shows class <'float'>

Ans. '6' -  It is an integer in python

a = 6
print(type(a))
The result shows class <int>

Ans. '*' - It is a mathematical Operator used to multiply two values like a*b

a = 5
b = 10
print(a*b)
Result Shows 50

Ans.  '-', '/', '+'  - It is a mathematical operator in python used to subtract, Divide, and add in python.
          or even used in advanced options while concatenating strings, integers, and values in python.

#Python program to evalute numbers
a = 50
b = 30
c = 19.8

#Python program to evaluate string
d = "Past"
e = "present"
f = "future"

#To print results
print(a+b+c)
print(a-b-c)
print(a/b/c)
print(d+e+f)
Result Shows 
99.8
0.1999999999999993
0.08417508417508418
Pastpresentfuture

Q2. What is the difference between string and variable?

Ans. '''Variables are symbols that you can use to store data in a program. You can think of them as empty boxes that you fill with some data or value. Strings are data, so we can use them to fill up a variable. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Strings in python are surrounded by either single quotation marks, double quotation marks, or three single quotes

a = "Python"
# a is a variable and the value assigned to it is a string which is in that case "Python"

Q3. Describe three different data types.

Ans. In python programming, the data type is an important concept. Variables can store data of different types, and different types can do different things.
Python has the following data types built in by default, in these categories:
Text Type:-str
Numeric Types:-int, float, complex
Sequence Types:-list, tuple, range
Mapping Type:-dict
Set Types:-set, frozenset
Boolean Type:-bool
Binary Types:-bytes, bytearray, memoryview
None Type:-NoneType

The top 5 data types used in python are:
1. string type, for sequences of character data. This is a blank string""
2. integer type for numbers, math
3. bool type for justifying conditions,
4. dict type: Dictionary Data Structure is mutable and it improves the readability of your code.
   
(At the end of the day, a python dictionary represents a data structure that can prove valuable in cleaning data and making it actionable. It becomes even more valuable because it is inherently simple to use and much faster and more efficient as well.)

5. list & tuple: Lists are mutable, and tuples are immutable, which means you can append a list but not a tuple.
# Make a python programme to print the list of all the data types
l1 = ["Hello Python", 25, 45.8, 5+8j, ["I am list under list", "Apple", "Orange", "water", 52], ("I am tuple" , 10.2), range(10, 20, 1), {"id1":"keyword", "id2":"datatypes", "id3":1010}, ({"apple", "banana", "cherry"}), frozenset({"apple", "banana", "cherry"}), True, False, b"Hello", bytearray(5), memoryview(bytes(5)), None]
print("Here is my all data types list: ", l1)
print("Total length of list: ", len(l1))
print(type(l1))
print(type(l1[0]))
print(type(l1[1]))
print(type(l1[2]))
print(type(l1[3]))
print(type(l1[4]))
print(type(l1[5]))
print(type(l1[6]))
print(type(l1[7]))
print(type(l1[8]))
print(type(l1[9]))
print(type(l1[10]))
print(type(l1[11]))
print(type(l1[12]))
print(type(l1[13]))
print(type(l1[14]))
print(type(l1[15]))
Result Shows 
Here is my all data types list:  ['Hello Python', 25, 45.8, (5+8j), ['I am list under list', 'Apple', 'Orange', 'water', 52], ('I am tuple', 10.2), range(10, 20), {'id1': 'keyword', 'id2': 'datatypes', 'id3': 1010}, {'apple', 'cherry', 'banana'}, frozenset({'apple', 'cherry', 'banana'}), True, False, b'Hello', bytearray(b'\x00\x00\x00\x00\x00'), <memory at 0x000002A1C6FDCF40>, None]
The total length of the list:  16
<class 'list'>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'list'>
<class 'tuple'>
<class 'range'>
<class 'dict'>
<class 'set'>
<class 'frozenset'>
<class 'bool'>
<class 'bool'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>
<class 'NoneType'>

Q4. What is an expression made up of? What do all expressions do?

Ans. An Expression Is An Instruction That Combines Values And Operators And Always Evaluates Down To A Single Value.
For Example, This Is An Expression: 2 + 2
This Expression Evaluates Down To The Single Integer Value: 4 #this Is Also An Expression
We Have Many Different Types Of Expressions In Python:-
1. Constant Expressions: These Are The Expressions That Have Constant Values Only.
2. Arithmetic Expressions: An Arithmetic Expression Is A Combination Of Numeric Values, Operators, And Sometimes Parenthesis.
   The operators are Addition, Subtraction, Multiplication, Division, Quotient, Remainder, Exponentiation
3. Integral Expressions: These Are The Kind Of Expressions That Produce Only Integer Results After All Computations And Type Conversions.
4. Floating Expressions: These Are The Kind Of Expressions That Produce Floating Point Numbers As a Result After All Computations And Type Conversions.
5. Relational Expressions: In These Types Of Expressions, Arithmetic Expressions Are Written On Both Sides Of the Relational Operator (>, <, >=, <=).
   These expressions are also called 
   Boolean expressions.
6. Logical Expressions: These Are Kinds Of Expressions That Result In Either True Or False. It Basically Specifies One Or More Conditions,
   with [and, or, not]
7. Bitwise Expressions: These Are The Kind Of Expressions In Which Computations Are Performed At the Bit Level.
8. Combinational Expressions: We Can Also Use Different Types Of Expressions In A Single Expression, And That Will Be Termed As Combinational Expressions.

#A program to understand more about expressions

#1.Constant Expression
a = 155 + 11050.35564
print(a)

#2.Arithmetic Expressions
a = 850
b = 180
add = a+b
sub = a-b
mul = a*b
div = a/b
quo = a//b
rem = a%b
exp = a**b

print("Addition: ", add)
print("Subtraction: ", sub)
print("Multiplication: ", mul)
print("Division: ", div)
print("Quotient: ", quo)
print("Remainder :", rem)
print("Exponentiation: ", exp)

#3.Integral Expressions
a = 52
b = 8
c = a+int(b)

print("Integral Expressions: ",c)

#4.Floating Expressions
a = 17
b = 4
c = a/b

print("Floating Expressions: ", c)

#5.Relational Expressions
a = 40
b = 85
c = 45
d = 52
e = a+b >= (c-d)
f = a-b <= (c+d)
x = a*b < (c/d)
y = a*b > (c*d)

print(e,f,x,y)

#6.Logical Expressions
a = (1500==1400)
b = (200 > 48)

#we can take out logical Expressions from it
x = a and b
y = b or a
z = not b

print(x,y,z)

#7.Bitwise Expressions
a = 10
x = a >> 2
y = a << 2

print(x,y)

#8.Combinational Expressions
a = 10
b = 20
c = a + (b>>1)
d = a - (b<<1)
e = a * (b or a)

print(c,d,e)
Result Shows
11205.35564
Addition:  1030
Subtraction:  670
Multiplication:  153000
Division:  4.722222222222222
Quotient:  4
Remainder: 130
Exponentiation:  197427037422210792385912104241757967839078409075382483869869056833091401730404306157195455753067403292341766445126647205223176403710964823033195389139030988256699988747764942118203827769859837263196877058438664690825620983482558713019187000118783578145189701904725672416593737512036372464145602099268298119418008074177350863465107977390289306640625000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Integral Expressions:  60
Floating Expressions:  4.25
True True False True
False True False
2 40
20 -30 200

Q5. This assignment statements, like spam = 10. What is the difference between an expression and a statement?

Ans. The expression represents something, Statement Does Something, 
Python evaluates the expression, python Executes the statement
expression shows results in a value but the statement shows results in action. In other words:
An expression evaluates to a single value. A statement does not.
An expression is something that can be reduced to a value, for example, "1+3" is an expression, which reduces and shows 4 as a result, 
but the statement shows print, str or imports, print("Hello from python")
Eg: expression - 4.6, statement - "Hello World", import sys, etc


Q6 After running the following code, what does the variable bacon contain? bacon = 22 bacon + 1

Ans. The bacon variable is set to 22. The bacon + 1 expression does not reassign the value of bacon 
that would need an assignment statement like bacon = bacon + 1

bacon = 22
bacon + 1 #It does not add plus 1 to 22 so it does not reassign value in the bacon variable
print(bacon)

#To add value to bacon
bacon = bacon + 1 #This method is used to reduce expression or develop statements in python
print(bacon)
The result shows 22 23

Q7. What should the values of the following two terms be? 'spam' + 'spamspam' 'spam' * 3

Ans. As shown in example both of these expression shows same string 'spamspamspam'

#lets test this code
a = 'spam' + 'spamspam'
b = 'spam' * 3
print(a,b)
spamspamspam spamspamspam


Q8. Why is eggs a valid variable name while 100 is invalid?

Ans. As shown in result SyntaxError: cannot assign to literal, which means variable name cannot begin with number or spaces or dash in between

#To print python programe with valid variable
eggs = 100
eggs100 = "egg"
_100 = "eggs"
eggs_100 = "buy more egg"

#invalid variables
100 = eggs   # not allowed
eggs 100 = "egg" # not allowed
eggs-100 = "egg" # not allowed


Q9. What three functions can be used to get the integer, floating-point number, or string version of a value? 

Ans. In Python these functions works and denote like this int() for integer, flot() for float number and str() for string. This functions are very helpful to get convert or pass something and get desired results.
#To print a program for integer, float, and string
a = int(52.4) #convert float number to integer
b = float(41) #convert integer number to float number
c = str("Hello Python")
print(a,b,c)
The Result shows 52 41.0 Hello Python

Q10. Why does this expression cause an error? How can you fix it? 'I have eaten ' + 99 + ' burritos.'

Ans. It shows that TypeError: can only concatenate str (not "int") to str, which means while making a statement a string and interger cannot combine with + sign to concatenate a new string of statement. so if we need to concatenate them then we need str function or convert, wrap int with double quotes, once all expressions becomes str then it will concatenate and not show any error.

# To print and check this expression
a = 'I have eaten ' + str(99) + ' burritos.'
b = 'I have eaten ' + '99' + ' burritos.'
print(a,b)

#This Will throw error
#'I have eaten ' + 99 + ' burritos.'

I have eaten 99 burritos. I have eaten 99 burritos.
Next Post Previous Post