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 ----------------------------------------------




Wednesday, November 29, 2023

Day 1 - print & input functions

Using replit.com for my py coding and execution.

# --------------------------------------- print() ----------------------------------------------

#print() function to print the contents in the console output

print("Hello World")

# To add 2 strings and print, we can use the + operator. Note that both the values in the operation are strings.

print("Add 2 strings using '+' operator: 'Ram' + 'sundar' will give: " + "Ram" + "sundar")

# From the above 2 statements, we can see that Strings can be represented in single or double quotes


# --------------------------------------- input() ----------------------------------------------

#input() function to get input from end user and store the string in a variable and printing it

# The input values are always string. So if we need an int/float, we should convert the data type.

user_name = input('Enter your name')

print("Name: " + user_name)

#OR as below without using the help of a variable

print ( "Name" " + input('Enter your name'))

age = int(input("Enter age"))

print ("Age is :" + str(age))


-----------------------------------------End of Day 1 ----------------------------------------------





Day 2 - Primitive Data Types

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