0% found this document useful (0 votes)
4 views6 pages

Essential Python Programming Tips

This document outlines key points to remember when coding in Python, including data types, variable naming conventions, and the importance of indentation. It provides examples of print statements, variable assignments, and mathematical operations, as well as practice questions for user input and string concatenation. The document emphasizes Python's case sensitivity and the dynamic nature of variable declarations.

Uploaded by

nothing12334343
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Essential Python Programming Tips

This document outlines key points to remember when coding in Python, including data types, variable naming conventions, and the importance of indentation. It provides examples of print statements, variable assignments, and mathematical operations, as well as practice questions for user input and string concatenation. The document emphasizes Python's case sensitivity and the dynamic nature of variable declarations.

Uploaded by

nothing12334343
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

IMPORTANT PYTHON KEY POINTS TO REMEMBER

 There is no declaration of data types


 There is no REPEAT UNTIL or CASE OFF cases in Python
 When coding in Python, you need to be careful with Indentation as the statements end or
change meaning or process with how indentation is applied
 Python is a Case Sensitive Language

Print / Output in Python

The print function outputs the text/custom text or any information which refers to a variable

Examples:

Text/Custom Text

print("Hello, this is my first class to understand basic functions of


Python")

Output:

Variable Information

x="Muneeb Arshad"
y=12.5
print(x)
print(y)
Output:

Note:
In the previous example, there are no declarations of any variable, however, they are created
the moment you assign a value to them, also their data type is assigned accordingly.
Since in Computer Science 9618, you are to include the declaration of variables as comment,
in which you are to specify their name you will assign to them and also state their data type.
Comments are initiated by using a ‘#’.
Example:

Things to remember when using Variables


 Variable should start with a letter or an underscore character
 Variable should be short, meaningful and must not contain any spaces
 Variable can only be in alphanumeric form, meaning that it may contain letters (A to
Z), numbers (0 to 9) and ‘_’
 Since Python is a case-sensitive language to code in so (Muneeb, muneeb, muneeB)
are three different variables
 A variable must not start with a number

Valid Examples
age = 17 Age = 18 _age = 19

age=17
Age=18
_age=19

print(age)
print(Age)
print(_age)
Output:

Invalid Examples
0age = 17 age-0 = 17 age 0 = 17
0age = 17
age-0 = 18
age 0 = 19

print(0age)
print(age-0)
print(age 0)
Output:
Data Types

There will be three main data types that we will be dealing with. They are:
o String (str) : “Text”
o Integer (int) : Whole Numbers
o Float (float): Real Numbers

How to check and change Data type

Example
Height=5.98
print(type(Height))
StdHeight=int(Height)
print(StdHeight)
print(type(StdHeight))

Output:

Mathematical Operators

 Addition ‘ + ’
 Subtraction ‘ – ‘
 Division ‘ / ‘
 Multiplication ‘ * ‘
 Exponent ‘**’
Practice Question
Assign 2 values to variables of your own choice. Multiply them with each other and add 50.2
to your answer. Store the solution in a new variable name (Solution) and print only the
integer part of the solution with a custom message. You must declare all variable as
comments

Solution:

num1=23
num2=45
Solution = (num1*num2)+50.2
print("The final answer is ", int(Solution))

Output:

String Concatenation

 A string is any form text specified within speech marks


 Concatenation is to link/join them together

Example
x="Muneeb" + "Arshad" + "123" + "*&^"
print(x)

Output:

Example
x="Muneeb"
y= "Arshad"
print(x,y)

Output:
Practice Question
Assign your First Name in a variable (Fname) and your Second Name in a
variable (Lname) and combined both of the string with space between them and
store it in a variable (Full_Name)
Example:
Fname="Muneeb"
Lname="Arshad"
Full_Name = Fname+" " +Lname
print(Full_Name)

Output:

Taking Input from the user

Example:
Number=input("Please enter a number of your choice: ")
print(Number)

Output:

Note:
When coding in Python, the input function always RETURNS a string value, so if it’s a
number which is to be used later for any arithmetic operation, then you must change its data
type to integer or float.
Practice Questions
1. Ask for two numbers from the user and add them together. Print the answer with a
relevant custom message.

2. Ask for two numbers from the user, one to be integer and the other to be float and add
them together. Print the answer with a relevant custom message and convert the
number to its integer type.

Solution 1
num1=input("Please enter the first number: ")
num2=input("Please enter the second number: ")
answer=int(num1)+int(num2)
print(answer)

Output:

Solution 2
num1=input("Please enter the first number: ")
num2=input("Please enter the second number: ")
_intnum2=int(float(num2))
solution=int(num1)+_intnum2
print(solution)

Output:

You might also like