Thursday, November 30, 2023

Day 2 - Primitive Data Types

#-------------------int, float, boolean and String, len(), type() ------------------------------

Retrieving each char from String

# String: consider this as a string of characters tied together.

# We can retrieve each character via its index in the String, which starts from 0 till the length of the string - 1

print("First letter in Hello: " + "Hello"[0])

print("Last letter in Hello: " + "Hello"[-1])

print("Last letter in Hello: " + "Hello"[len("Hello")-1])

# The last letter can be accessed by -1 or it can be accessed by finding the length of the string using len() function and subtracting it by 1.

# len() function is used to find the length of the string. We cannot pass an int/float to it. 

print ("Length of the string 'Hi Ram': " + str(len("Hi Ram")))

''' 

Note that the print function '+' operator is overloaded only to add 2 strings. If we need to add another datatype, we should:

1. Either convert it to String as in the above statement

2. Or use fString.

Similarly to find length of int, convert to string and use the len() function

'''

# type(<var name>) function is used to find the data type of that variable in question.

user_name = "Ram"

age = 5

weight = 5.0

print (type(user_name))

print (type(age))

print (type(weight))

# Although the weight is 5.0, since it uses a decimal point, it is a float variable

# True and False are the 2 boolean values


# ---------------------------- Simple Bio Data Entry and Display------------------------

username = input("Enter your name\n")

age = input("Enter your name\n")

weight = input("Enter your weight\n")

gender = input("Enter your gender 'M' or 'F' \n")


print ("--------------Your Bio Data---------------\n")

print ("Name: " + username + "\n")

print ("Age: " + str(age) + "\n")

print ("Weight: " + str(weight) + "\n")

print ("Gender: " + gender + "\n")

print ("---------------------End---------------------\n")

# --------------------------------------------------------------------------------------------------------




-----------------------------------------End of Day 2 ----------------------------------------------




No comments:

Post a Comment

Day 2 - Primitive Data Types

#-------------------int, float, boolean and String, len(), type() ------------------------------ Retrieving each char from String # String: ...