Data Types and Variables
Data Types
- String: string data types are a series of characters enclosed in single ( ' ) or double ( " ) quotes. Example: "Hello World!", "ABCD", "1234"
- Number: Numbers are whole numbers or numeric values. We also call them integers. Example: 123, 456, -1,-2,0
- Float: Floats are decimal type numbers. Example: 1.2, 2.5, 3.14, -3.14 etc.
- Boolean: Booleans are also called Binary values. This datatype has only two values True and False. Value for True is 1 and Value for False is 0.
Operators
Operators are applied on values of different data types to change them. For example + (plus) operator can be used to add two numbers. Below are the common operators.
+
-
*
/
%
//
Behavior of an operator changes based on the datatypes. For example a plus operator is used to add two numeric values. 2 + 2 Output 4. However, a plus operator with string concatenates them. "abc" + "cdf" Output "abccdf".
Also, not every operator is applicable for every datatype. For example multiplying two strings "abc" * "cdf" does not make sense.
Variables
Use use variables to store the different values. Type of the variable is defined by the type of value stored.For example name = "akhilesh" Type of name is string, because the value "akhilesh" is of type of string.
Rules to Name Variables:
- Name of a variable should only contain alphanumeric characters and ( _ ) underscore.
- Name of a variable can not start with numbers.
- Name of a variable can not have special characters like %, #, $ etc.
- Name of a variable should be one word only. Example my age is not a valid variable name. However, myAge is a valid variable name.
- Name of a variable can not be any reserved keyword by Python such as True, False etc.
Assignments
From Aashi
ReplyDelete1. What is a string value type? What are the two different ways to create a string value?
A string value type is an series of characters.The two different ways to create a string value are:
1.Using single quotations around the string.(eg: ('Hi!'))
2.Using double quotations around thr string.(eg:("Hi!"))
#2. What is the difference between Float and Integer?
The difference between a Float and in Integer is that an Integer consists of a whole numerical value and a Float consists of a decimal value.
#3. What are the two different values for Boolean type? How do you write them?
The two different values for Bollean type are True and False.You write them by using the characters "T","r","u","e" and "F","a","l","s","e".
#4. What is the difference between equal to operator and assignment operator? How do you write and use these two operators?
Equal to operator: An operator that retutns True when two values are equal to each other.eg(
if a==89:
print("True")
Assignment operator:An operator that assigns a value to a variable.eg(
a=5*1.5
a=a+5
#5. Find which operators (+, -, *, /) can be applied to string types?
+
*
#6. Find which operators (+, -, *, /) can be applied to Boolean types?
all of them
#7.
'hello world!' -> string:
1 -> Integer
True -> Boolean
False -> Boolean
-7 -> Integer
3.14 -> Float
'abcd' -> String
a = 5
b= 5
c = 7
if (b==c) or (a==b):
print((b==c) / (a==b))
if (b==c) / (a==b):
print("Hurray")
else:
print("Not Hurray")
Good work Aashi!
Delete