What is syntax? • Syntax implies the set of rules which define how the program will be written and interpreted. • For example, this is the Python syntax: • print (“Welcome, John!”)
• In the above example, syntax implies the characters such as opening and closing brackets () and the inverted commas “” as well as the keyword “print”. • Output: • Welcome, John!
What is indentation? • Python uses indentation to identify the different blocks of code. • The white space indicates the indentation. • All the codes with the same spaces on the left belong to the same block of code. • Usually, the number of spaces on the left are four.
Take a look at the example below:
• Output: • 64 • In the example above, since the second line of code is a separate expression, the indentation is not correct. Hence, when you run the code it throws an error. But when you remove the space/indentation, the correct output is shown.
What are variables and operators? • Variables • Variables are characters or words that are given a user-defined value. • Variables, as the name suggests can be varied during the execution of the code. • They are allocated a space in the computer memory based on their data type. • Python doesn't require explicit declaration of the data type for a variable.
Example 1
• Output: • 45 • 1456.8 • Alex
Example 2
• Output: • 100
Changing the value of a variable
• Output: • Original value: 100 • Updated value: 120.3
Many Values to Multiple Variables • Python allows you to assign values to multiple variables in one line:
• Output: • Orange • Banana • Cherry
One Value to Multiple Variables • you can assign the same value to multiple variables in one line:
• Output: • Orange • Orange • Orange
Unpack a Collection • If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.
Output Variables • The Python print() function is often used to ou • tput variables • x = "Python is awesome" print(x) • Output • Python is awesome
• In the print() function, you output multiple variables, separated by a comma: • x = "Python" y = "is" z = "awesome" print(x, y, z) • Output • Python is awesome
• You can also use the + operator to output multiple variables: • x = "Python " y = "is " z = "awesome" print(x + y + z) • Output • Python is awesome
• For numbers, the + character works as a mathematical operator: • x = 5 y = 10 print(x + y) • Output • 15
• In the print() function, when you try to combine a string and a number with the + operator, Python will give you an error: • x = 5 y = "John" print(x + y)
Output • TypeError: unsupported operand type(s) for +: 'int' and 'str'
• The best way to output multiple variables in the print() function is to separate them with commas, which even support different data types: • x = 5 y = "John" print(x, y) • Output • 5 John
Global Variables • Variables that are created outside of a function (as in all of the examples above) are known as global variables. • Global variables can be used by everyone, both inside of functions and outside. • Example • Create a variable outside of a function, and use it inside the function • x = "awesome" def myfunc(): print("Python is " + x) myfunc()
• Output • Python is awesome
• If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
• Create a variable inside a function, with the same name as the global variable • x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
• Output • Python is fantastic Python is awesome
The global Keyword • Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. • To create a global variable inside a function, you can use the global keyword.
• If you use the global keyword, the variable belongs to the global scope: • def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
• Output • Python is fantastic
• Also, use the global keyword if you want to change a global variable inside a function. • Example • To change the value of a global variable inside a function, refer to the variable by using the global keyword: • x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
• Output • Python is fantastic
Exercise • Create a variable named carname and assign the value Volvo to it. • Create a variable named x and assign the value 50 to it. • Display the sum of 5 + 10, using two variables: x and y. • Create a variable called z, assign x + y to it, and display the result

Python in easy way It includes different data type

  • 2.
    What is syntax? •Syntax implies the set of rules which define how the program will be written and interpreted. • For example, this is the Python syntax: • print (“Welcome, John!”)
  • 3.
    • In theabove example, syntax implies the characters such as opening and closing brackets () and the inverted commas “” as well as the keyword “print”. • Output: • Welcome, John!
  • 4.
    What is indentation? •Python uses indentation to identify the different blocks of code. • The white space indicates the indentation. • All the codes with the same spaces on the left belong to the same block of code. • Usually, the number of spaces on the left are four.
  • 5.
    Take a lookat the example below:
  • 8.
    • Output: • 64 •In the example above, since the second line of code is a separate expression, the indentation is not correct. Hence, when you run the code it throws an error. But when you remove the space/indentation, the correct output is shown.
  • 9.
    What are variablesand operators? • Variables • Variables are characters or words that are given a user-defined value. • Variables, as the name suggests can be varied during the execution of the code. • They are allocated a space in the computer memory based on their data type. • Python doesn't require explicit declaration of the data type for a variable.
  • 10.
  • 11.
    • Output: • 45 •1456.8 • Alex
  • 12.
  • 13.
  • 14.
    Changing the valueof a variable
  • 15.
    • Output: • Originalvalue: 100 • Updated value: 120.3
  • 16.
    Many Values toMultiple Variables • Python allows you to assign values to multiple variables in one line:
  • 17.
    • Output: • Orange •Banana • Cherry
  • 18.
    One Value toMultiple Variables • you can assign the same value to multiple variables in one line:
  • 19.
    • Output: • Orange •Orange • Orange
  • 20.
    Unpack a Collection •If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.
  • 21.
    Output Variables • ThePython print() function is often used to ou • tput variables • x = "Python is awesome" print(x) • Output • Python is awesome
  • 22.
    • In theprint() function, you output multiple variables, separated by a comma: • x = "Python" y = "is" z = "awesome" print(x, y, z) • Output • Python is awesome
  • 23.
    • You canalso use the + operator to output multiple variables: • x = "Python " y = "is " z = "awesome" print(x + y + z) • Output • Python is awesome
  • 24.
    • For numbers,the + character works as a mathematical operator: • x = 5 y = 10 print(x + y) • Output • 15
  • 25.
    • In theprint() function, when you try to combine a string and a number with the + operator, Python will give you an error: • x = 5 y = "John" print(x + y)
  • 26.
    Output • TypeError: unsupportedoperand type(s) for +: 'int' and 'str'
  • 27.
    • The bestway to output multiple variables in the print() function is to separate them with commas, which even support different data types: • x = 5 y = "John" print(x, y) • Output • 5 John
  • 28.
    Global Variables • Variablesthat are created outside of a function (as in all of the examples above) are known as global variables. • Global variables can be used by everyone, both inside of functions and outside. • Example • Create a variable outside of a function, and use it inside the function • x = "awesome" def myfunc(): print("Python is " + x) myfunc()
  • 29.
  • 30.
    • If youcreate a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
  • 31.
    • Create avariable inside a function, with the same name as the global variable • x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
  • 32.
    • Output • Pythonis fantastic Python is awesome
  • 33.
    The global Keyword •Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. • To create a global variable inside a function, you can use the global keyword.
  • 34.
    • If youuse the global keyword, the variable belongs to the global scope: • def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
  • 35.
  • 36.
    • Also, usethe global keyword if you want to change a global variable inside a function. • Example • To change the value of a global variable inside a function, refer to the variable by using the global keyword: • x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
  • 37.
  • 38.
    Exercise • Create avariable named carname and assign the value Volvo to it. • Create a variable named x and assign the value 50 to it. • Display the sum of 5 + 10, using two variables: x and y. • Create a variable called z, assign x + y to it, and display the result