Bedrock to Python

  • In C++/Java variable type can't be changed once declared, but python allows it. 
  • Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
           if 9 > 5:                                    if 11 > 2:
         print("Nine is greater")                 print("Eleven is greater") 
             (No Error)                                    (Error)

  • Comments can be placed at the end of a line, and Python will ignore the rest of the line: 
                print("Hello, World!"#This is a comment

        """
        This is a comment
        written in
        more than just one line
        """
        print("Hello, World!")

  • Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
  • String variables can be declared either by using single or double quotes.
            x = 78     # x is of type int
      y = "Yash" # y is of type str
       x=y         # x is now of type str

        Rules for Python variables:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

  • The Python print statement is often used to output variables.To combine both text and a variable, Python uses the + character or ",":
            x = "ptluvysp"                               x = "ptluvysp"
      print("I agree " + x)                      print("I agree ",x)
  • Global Variable - Inside function under normal declaration it is local variable, to convert it into global we use global  keyword.
  • To change the value of a global variable inside a function, refer to the variable by using the global keyword.
                def func():                                x = "awesome"
                       global x                                                                     def myfunc():                 
                       x = "fantastic"                                                          global x                          
                ("x" is now declared globally)                                                x = "fantastic"
                                                                                                   ("x" is changed in its global definition)
  
    Data Types -
  • Python has the following data types built-in by default, in these categories:



  • If we want to specify the data type, we can use constructor functions example of integer and string:- 
                                    x= str("Yash")                                     x = int(78.2) 
  • Complex numbers are written with a "j" as the imaginary part. You cannot convert complex numbers into another number type.                         

Comments